CFUnited Speaker Q&A

CFUnited folks have asked the speakers to answer some questions regarding the conference and post on our blogs, here's my response:

Topic: Leveraging Basic Object Oriented Concepts in ColdFusion - http://cfunited.com/2009/topics/295

1. Have you spoken at CFUnited in the past? What would you tell someone who hasn't been to CFUnited before?
I spoke at CFUnited 2008, and I have attended every year since the CFun days. If you have never been to CFUnited, you are missing out in one of the best chances to trully make your job better in a number of facets. You'll learn from industry experts on the latest ColdFusion and Flex/Air trends, not only this gives you the power to build better apps but makes you more marketable when looking for a new job. You'll have a chance to meet and network with a great number of people who's day to day experiences are similar to your own, this is a huge resource when you stumble on a problem or if you are also looking for talent to help in your projects. The Expo hall is a great chance to checkout what products and services are there to make your day to day life easier and help achieve your development goals.

2. Why should people attend your session(s)?
If you are not yet doing Object-Oriented programming because you keep hearing all those buzz words like Singletons, Decorators, and other Design Patterns and can't find an example using ColdFusion to help you understand what they mean, you may want to checkout my "Leveraging Basic Object Oriented and Design Patterns in ColdFusion" session. This session is built to help you break into the OO world with a basic understanding of Design Patterns and how they help solve re-occurring problems.

3. Do you have any projects in the works that you will be revealing at CFUnited?
It's too early to tell...

4. Besides your topic, what other sessions are you looking forward to?
I'm looking forward to seeing some of the Coldbox sessions, more on Flex and AIR development with ColdFusion.

5. Where can people find you at CFUnited? (At the bar, networking, working, in your room, etc.)
AboutWeb booth, sessions, bar, networking, bar, pool, golf course and yes.. the bar!

6. What's the latest news with you? Has anything changed since last CFUnited?
My wife and I are expecting our first child, boy, due July 1st! He may be making his first appearance at CFUnited this year!!!

7. When you are not working what do you like to do in your free time?
Try out new technologies and frameworks, golfing and grilling with family and friends.

Capital Area Flex User's Group April Meeting Reminder

Just a quick meeting reminder that the Capital Area Flex User's Group is meeting this Wednesday night at 6:30pm at AboutWeb.

We have another amazing meeting lined up for you tomorrow night. To start off the night we'll have Ashish Jaiman from Microsoft present on Silverlight. This should be an opportunity to see where Microsoft stands with it's competing product and perhaps inspire some friendly rivalry.

After a break for food and drinks, Kelly Brown will be presenting on BlazeDS, an open source Flex/Air server initiative from Adobe.

Please arrive by 6:30pm at our Rockville office as presentations will begin promptly at 7:00pm. For directions and more information visit www.dc-flex.org.

As always, we will be broadcasting the event live on Adobe Connect. To follow the event online (sorry no e-food though!) go to http://adobechats.adobe.acrobat.com/capfug0408/.

Flex Camp Washington, DC 2008

Just want to pass on the word that tomorrow AboutWeb LLC and Adobe Systems will be hosting the Flex Camp Washington, DC 2008. This is one-day gathering with Adobe Flex experts covering everything you need to know about the power of Flex, the upcoming release of Flex 3, and a bit about the much anticipated Adobe Air.

Since we have already surpassed the registration capacity, we are going to be broadcasting the entire event live on Adobe Connect from 9:30am to 12:00pm and then after lunch break from 1:00pm to 5:00pm (All times are Eastern Standard Time).

To view the event go to http://adobechats.adobe.acrobat.com/dcflexcamp08/, choose the "Enter as a Guest" option and click "Enter Room" after entering your name. We look forward to seeing you online!

Capital Area Flex User Group - November 07 Recorded Session

As mentioned previously we have started to broadcast live and record the Capital Area Flex User Group meetings. Last night we had a great turnout and special thanks to Danny Dura and Christian Cantrell for coming out and presenting.

Here are the recorded sessions:

Danny Dura, Adobe Evangelist
Adobe AIR Overview
http://adobechats.adobe.acrobat.com/p26954584/

Christian Cantrell, AIR Product Manager
Deconstructing AIR Applications
http://adobechats.adobe.acrobat.com/p41045993/

Capital Area Flex User's Group - Now Broadcasting Live!

Just want to quickly mention that tonight's CAPFug meeting will be the first of many that will be broadcast live using Adobe Connect.

Tonight's meeting with our special guest speakers from Adobe can be seen here:

http://adobechats.adobe.acrobat.com/capfug1107

Capital Area Flex User's Group - A breath of fresh AIR!

Just a quick note that tomorrow evening, Wednesday, November 7th, the Capital Area Flex User's Group(CAPFug) will be hosting two special guest speakers from Adobe.

Danny Dura, Adobe Evangelist, will be giving an overview of Adobe AIR and Christian Cantrell, AIR Product Manager, will dive into some details with "Deconstructing AIR Applications".

