I am just playing with the singleton design pattern, and this is how I
imagine it implemented in ColdFusion, I am just looking after some feedback
on this, and most likely improvements. And maybe I am just way off!!?
If needed I can provide some more info on what's going on.
********* Object.cfc
<cfcomponent
displayname="Object"
hint=""
output="false"
author="Taco Fleur ([EMAIL PROTECTED])">
<!---
Define: Is A
Define: Has A
--->
<!--- Declare properties for this Object --->
<!--- Create instance variables --->
<cfscript>
//
</cfscript>
<cffunction
name="init"
access="public"
returntype="Object">
<cfscript>
// Return the instance of the Object
return getInstance();
</cfscript>
</cffunction>
<cffunction
access="private"
name="isSingleton"
output="false"
returntype="boolean"
displayname="Is Singleton"
hint="Returns whether this object is a singleton">
<cfreturn false />
</cffunction>
<cffunction
access="private"
name="setSingleton"
output="false"
returntype="void"
displayname="set Singleton"
hint="">
<cfscript>
evaluate( getSingletonScope() & "[ getMetaData( this
).name ] = this" );
</cfscript>
</cffunction>
<cffunction
access="private"
name="getSingleton"
output="false"
returntype="struct"
displayname="Get Singleton"
hint="">
<cfscript>
return evaluate( getSingletonScope() & "[
getMetaData( this ).name ]" );
</cfscript>
</cffunction>
<cffunction
access="private"
name="getInstance"
output="false"
returntype="struct"
displayname="Get Instance"
hint="">
<cfscript>
// Check if this object is a singleton
if ( isSingleton() )
{
// Check if the singleton is
instantiated
if ( isInstantiatedSingleton() )
{
// Get the singleton
this = getSingleton();
}
// Otherwise instantiate the singleton
else
{
setSingleton();
}
}
return this;
</cfscript>
</cffunction>
<cffunction
access="private"
name="getSingletonScope"
output="false"
returntype="string"
displayname="Get Singleton Scope"
hint="Returns the scope this singleton should be
placed in">
<cfreturn "variables" />
</cffunction>
<cffunction
access="private"
name="isInstantiatedSingleton"
output="false"
returntype="boolean"
displayname="Is Instantiated Singleton"
hint="">
<cfscript>
return structKeyExists( evaluate(
getSingletonScope() ), getMetaData( this ).name );
</cfscript>
</cffunction>
</cfcomponent>
********* Child.cfc
<cfcomponent
extends="Object"
displayname="Child"
hint=""
output="false"
author="Taco Fleur ([EMAIL PROTECTED])">
<!---
Define: Is A
Define: Has A
--->
<!--- Declare properties for this Object --->
<!--- Create instance variables --->
<cfscript>
//
this.instance.test = "";
</cfscript>
<cffunction
name="init"
access="public"
returntype="Child">
<cfscript>
super.init();
// Return the instance of the Object
return getInstance();
</cfscript>
</cffunction>
<cffunction
access="private"
name="isSingleton"
output="false"
returntype="boolean"
displayname="Is Singleton"
hint="Returns whether this object is a singleton">
<cfreturn true />
</cffunction>
<cffunction
access="private"
name="getSingletonScope"
output="false"
returntype="string"
displayname="Get Singleton Scope"
hint="Returns the scope this singleton should be
placed in">
<cfreturn "server" />
</cffunction>
<cffunction
access="public"
name="test"
output="false"
returntype="void"
displayname="Test"
hint="">
<cfargument
name="test"
type="string" />
<cfscript>
this.instance.test = arguments.test;
</cfscript>
</cffunction>
</cfcomponent>
********* test.cfm
<cfscript>
//structDelete( server, "Child" );
obj = createObject( "component", "child" ).init();
// Uncomment the following line the first time this object is instantiated
//obj.test( "testing" );
</cfscript>
<cfdump var="#obj#">
<cfdump var="#server#">
--
Taco Fleur
Senior Web Systems Engineer
http://www.webassociates.com <http://www.webassociates.com/>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Special thanks to the CF Community Suite Silver Sponsor - RUWebby
http://www.ruwebby.com
Message: http://www.houseoffusion.com/lists.cfm/link=i:4:188596
Archives: http://www.houseoffusion.com/cf_lists/threads.cfm/4
Subscription: http://www.houseoffusion.com/lists.cfm/link=s:4
Unsubscribe:
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.4
Donations & Support: http://www.houseoffusion.com/tiny.cfm/54