Thank you to Bill Rawlinson and Sean Corfield who pointed me at some excellent articles about factory patterns.  Now I think I understand a lot more about how that stuff works, and how objects are passed around.
 
I read Joe Rinehart's excellent article which is excellent from my point of view, because it is  entertainingly written, not too technical in its language, and I found it easy to understand.  I was able to use the technique as he described it immediately, with excellent results (see below).  I am most grateful to Bill, Sean and Joe for their continuing efforts to help us get the hang of this OO stuff.
 
Here's what I did last night, and you'll see I had spectacular results ....
 
I have a low-traffic but live site, where I've been using bean CFCs and DAOs to manipulate the data.  The admin area is built with a series of these things.   But until last night, I loaded an instance of each DAO on each page call, by having them created in the Application.cfm file, with a series of these lines:
 
events = CreateObject("component", "#request.appmapping#.cfcs.eventsmanager").init(request.dsn,session.auth.UserLogin);
 
Each page request, I was creating a dozen of these things.   I knew that was probably not the best way to do it, and while i didnt have the word "factory" for it, I knew i had to create a mechanism to instantiate only those objects that were going to be required for this page request.   However it is a low-traffic site and it was working, so I felt I could happily live with it as is for the mean time, while I learned more about how to do it.
 
Now, in my Application.cfm, I have a single object created: 
 
DAOFactory =  createobject("component", "#request.appmapping#.cfcs.DAOfactory").init(request.dsn,userlogin);
 
The DAOFactory follows the outline in Joe Rineharts article, and each page request calls on the DAOFactory to create the objects needed to fulfil this request.   At the moment the DAOFactory is created each page request, but I"ll move it into the application scope when the site is more stable, so that I dont have to keep creating the Application scope every time I have a change.
 
However here are some of the results of taking this approach:
PRIOR:
Home page created in 141ms (figs from the CFMX debugging display)
Pagex created in 125ms
Pagey created in 156ms
Admin Home page 984ms.
 
AFTER:
 
Home page created in 63ms
Pagex created in 31ms
Pagey created in 34ms
Admin page created in 94ms.
 
While I know these figures dont tell much in themselves because there is the time cost in having debugging displayed too,  there is certainly a dramatic reduction in the overhead through not instantiating CFCs that were not required for this request.
 
So I think this proves the factory pattern pays for itself. Yes?
 

Cheers
Mike Kear
Windsor, NSW, Australia
Certified Advanced ColdFusion Developer
AFP Webworks
http://afpwebworks.com
ColdFusion, PHP, ASP, ASP.NET hosting from AUD$15/month
 
----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email to [email protected] with the words 'unsubscribe cfcdev' as the subject of the email.

CFCDev is run by CFCZone (www.cfczone.org) and supported by CFXHosting (www.cfxhosting.com).

An archive of the CFCDev list is available at www.mail-archive.com/[email protected]

Reply via email to