The meeting will start shortly after 6:00pm here at the AboutWeb Office and as always, free pizza, sodas and snacks are provided during the social break between sessions.


Capital Area Flex User's Group(CAPFug)
AboutWeb Office
6177 Executive Blvd
Rockville, MD 20852
(301) 468-9246

ObjectFactory Explained

In my previous post, I posted some code from the CentralPA CFUG presentation that Scott Stroz and I used. It used an ObjectFactory.cfc file that I had created prior to the presentation and am kicking myself for not doing/using this earlier. Now you're probably saying to yourself... "Another ObjectFactory!!!". Yes, this one I hope will prove to have its uses.

First, a bit about objectFactories. In a nutshell, objectFactories are nothing more then an object that returns other objects given a key. Meaning once objectFactory is instantiated in the application (in our case in the Application scope as a singleton), I can simply get to any objects "known" within the objectFactory by calling it like this:

Listing 1
<cfset myObjectInstance = application.objectFactory.create("ObjectKey") />

One of the advantages to this approach is obvious, there's no need to defined the entire path to the object everytime I need to instantiate it. This comes very handy if I have to make changes to my object paths, for example while using a proxy object to wrap the original object (see Decorator Pattern). When using an objectFactory, all my object paths are all in one place and not scattered throughout my entire application. Here's an example of a regular objectFactory:

Listing 2 - ObjectFactory.cfc
<cfcomponent displayname="ObjectFactory">

<!--- constructor method --->
<cffunction name="init" access="public" returntype="ObjectFactory" output="false" hint="constructor method">
<cfreturn this />
</cffunction>

<!--- public methods --->
<cffunction name="create" access="public" returntype="Any" output="false" hint="I return an object">
<cfargument name="objectKey" type="String" required="yes" />
<cfset var object = 0 />
<cfswitch expression="#arguments.objectKey#">
<cfcase value="Registrant">
<cfset object = createObject("component","cfugDemo.com.model.registrant.Registrant") />
</cfcase>
<cfcase value="RegistrantGateway">
    <cfset object = createObject("component","cfugDemo.com.model.registrant.RegistrantGateway") />
</cfcase>
<cfcase value="Fee">
<cfset object = createObject("component","cfugDemo.com.model.fee.Fee") />
</cfcase>
<cfcase value="FeeGateway">
<cfset object = createObject("component","cfugDemo.com.model.fee.FeeGateway") />
</cfcase>
</cfswitch>
</cffunction>

</cfcomponent>

And this could be instantiated in my application as:

Listing 3 - Application.cfm
...

<!--- objectFactory - singleton --->
<cfif NOT structKeyExists(application,"objectFactory") OR url.reInit>
<cfset application.objectFactory = createObject("component","cfugDemo.com.model.objectFactory.ObjectFactory").init() />
</cfif>

...

And anytime I want to use an instance of an object I simply call it like this:

Listing 4
<!--- instantiate registrant --->
<cfset registrant = application.objectFactory.create("Registrant").init() />

As you can see the create method simply returns the appropriate object. So if I need to change the path to use a proxy object or an object that extends the object type already in my code I simply edit the ObjectFactory.cfc file.

Okay, that was a basic example of objectFactory, now why implement object oriented principles and still have to edit the ObjectFactory.cfc file everytime I want it to be able to return a new object. Isn't the reason we like CFC's because we can reuse them across our applications? Hence the improvements I made to the ObjectFactory.cfc. First, why hard code all objects in my application in the cfswitch statement if I can simply tell the ObjectFactory.cfc what objects are there on application start? By allowing me to register each object information once into my factory and store it into a Struct data type, it now can be reused and also the create method can run faster without the need to figure out which object to instantiate via the cfswitch.

Another improvement made was to be able to tell the ObjectFactory in the register method that an object is a singleton, meaning I only want that object instantiated once in my entire application and shared everytime it's requested. This is very useful for objects like DAO's, Gateways, and Config Beans. Since in ColdFusion complex objects are passed by reference, the ObjectFactory simply stores a reference to the instantiated object before it returns the object to the user. Since ObjectFactory is already being persisted in the Application scope, the singleton object is also persisted. So let's take a look at the new improved Object Factory below:

Listing 5 - ObjectFactory.cfc
<cfcomponent displayname="ObjectFactory" hint="ObjectFactory - Class Factory Object">

<!--- properties --->
<cfset variables.objectRegistry = 0 />
<cfset variables.applicationSingletons = 0 />

<!--- constructor init() method --->
<cffunction name="init" access="public" returntype="ObjectFactory" output="false" hint="initiates instance of ObjectFactory">
<cfset setObjectRegistry(structNew()) />
<cfset setSingletons(structNew()) />
<cfreturn this />
</cffunction>

