So, rather than create your own object yourself like so:
<cfset myObject = createobject("component",model.myObject)>
You would call upon your Factory object to make it for you, similar to the following:
If you opt to have an object create your other objects for you, thenYOU, O Best Beloved, are a user of the Factory Pattern, and may boast of it in public settings as the mood strikes you.
I’ll have to respond to that question with another question: Has it crossed your mind that sometimes, perhaps, one object might need an instance of another object inside of itself in order to do some kind of work?
Whether it has or hasn’t crossed your mind, the fact is that many times this will be the case, especially when you’re writing your app around an MVC framework. Now, the traditional method of making one object available within another object would be to simply write a line of code within your CFC that instantiates the other object, such as in our example above. However, what if you are using this same CFC…we’ll say, a CFC that performs emailing duties, within many other objects? Then one day you make a change to the Email.sendMail() method that requires an additional parameter be passed in. You would have to go to every other CFC that instantiates the Email.CFC and modify that line of code to accommodate the new parameter. Could take a long time, you might miss one, etc. Using a Factory, however (such as the Coldspring framework, which I am totally in love with
), you can make that change in one place and the Factory will ensure the change is cascaded appropriately.
So what would be a real world scenario where I would actually need one object inside of another? Hmmm…how about registering a new user for access to a site? The steps involved with registering a new user are:
- create the user record
- send the user an email.
In order to keep my functionality all segregated nice and neat so i can use it here and there, I have myself a User object that handles user records, and an Email object that performs emailing duties. Since my registration process means I need to be able to perform functionality from both the User object AND the email object, I create a third object called RegistrationService.CFC that will do nothing more than orchestrate, or coordinate, work that the User and Email objects know how to do. This scenario is just one of many compelling reasons to utilize a factory.
One last thought...Factory Pattern…this is another one of those “patterns” that I do not consider to be a pattern of any sort. From my real world experience, an object can BE a Factory, or you could even create an entire application (or framework) whose job it is to BE a Factory…but a pattern of factories? Nah, doesn’t gel.
Doug out.
You are not logged in, so your subscription status for this entry is unknown. You can login or register here.
Thus, due to ColdFusion not having the concept of a class path for CFCs (is that true?), the Factory Pattern also becomes very handy for storing your CFCs in well structure packages when you do not have access to the ColdFusion root or CF Admin.
Example:
/www/WEB-INF/comp/factory.cfc
/www/WEB-INF/comp/com/dougboude/testDAO.cfc
/www/WEB-INF/comp/net/somewhere/anothertestDAO.cfc
You then only need to hardcode the path to the factory through a mapping, then in the factory you can refer to the objects in the more standard of naming packages of:
-- factory.cfc --
var test = createObject("component", "com.dougboude.testDAO");
var test2 = createObject("component", "net.somewhere.anothertestDAO");
Now something I don't know is if this works when using return types ;)


