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 has 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. Phill is very active in the ColdFusion community via speaking at conferences, contributing articles, user group presentations, community forums/chat rooms and this blog. He currently holds a position as Senior 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 />
U.N. Security Council Condemns ‘Acts’ in Israeli Raid http://www.sf9456.com
BP Tries Again to Divert Oil Leak With Dome http://www.sf9458.com
Strike Is Said to Kill a Top Qaeda Leader Hurdles http://www.iqwsf.com
<a href="http://www.chaussureshub.com">tn requin</a>
<a href="http://http://www.69sneakers.com">nike shox r4</a>
<a href="http://www.shoxnzworld.com">nike shox running shoes</a>
<a href="http://http://www.lyle-scott-polo.com">lyle and scott wholesale</a>
<a href="http://http://www.tnchaussurecom.com">mbt walking shoes</a>
<a href="http://http://www.buy-ed-hardys.com">cheap ed hardy</a>
<br>
I love to buy <h1>[url=http://www.chicurling-iron.com/rare-styler-ghd-hai...]rare ghd iv styler[/url]</h1>, because it is nice.
<br>
We offer all <h1>[url=http://www.jordansbox.com/air-jordan-16-c-18.html]air jordan 16[/url]</h1>.So just trust <h1>[url=http://www.jordansbox.com/air-jordan-16-c-18.html]air jordan 16[/url]</h1> as you trust God.
<br>
The <h1>[url=http://www.adidasbox.com/adidas-top-ten-2000-black...]Top ten 2000[/url]</h1> is the most popular this year.
<br>
cheap <h1>[url=http://www.cheapjordans.org/ajf-20-c-71.html]Air Jordan Force shoes[/url]</h1> are on sale in 24/7 online store.
<br>
How can you find your favourite <h1>[url=http://www.ugg2003.com/ugg-boots-womens-mayfaire-c...]cheap ugg boots womens mayfaire[/url]</h1> online? Some people could not get the very <h1>[url=http://www.ugg2003.com/ugg-boots-womens-mayfaire-c...]cheap ugg boots womens mayfaire[/url]</h1> in the internet.
<br>
http://www.coachoutletmalls.com coach bags outlet
http://www.guccihandbagsoutlet.com gucci handbags outlet
http://www.guccihandbagsoutlet.com gucci outlet
http://www.gucci-shoes.cc gucci store
http://www.gucci-shoes.cc gucci outlet
http://www.gucci-shoes.cc gucci
http://www.gucci-shoes.cc gucci mane
http://www.gucci-shoes.cc gucci sneakers
http://www.gucci-shoes.cc gucci 2009
http://www.gucci-shoes.cc gucci 2010
http://www.gucci-shoes.cc gucci new arrival
http://www.gucci-shoes.cc cheap gucci shoes
http://www.gucci-shoes.cc buy gucci shoes
http://www.gucci-shoes.cc men gucci shoes
http://www.gucci-shoes.cc gucci men shoes
http://www.gucci-shoes.cc women gucci shoes
http://www.gucci-shoes.cc gucci women shoes
http://www.gucci-shoes.cc discount gucci shoes
http://www.gucci-shoes.cc gucci shoes man
http://www.gucci-shoes.cc buy gucci
http://www.adapterlist.com/dell/d830.htm Dell d830 Battery
http://www.adapterlist.com/dell/inspiron-9400.htm Dell inspiron 9400 Battery
http://www.adapterlist.com/dell/inspiron-9300.htm Dell inspiron 9300 Battery
http://www.adapterlist.com/dell/inspiron-e1705.htm... dell inspiron e1705 laptop battery
http://www.adapterlist.com/toshiba/pa3465u-1brs.ht... Toshiba pa3465u-1brs Battery
http://www.adapterlist.com/toshiba/pa3534u-1bas.ht... Toshiba pa3534u-1bas Battery
http://www.adapterlist.com/dell/inspiron-640m.htm Dell inspiron 640m Battery
http://www.adapterlist.com/toshiba/pa3534u-1brs.ht... Toshiba pa3534u-1brs Battery
http://www.adapterlist.com/toshiba/satellite-a205.... Toshiba satellite a205 Battery
http://www.batterylaptoppower.com/dell/inspiron-15... Dell inspiron 1520 Battery
http://www.batterylaptoppower.com/dell/inspiron-17... dell inspiron 1720 battery
http://www.batterylaptoppower.com/dell/vostro-1700... dell vostro 1700 battery
http://www.batterylaptoppower.com/hp/hstnn-ib33.ht... Hp hstnn-ib33 laptop battery
http://www.batterylaptoppower.com/hp/dv9500.htm Hp dv9500 Battery
http://www.batterylaptoppower.com/hp/dv9700.htm Hp dv9700 laptop battery
http://www.batterylaptoppower.com/hp/pavilion-dv97... Hp pavilion dv9700 laptop battery
http://www.batterylaptoppower.com/hp/dv9000.htm hp dv9000 battery
http://www.batterylaptoppower.com/dell/inspiron-10... Dell inspiron 1000 Battery
http://www.batterylaptoppower.com/dell/inspiron-12... Dell inspiron 1200 Battery
http://www.batterylaptoppower.com/dell/inspiron-22... Dell inspiron 2200 Battery
http://www.gucci-zone.com gucci outlet
http://www.gucci-zone.com gucci outlet online
http://www.gucci-zone.com gucci outlet store
<a href=" http://www.caphatshop.com" title="New Era Hats For Wholesale">New Era Hats For
Wholesale</a>
<a href=" http://www.caphatshop.com" title="New Era Caps For Wholesale">New Era Caps For
Wholesale</a>
<a href=" http://www.caphatshop.com" title="Monster Energy Hats For Wholesale">Monster Energy Hats
For Wholesale</a>
<a href=" http://www.caphatshop.com" title="Red Bull Hats For Wholesale">Red Bull Hats For
Wholesale</a>
http://www.buy-2moons-dil.com http://www.2moons-dil.org 2moons dil
<br>
<h1>[url=http://www.cheapairmax.net]nike air max cheap[/url]</h1> include allure and frolic. our family all like <h1>[url=http://www.cheapairmax.net]nike air max cheap[/url]</h1><br>
Cheap and High-quality <h1>[url=http://www.supra-skateshoes.com/supra-skateboard-d...]supra skateboard dancing black red[/url]</h1> platform.Free shipping.
<br>
<h1>[url=http://www.usaworldcupjersey.com/2010-world-cup-je...]2010 world cup jerseys south africa[/url]</h1> is fushion in this summer.
<br>
<h1>[url=http://www.supra-skateshoes.com/supra-chad-muska-s...]supra muska skytop blue black white[/url]</h1> is very good,we all like <h1>[url=http://www.supra-skateshoes.com/supra-chad-muska-s...]supra muska skytop blue black white[/url]</h1>
<br>
<h1>[url=http://www.worldcupapparelsale.com/shop-by-country...]Cameroon Soccer Jersey[/url]</h1> very depressed model so as well skillful certainty,we all like <h1>[url=http://www.worldcupapparelsale.com/shop-by-country...]Cameroon Soccer Jersey[/url]</h1>
<br>
http://www.coachoutletmalls.com/ coach bags
http://www.coachoutletmalls.com/ coach bags outlet
http://www.guccihandbagsoutlet.com/ gucci outlet
http://www.guccihandbagsoutlet.com/ gucci outlet online
http://www.guccihandbagsoutlet.com/ gucci handbags
<a href="http://www.ugg-shoes.us/" title="UGGS shoes">UGGS Shoes</a>
<a href="http://www.ugg-classic.biz/" title="ugg classic boots">ugg classic boots</a>
<a href="http://www.cheap-uggoutlet.com/" title="uggs for cheap">uggs for cheap</a>
<a href="http://www.uggclassic.us/" title="UGG cardy">ugg cardy</a>
As you know all Boots Bailey Button, are now very popular. These sheepskin boots are sold cheaper amounts hook means you can own an institution. I added a photo of Alexander in my article and photo with boots. Check also style boots in the article. Boots read article.
<a href="http://www.paul-smithes.com/" title="Paul smith Belts">Paul Smith Belts</a>
<a href="http://www.chi-sale.biz/" title="Flat Iron Chi">Flat Iron Chi</a>
<a href="http://www.mbt-shoes-mbt.com/" title="discount mbt shoes">discount mbt shoes</a>
http://www.ed-hardy.cc edhardy
http://www.ed-hardy.cc ed hardy outlet
http://www.cheap-mbt-shoes.net mbt sale
http://www.coachoutletfactory.com coach factory outlet
http://www.coachoutletfactory.com coach outlet
http://www.gucci-outlet.us/ gucci outlet
http://www.coachoutletfactory.com coach outlet online
http://www.coachoutletfactory.com coach factory outlet online
http://www.coachoutletfactory.com coach outlet store online
http://www.coachoutletfactory.com coach outlet online store
http://www.coachoutletfactory.com coach factory outlet sale
http://www.gucci-outlet.us/ gucci outlet
http://www.gucci-outlet.us/ gucci bags
http://www.gucci-outlet.us/ gucci outlet online
http://www.gucci-outlet.us/ gucci bags outlet
http://www.coach-handbags-outlet.com coach bags outlet
http://www.coach-handbags-outlet.com coach handbags on sale
http://www.mbtshoesaleonline.com mbt outlet
http://www.mbtshoesaleonline.com mbt shoes
http://www.mbtshoesaleonline.com mbt shoes online
www.efox-shop.com
<a href="http://www.efox-shop.com"> chinahandy</a>
<a href="http://www.efox-shop.com"> dual sim handy</a>
<a href="http://www.efox-shop.com"> touchscreen handy</a>
<a href="http://www.efox-shop.com"> Chinesische Handys</a>
<a href="http://www.efox-shop.com"> chinesische handymarken</a>
<a href="http://www.efox-shop.com">china handy hersteller </a>
<a href="http://www.efox-shop.com"> Lesegerät </a>
[url=http://www.jordansbox.com]cheap jordans[/url]
[url=http://www.shop-supra.com]supra footwear[/url]
[url=http://www.mbt4life.com/]mbt shoe[/url]
[url=http://www.mbt4life.com/mbt-mwalk-grey-womens-shoes-p-800.html" target="_blank">http://www.mbt4life.com/mbt-mwalk-grey-womens-shoe...]mbt walking shoes sale[/url]
[url=http://www.shoestimeberland.com/custom-boots-c-46....]Custom Boots[/url]
<p><a href=" http://www.autocarplaza.com/">bmw stereo nav Tucson</a> only plays AVI video files. Any <a href=" http://www.autocarplaza.com/">e-fun system car dvd player</a> required by <a href=" http://www.autocarplaza.com/">bmw dvd nav player</a> AVI file must be installed before<a href=" http://www.autocarplaza.com/"> pathfinder dvd gps player</a> play car dvd player video. <a href=" http://www.autocarplaza.com/">bmw 3 series touch screen</a> basic Windows multimedia support for playing AVI files must be installed.<a href=" http://www.autocarplaza.com/"> kia navigaties</a> does not setup any shell associations to become <a href=" http://www.autocarplaza.com/">2003 toyota camry navigation system</a> default AVI player. However, this could be setup by an install program that you provide or by <a href=" http://www.autocarplaza.com/">7" dvd/nav</a> end user.</p>
<p><a href=" http://www.autocarplaza.com/">e53 dvd gps</a> always plays back videos at 100% of <a href=" http://www.autocarplaza.com/">bmw dvdnav e39</a> original size so that <a href=" http://www.autocarplaza.com/">bmw touch screen navigation</a> remain readable. <a href=" http://www.autocarplaza.com/">dvd systems to fit bmw</a> Player also has <a href=" http://www.autocarplaza.com/">e46 hd radio</a> r TechSmith Screen Capture Codec (TSCC) built into it, so you don't have to worry if your users have installed or not.</p>