Pete, If you're instantiating your ComponentFactoryManager from Application.cfc in the onApplicationStart method, you don't need the fancy cfif StructKeyExists / cflock stuff. You can just instantiate the object, Application.cfc will take care that only one is created.
And if all your ComponentFactoryManager does is instantiate a ComponentFactory into Application scope, then you probably don't need a manager to do that. Just instantiate ComponentFactory in Application.cfc and be done with it. I think the smart thing to do at this point is to keep your object model as simple as possible. In your factory, i'd consider creating separate methods for each object you need. Why? Well, because that Create method isn't very flexible. There will come a time in the not too distant future when you need to pass something into the init() of the object you wanna create, hmmm? And then you'll need to create a separate factory method for that object, or fudge the one you've got. And that will begin to ripple out into your app ... some places you use the create method and pass in the component type, some places you use createSomeObject, and pass in other arguments ... and you start to think that it might be better to do it consistently ... Something to think about at least. :) n. -----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Behalf Of Peter H Sent: Thursday, May 12, 2005 5:39 PM To: [email protected] Subject: RE: [CFCDev] Singleton / Factory request Thanks Nando, I've ended up seperating the Singleton from the factory. Not sure if its the best way though. Any comments appreciated. Cheers, Pete (aka lad4bear) In Application.cfc <!--- Instantiate ComponentManager ---> <cfif not StructKeyExists(Application, 'ComponentFactoryManager') > <cflock name="LoadComponentFactoryManager" timeout="10" type="exclusive"> <cfif not structKeyExists( Application, 'ComponentFactoryManager' )> <cfset Application.ComponentFactoryManager = CreateObject('Component', 'Components.ComponentFactoryManager') /> </cfif> </cflock> </cfif> In ComponentFactoryManager <<singleton>> <cfcomponent displayname="ComponentFactoryManager"> <cffunction name="Instance" access="public" returntype="any" output="false" > <cfif not StructKeyExists(Application, 'ComponentFactory') > <cflock name="LoadComponentFactory" timeout="10" type="exclusive"> <cfif not structKeyExists( Application, 'ComponentFactory' )> <cfset Application.ComponentFactory = CreateObject('Component', 'Components.ComponentFactory') /> </cfif> </cflock> </cfif> <cfreturn Application.ComponentFactory /> </cffunction> </cfcomponent> In ComponentFactory <<factory >> <cfcomponent displayname="ComponentFactory" hint=""> <cffunction name="Create" access="public" returntype="any" output="false" > <cfargument name="ComponentType" type="string" required="true"> <cfset var component = 0 /> <cfswitch expression="#arguments.ComponentType#"> <cfcase value="PingRequest"> <cfset component = CreateObject('Component', 'Components.PingRequest').Init() /> </cfcase> <cfcase value="OptionRequest"> <cfset component = CreateObject('Component', 'Components.OptionRequest').Init() /> </cfcase> </cfswitch> <cfreturn component /> </cffunction> </cfcomponent> <br><br><br>----Original Message Follows----<br>From: "Nando" <[EMAIL PROTECTED]><br>Reply-To: [email protected]<br>To: <[email protected]><br>Subject: RE: [CFCDev] Singleton / Factory request<br>Date: Thu, 12 May 2005 15:51:42 +0200<br><br><cfif NOT StructKeyExists(application,"mySingleton")><br> <cflock name="mySingletonLock" Timeout="10" THROWONTIMEOUT="No"<br>Type="Exclusive"><br> <cfif NOT StructKeyExists(application,"mySingleton")><br> <cfset application.mySingleton =<br>CreateObject('component','Singleton').init(....)><br> </cfif><br> </cflock><br></cfif><br><br>Does that help? The lock is necessary to be absolutely sure, because there's<br>a very small time lag between the moment a request passes thru the first<br>cfif and the moment application.mySingleton is created.<br><br>n. :)<br> -----Original Message-----<br> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Behalf<br>Of Peter H<br> Sent: Thursday, May 12, 2005 3:16 PM<br> To: [email protected]<br> Subject: [CFCDev] Singleton / Factory request<br><br><br> Hi guys,<br><br> I'm trying to build a Singleton / Factory but seemed to have got myself a<br>little tangled up. Here's what I'm trying to do.<br><br> 1) Have a factory component that is responsible for creating all of my<br>other components by name.<br><br> 2) Make this factory a singleton so that it's only created once and stored<br>in application scope.<br><br> I've heard people talk about this on the board and was hoping someone<br>would post be able to post some code for me to look at.<br><br> Cheers, Pete (aka lad4bear)<br> ----------------------------------------------------------<br> You are subscribed to cfcdev. To unsubscribe, send an email to<br>[email protected] with the words 'unsubscribe cfcdev' as the subject of the<br>email.<br><br> CFCDev is run by CFCZone (www.cfczone.org) and supported by CFXHosting<br>(www.cfxhosting.com).<br><br> CFCDev is supported by New Atlanta, makers of BlueDragon<br> http://www.newatlanta.com/products/bluedragon/index.cfm<br><br> An archive of the CFCDev list is available at<br>www.mail-archive.com/[email protected]<br><br><br><br>--------------- -------------------------------------------<br>You are subscribed to cfcdev. To unsubscribe, send an email to [email protected] with the words 'unsubscribe cfcdev' as the subject of the email.<br><br>CFCDev is run by CFCZone (www.cfczone.org) and supported by CFXHosting (www.cfxhosting.com).<br><br>CFCDev is supported by New Atlanta, makers of BlueDragon<br>http://www.newatlanta.com/products/bluedragon/index.cfm<br><br >An archive of the CFCDev list is available at www.mail-archive.com/[email protected]<br> ---------------------------------------------------------- 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). CFCDev is supported by New Atlanta, makers of BlueDragon http://www.newatlanta.com/products/bluedragon/index.cfm An archive of the CFCDev list is available at www.mail-archive.com/[email protected] ---------------------------------------------------------- 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). CFCDev is supported by New Atlanta, makers of BlueDragon http://www.newatlanta.com/products/bluedragon/index.cfm An archive of the CFCDev list is available at www.mail-archive.com/[email protected]
