Nicholas Tunney - AboutWeb - Object Orientation for ColdFusion
I figured I needed an overview of OO in CF in a practical sense, so I checked out this session.
Basic OO overview - standard stuff
The class is the core of OO - properties, constructors, accessors, object methods, sometimes CRUD.
An object is an instantiated class.
Think like the object. (Simon's ObjectThink)
- Who can I talk do?
- Who do I depend on?
- What can I do?
Encapsulation - Black boxing
- Private properties
- Accessors/Mutators - Getters and Setters - Developers don't just access structure.color, they getColor() instead.
- Watch out for over engineering - Very easy to do
Polymorphism - one class, many instantiations
Example: Employee Class
- Manager
- Accountant
- Programmer
Inheritance and Composition - The "is a" "has a" paradigm
- Employee
- Employee has an email address
- Manager is an employee
- "Favor Composition" - always think of composition first
Practical application in ColdFusion
Most people just use CFCs as "query containers". That's okay, but you are missing out on more power.
Email Nicholas for a library of CFEclipse snippets!
Creating a class - Constructor
<cffunction name=init" access="public" returntype="Employee" output="false">
<cfreturn this />
</cffunction>
creating a public function places varaiables into the variable scope and then this scope.
Creating a class - Accessor (Getter)
<cffunction name="getID" access="public" returntype="numeric" output="false">
...calculations...
<cfreturn variables.id />
</cffunction>
Creating a class - Mutator (setter)
<cffunction name="setID" access="public" returntype="void" output="false">
<cfargument name="id" require" true" type="numeric">
...calc...
<cfset variabes.id = arguments.id />
</cffunction>
Creating a class - public and private methods - what other methods need to be in the object?
- DAO - CRUD
- The "what can I do" of the object
Instantiating an object - best practices
<cfset myObject="createObject("component", path_to_cfc).init() />
example:
<cfset Carl="createObject("component", "cfc.Employee").init(1, "Carl") />
Interacting with Properties
- Properties are in private scope (encapsulation
<cfset Carl.setFirstName("Carl") />
<cfset firstName = Carl.getFirstName() />
Persistance Methods - CRUD - can pull them out into other objects, but no problem to just stick them in your main object. Can be further encapsulated using a commit() method. The object can be set to "figure it out" when a dev calls the commit method. ie, update if already exists, add if doesn't exist.
Gateway Objects - singleton object instantiated once per app.
- Used for accessing multiple objects or records
- For small apps, methods can be put into one main data access object
- for large apps, best practice is to move methods into object specific gateways
example: list and output all employees. Does it need to be it's own object? Or loop over objects? No, just stick query in Gateway object and call that one method to get your list.
Nick's sites
nictunney.com
cfoop.org
TIP!
Use Poseidon for UML and diagramming. It may also have ColdFusion hooks. Nick also absolutely loves Model-Glue. It will "get out of your way" when you need to. Nick loves this feature! Sounds like I would love it too. You don't ALWAYS needs to live strictly within the OO, MVC structure.
Migrating a site: Look at existing messy app. Create a Model-Glue directory structure off to the side. Attack the app in small chunks and modularize everything logically.
There are no comments for this entry.
[Add Comment]