>Any comments appreciated.

Why does the factory manager go back out of itself to application scope,
when it itself is already a singleton in application scope?

You could change to:

<!--- 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') />
                        <cfset Application.ComponentFactoryManager.init()>
                </cfif>
        </cflock>
</cfif>

In ComponentFactoryManager <<singleton>>

<cfcomponent displayname="ComponentFactoryManager">

        <cffunction name="init">
                <cfset variables.instance = structnew()>
                <cfset variables.instance.ComponentFactory = 
CreateObject('Component',
'Components.ComponentFactory') />
        </cffunction>

        <cffunction name="Instance" access="public" returntype="any" 
output="false"
 >
                <cfreturn variables.instance.ComponentFactory />
        </cffunction>

</cfcomponent>

-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
Behalf Of Peter H
Sent: 12 May 2005 16:39
To: CFCDev@cfczone.org
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: &quot;Nando&quot;
&lt;[EMAIL PROTECTED]&gt;<br>Reply-To: CFCDev@cfczone.org<br>To:
&lt;CFCDev@cfczone.org&gt;<br>Subject: RE: [CFCDev] Singleton / Factory
request<br>Date: Thu, 12 May 2005 15:51:42 +0200<br><br>&lt;cfif NOT
StructKeyExists(application,&quot;mySingleton&quot;)&gt;<br>  &lt;cflock
name=&quot;mySingletonLock&quot; Timeout=&quot;10&quot;
THROWONTIMEOUT=&quot;No&quot;<br>Type=&quot;Exclusive&quot;&gt;<br>
&lt;cfif NOT StructKeyExists(application,&quot;mySingleton&quot;)&gt;<br>
&lt;cfset application.mySingleton
=<br>CreateObject('component','Singleton').init(....)&gt;<br>
&lt;/cfif&gt;<br>  &lt;/cflock&gt;<br>&lt;/cfif&gt;<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: CFCDev@cfczone.org<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>cfcdev@cfczone.org
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/cfcdev@cfczone.org<br><br><br><br>---------------
-------------------------------------------<br>You
are subscribed to cfcdev. To unsubscribe, send an email to
cfcdev@cfczone.org 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/cfcdev@cfczone.org<br>




----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email to
cfcdev@cfczone.org 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/cfcdev@cfczone.org





----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email to 
cfcdev@cfczone.org 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/cfcdev@cfczone.org


Reply via email to