CFUnited - Nic Tunney - Intro To OO With CF

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.id = 0 />
<cfset variables.firstName = "" />

Constructor:

<cffunction name="init" access="public" returnType="Employee" output="false">
<cfreturn this />
</cffunction>

Accessor (Getter):

<cffuntion name="getID" ...>
...
<cfreturn variables.id />
</cffunction>

Mutator (Setter):

<cffunction name="setID" ...>
<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:

<cfset myObject = createObject("component", path_to_cfc).init() />

Now you can call the methods in the object directly.

<cfset id = myObject.getID() />

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.

Comments

Calendar

Sun Mon Tue Wed Thu Fri Sat
  12345
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31   

Subscribe

Enter your email address to subscribe to this blog.

Tags

actionscript ajax blogging cfsnippets coldfusion flash forms flex misc model-glue off topic personal project learn slidesix sql

Recent Comments

More CF+Java: Compiling Classes And Persisting Objects
Getburl said: I have been attempting to get Db4o working in my CF application and I have not succeeded. I would lo... [More]

Thoughts On Ajax Frameworks And ColdFusion/Adobe
Erast said: http://fanniecollins.10gb... emo http://gracetrevino.phree...... [More]

Extending Ext With Ext Extensions
Erast said: http://fanniecollins.10gb... emo http://gracetrevino.phree...... [More]

CF Needs An Open Source Contact List Importer
Kay Smoljak said: Heh, the fact that sites DO it doesn't mean they SHOULD. To us it's ok, but to a non-tech-savvy user... [More]

A Few Project Updates
Helena said: Now punctually what is the situation ? [More]

RSS


coldfusionbloggers

FullAsAGoog MXNA

Consumed By Feed-Squirrel.com