ColdFusion Singletons Revisited

My last article on singletons got a few comments on blogs and in email including how it could be improved (thanks Michael) and a few questions. I also omitted the code showing how the singletons were created and called (now added).

So here's the new improved code!

[More]

ColdFusion Singletons

Singletons are perhaps one of the most simple Design Patterns. For those who don't know sigletons are a class that can only have one instance. They can be thought of as a glorified global variable - but are a lot more useful.

Most ColdFusion classes, or rather instances of CF components, can be turned in a singleton by placing the following code in your Application.cfm:

view plain print about
1<cfif not structkeyexists(application,<instance name>)>
2        <cfset application.<instance name> = createobject("component",<path to component>)>
3    </cfif>

or OnApplicationStart method of your Application.cfc:

view plain print about
1<cfset application.<instance name> = createobject("component",<path to component>)>

[More]