<!--- getters/setters accessors methods --->
<cffunction name="getObjectRegistry" access="private" returntype="Struct" output="false" hint="gets objectRegistry property">
<cfreturn variables.objectRegistry />
</cffunction>
<cffunction name="setObjectRegistry" access="private" returntype="Void" output="false" hint="sets objectRegistry property">
<cfargument name="objectRegistry" type="Struct" required="yes" hint="value of objectRegistry" />
<cfset variables.objectRegistry = arguments.objectRegistry />
</cffunction>

<cffunction name="getSingletons" access="private" returntype="Struct" output="false" hint="gets singletons property">
<cfreturn variables.singletons />
</cffunction>
<cffunction name="setSingletons" access="private" returntype="Void" output="false" hint="sets singletons property">
<cfargument name="singletons" type="Struct" required="yes" hint="value of singletons" />
<cfset variables.singletons = arguments.singletons />
</cffunction>

<!--- public methods --->
<cffunction name="register" access="public" returntype="Void" output="false" hint="registers object information into factory">
<cfargument name="key" type="String" required="yes" hint />
<cfargument name="object" type="String" required="yes" />
<cfargument name="isSingleton" type="Boolean" required="no" default="false" />
<cfargument name="type" type="String" required="no" default="Component" />
<!--- init local var(s) --->
<cfset var registerComponent = getObjectRegistry() />
<!--- register component info --->
<cfset registerComponent[arguments.key] = structNew() />
<cfset registerComponent[arguments.key].object = arguments.object />
<cfset registerComponent[arguments.key].isSingleton = arguments.isSingleton />
<cfset registerComponent[arguments.key].type = arguments.type />
</cffunction>

<cffunction name="unregister" access="public" returntype="Void" output="false" hint="unregisters object information from factory">
<cfargument name="key" type="String" required="yes" />
<!--- if object is registered, remove it from registry --->
<cfif structKeyExists(getObjectRegistry(),arguments.key)>
<cfset structDelete(getObjectRegistry(),arguments.key) />
</cfif>
</cffunction>

<cffunction name="create" access="public" returntype="Any" output="false" hint="returns instantiated object">
<cfargument name="key" type="String" required="yes" />
<!--- init local var(s) --->
<cfset var objectRegistry = getObjectRegistry() />
<cfset var singletons = getSingletons() />
<cfset var registeredObject = 0 />
<cfset var object = 0 />
<!--- check if object is in objectRegistry --->
<cfif structKeyExists(objectRegistry,arguments.key)>
<cfset registeredObject = objectRegistry[arguments.key] />
<!--- if object was flagged as singleton and has already been instantiated get object from singleton struct --->
<cfif registeredObject.isSingleton AND structKeyExists(singletons,arguments.key)>
<cfset object = singletons[arguments.key] />
<cfelse>
<!--- instantiate new object to return --->
<cfset object = createObject("#registeredObject.type#","#registeredObject.object#") />
<!--- if new object is to be instantiated as a singleton add instance reference to applicationSingletons struct --->
<cfif registeredObject.isSingleton>
<cfset singletons[arguments.key] = object />
</cfif>
</cfif>
<cfelse>
<!--- return false if key does not exist in objectRegistry --->
<cfset object = false />
</cfif>
<cfreturn object />
</cffunction>

</cfcomponent>

Notice that once I instantiate the object (see Listing 4) I can then register each object into it:

Listing 6 - Application.cfm
...

<!--- register objects --->
<cfif NOT structKeyExists(application,"objectFactory") OR url.reInit>
<!--- registrant --->
<cfset application.objectFactory.register("Registrant","cfugdemo.com.model.registrant.Registrant") />
<cfset application.objectFactory.register("RegistrantGateway","cfugdemo.com.model.registrant.RegistrantGateway",true) />
<!--- fee --->
<cfset application.objectFactory.register("Fee","cfugdemo.com.model.fee.Fee") />
<cfset application.objectFactory.register("FeeGateway","cfugdemo.com.model.fee.FeeGateway",true) />
</cfif>

...

For those objects I want to be instantiated as singletons like RegistrantGateway and FeeGateway notice the third optional argument is set to true. Also, in this case we are registering all objects explicitly in code, you could also read an xml file with objects definition info in it and loop through the content and register them automatically. As you can see this ObjectFactory can now be reused in any of my applications without having to make application specific changes to it, it's also a bit faster in that it doesn't have to do any loop or switch statements to find the object to instantiate it (this would really be significant in applications with many objects but worth mentioning).

Object-Oriented Programming in ColdFusion Presentation

Just a quick note that I'll be presenting for the Central Pennsylvania ColdFusion User's Group with Nic Tunney on "Using Object-Oriented Programming in ColdFusion". This will be a basic overview of OO, advantages over procedural coding, and how ColdFusion can take advantage of some of these concepts.

It will take place on Thursday, October 19th at 7:00pm at Country Meadows of Hershey. Looking forward to meeting you there.

BlogCFC was created by Raymond Camden. This blog is running version 5.003. Powered by ColdFusion Server v8,0,1,195765.