OOP Concepts Applied In CFML: Abstraction

Word Count: 149

One of the most fundamental concepts you will need to grasp is Abstraction. Let's take a look at the Wikipedia definition and we can break it down from there.

In computer science, abstraction is a mechanism and practice to reduce and factor out details so that one can focus on a few concepts at a time. The following English definition of abstraction helps to understand how this term applies to Computer Science, IT and Objects - i.e. abstraction is: A concept or idea not associated with any specific instance

Like everything else in life people really like to complicate things to stand above the rest. When developers speak of OO you will often hear Encapsulation and Abstraction. At first it may sound complicated but if you look at the definition, it really is not.

Abstraction in the CFML land is something we all need to practice. I image anyone out there who doesn't know anything about OO has written what we now refer to as the Uber CFC. While this is not a concept you want to follow its much better than the alternative, which is cramming presentation and business logic into a CFML template.

We can use this as a great starting point for abstraction. Lets say we are writing a simple contact manager application for our human resources department. In our old application we might of had one very large Uber Component called Employee. This is a great starting point but we can easily improve the architecture of the application by applying the very simple Abstraction concept. Here is an example of what our Employee Component might of looked like before we refactor.

Properties
id
fname
lname
email
salary

Methods
init
get/set id
get/set fname
get/set lname
get/set email
get/set salary

This is not as Uber as you thought it would be but just bear with me for a sec. In a real world this component would be huge but I simplified it for this example. So the first problem we have is that we have classified every employee into one group. Taking from my own position you can easily break everyone down into 2 distinct categories, part time and salary. A part time employee would never have a salary property so why would set one. From this we could create an interface (I know I know, just run with it).

Update: I ran through this example so fast today I didn't event check my code. This example should be using Inhertiance (I am learning).

This may not be the greatest example but it show the exact reason to use Abstraction. Not all Employees are the same so you don't want to classify them as the same. Abstraction here breaks our employees into 2 distinct Objects. While we will want to treat them as employees some time there are time when will need to treat them as a part time or full time employee specifically. Without this level of Abstraction your component will start to get larger because now you need to know if the employee is full time or part time employee.

To take it one step further different departments may have different properties or behaviors that other departments do not. You could then abstract your employee component into different departments hr/engineering/payroll etc and then classify your employees into part time / full time.

I have real world example of abstraction that I plan to share with shortly but until that time I would love to hear your thoughts on this. I would also love to hear some more examples that you have come across that could help us. Before I move on to other OO concepts I want to bet this one to death so we can all really have it sink in. Again, my name is NOT  Hal Helms so I am not not an expert on the subject so please correct me where need be!

Comments

#1 Posted By: Michael Sharman Posted On: 10/16/08 5:25 PM
Hi Dan,

Wondering if you could explain a bit more about your choice of using an Interface for this scenario instead of Inheritance?

btw I failed your captcha (twice) and had to type the "comments" again :(
#2 Posted By: ike Posted On: 10/16/08 8:34 PM
I think you're right that payroll is a good place for an abstraction, although personally I would want to remove it from the employee rather than placing interfaces on the employee like this. I want to use the phrase "class explosion" here, but I don't tend to use interfaces much so I'm not sure if I would be describing it with the right terminology. But it seems to me like the use of interfaces here could maybe produce more code than might be really needed. So I'd probably go with a separate sort of abstract PayrollType class (CFC) that could be extended and that way you could use inheritance to keep the basic functionality in the abstract PayrollType class (so you only have to write it once) and polymorphism to handle the places where each type (PayrollSalary CFC vs. PayrollHourly CFC) needs to do something differently to calculate a paycheck.
#3 Posted By: Dan Vega Posted On: 10/16/08 8:44 PM |
Author Comment
There really was no thought put into this example. I take a lot of this from the code I have written in Action Script lately. Interfaces are great for the contract alone. Let's say we wanted to create another type of employee called contract employee. This type of person does not get a salary because when we think of salary we think yearly. This contract then forces us to define certain behaviors of the new contract employee while abstracting out the detials of that particular employee. I will admit that I have not used interfaces a whole lot in CF. I plan to though in the near future to find out if I like them or not.
#4 Posted By: Dan Vega Posted On: 10/16/08 8:57 PM |
Author Comment
@Michael
I will get into some more example of different OO concepts later on in this series but I really want to pound the idea of Abstraction into the ground before I move on.
#5 Posted By: John Whish Posted On: 10/17/08 4:29 AM
@Dan, looking forward to more from your OOP series and it's great to see someone else writing about the basics. I know this is a slight deviation but your example would have problems if a part time employee became a full time employee. To me, this looks like a classic case to use composition.
#6 Posted By: John Whish Posted On: 10/17/08 4:32 AM
@Dan, another thought! You could have a person class that employee inherits, and then the employee could by composed of employee types. Having said that, like you I'm no OO guru :)
#7 Posted By: Dan Vega Posted On: 10/17/08 8:21 AM |
Author Comment
Great thoughts John. My plan is to keep speaking on the Architecture side of things. I think once I get that part down the code is the easy part ;)


Post Your Comment







Show Captcha

If you subscribe, any new posts to this thread will be sent to your email address.

Copyright © 2007 Dan Vega | BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.