Nov
21
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 1One 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<!--- 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<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<!--- 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).

Phill Nacelli has been developing software for over 9 years, and have been using ColdFusion since version 4.5. He has engineered and developed multiple web based applications for the federal government, non-profit association/education market and enjoys playing with the latest in programming techniques, frameworks and development tools. He currently holds a position as Software Architect at




In Listing 4, the object is instantiated and the init method is called. If the object is a singleton, next time you want to use that object make sure you do not call the init method!
For Example: Listing 4 was part of a previous page that initializes registrant, next time I want to use it I simple do <cfset registrant = application.objectFactory.create("Registrant") /> and the same object will be returned.
Just like in using init with createObject the object will be reInitialized and you'll not have the desired results.
Nice article! Hoping this is first in a series leading someone up to using ColdSpring (or LightWire)?! Next issue is providing constructor arguments to each object from your config file. Then the one after that is injecting dependencies (UserService needs UserDAO) and the one that raises is constructing object dependency trees in the correct order while checking for circular constructor dependencies and distinguishing between setter and constructor injection!
If you want a cool, proven framework, ColdSpring is the king. If you want to see a working solution in under 300 lines of code so you can understand how it works, check out http://lightwire.riaforge.org!
I absolutely agree with you! The ObjectFactory implemented here is best used if you are not using ColdSpring or LightWire. Hence the fact that it's up to the user in this case to initialize the object, if I wanted my objects already initialized, I'd definately turn to ColdsSpring/LightWire since it already has the complexities built in for me to be able to define my objects complete with init args via the xml config file.
I'm currently working on a project that uses Model-Glue (v1.1) only and in this case without ColdSpring or LightWire, I ended up creating this simple ObjectFactory to handle this task.
Cheers...
I agree that ColdSpring and Lightwire make it possible to leave all this stuff behind. However, as I am learning OOP it is helpful to start here, doing things manually, so that when I move on to ColdSpring or Lightwire I know what the code is doing and I have a much firmer foundation.
-Aaron
Am I right in believing that when an object is registered with the ObjectFactory that it is not actually instantiated, only placed in a struct for quick reference when instantiated later?
And to instantiate the object later, you make a call to the create method of the objectFactory: application.objectFactory.create("Registrant")
-Aaron
That's correct, registering the objects just let's the ObjectFactory "learn" how to instantiate the object. Instantiating the object happens when you call the "create()" method.
Cheers..
This would allow objects to only be instantiated when needed, rather than say having to create them all by default in case you need them.
It sounds like what you are suggesting is placing the objectFactory in the application scope and not having to register the objects you plan to instantiate later. In this case, when you instantiate the object it would register and create it if it does not exist and if it did exist it would just pass values in by reference.
I think that is a wonderful idea! I really don't like having a huge block of code in Application.cfm to register all of my objects.
@Phil
Do you currently register every object for the entire application in the top most Application.cfm? I have been creating Application.cfm files in each directory and registering only those files that are common to that directory. I have a strong feeling this could become unwieldy, but if I put them all in the top level Application.cfm it would be huge... what do you suggest?
But one other method I have used where I wanted to do anything depending on the folder I am in, so that the application.cfm behaves differently, rather than having multiple application.cfm's, is to simply detect the current template path, and include a config file from that folder. You could use the same method I guess for registering files common to that directory.
Registering an object is simply a functionality of the objectFactory. How you implement it is up to you, as I mentioned earlier, one method is to have object's info on a xml or flat file and have ColdFusion simply read and register them.
Cheers...
Is a DAO considered a singleton in addition to the Gateway. I'm trying this factory out for the first time (finally).
Thanks - Justin
You are correct! DAO objects can also be singletons provided you pass the bean into the CRUD methods. Just a quick note also that with collaboration from Nic Tunney and Kelly Brown, we at AboutWeb have a newer more complete Object Factory, I plan on updating the documentation and re-releasing it.
All the best,
Thanks for the tip. I'll pretend I know what you are talking about!
You're a real CF developer ;)
Thanks for the great article, it helped me understand objectFactories.
Just one error, looks like the first cfargument tag in your revised ObjectFactory.cfc code has a hint attribute without any value which caused a null pointer error
<cfargument name="key" type="String" required="yes" hint />