CFUnited - Nic Tunney - Intro To OO With CF
Posted By : todd sharp Posted At : June 27, 2007 9:17 AM Posted In: CFUnited 2007, ColdFusion
0
My first session is Intro To OO With ColdFusion by Nic Tunney. On a side note - last evening I was told by several folks that I look exactly like Nic. I was even approached by someone in the lobby who thought I was Nic. Still trying to figure out if this is a good thing or a bad thing (just kidding).
Why Use OO Concepts?
- Scalability
- Uniformity
- Reusability (Copy/Paste is not reusability.)
- Maintainability
ColdFusion is not pure OO. Pure OO languages treat everything in the language as an object. CF allows us to use tools within the language that apply OO concepts but does not require the use of objects. OO is not a language or a framework. It is principles, best practices and patterns.
Class:
- Properties
- Constructor
- Accessors/Mutators (getters/setters)
- Object Methods
- Sometimes Data Access (CRUD)
Object:
- Instantiated class
- Real world entity
Defining an object. Who can I talk to, Who do I depend on, What can I do? A person object can walk, talk. Who can it talk to? Where can it walk. Encapsulation: Good API defines the object.
Polymorphism: One class, many instantiations. An employee class could have a manager, etc.
Inheritance and composition. An employee "has an" (inheritance) email address - not the employee "is a" (composition) email address. Nic says we should try to favor composition since we can not do multiple inheritance in CF.
Applying OO principles to ColdFusion.
Creating a class -
Properties:
<cfset variables.firstName = "" />
Constructor:
<cfreturn this />
</cffunction>
Accessor (Getter):
...
<cfreturn variables.id />
</cffunction>
Mutator (Setter):
<cfargument name="id" required="true" type="numeric" />
<cfset variables.id = arguments.id />
</cffunction>
Public/Private methods. Data Access - CRUD. The "what can i do" of the object. Differing schools of thought about whether they should be within the object or in a seperate DAO.
Instantiating an object - best practice is to use createObject like so:
Now you can call the methods in the object directly.
Persistence methods. CRUD (create, read, update, delete). Can be further encapsulated with a commit() or save() method which would check if the object exists and either create or update appropriately.
Gateway objects. Used for accessing multiple objects or records. For small applications, methods can be put into one main data access object. For large applications, best practice is to move methods into object specific gateways.
Conclusion: OO is not new - just new to CF. CF is not pure OO. Classes contain encapsulated properties, accessor and mutator methods, private and public methods. Objects are instantiated classes. Objects are programatic representations of real world entities. Think objects instead of Relational DB. Composition is preferred to inheritance. DAO methods may be in the class or in its own class (CRUD). Gateways access recordsets or multiple objects. Code generators can be your best friend!
In summary I felt Nic's session was a very good intro and overview of the main principles and concepts of OO. Obviously the topic is not something that anyone can completely grasp and feel comfortable with in an hour session, but Nic did well with the intro. Overall the session definitely peaked my interest and desire to learn more.


