>Also, what if CFCs need to create objects, how do they call the Factory if
>its in the app scope?
:)
Isn't this stuff great to think about?
In the previous example, let's say your ServiceObj needed Factory to
createObject()'s for it.
<cffunction name="makeServiceObj" access="private" returntype="ServiceObj"
output="no">
<cfset var ServiceObj =
createObject('component','ServiceObj').init(variables.Utils, this) />
<cfreturn ServiceObj />
</cffunction>
That's all you'd need to do! Well, almost ... Did you notice the difference?
:)
Then in the init() of ServiceObj, all you'd need is this:
<cfset variables.Factory = arguments.Factory />
Of course, once you've got a reference to Factory in your ServiceObj, then
you wouldn't need necessarily to pass Utils into it. You could just ask
Factory for a instance:
In Factory:
<cffunction name="makeServiceObj" access="private" returntype="ServiceObj"
output="no">
<cfset var ServiceObj =
createObject('component','ServiceObj').init(this)
/>
<cfreturn ServiceObj />
</cffunction>
Then in the init() of ServiceObj, all you'd need is this:
<cfset variables.Factory = arguments.Factory />
<cfset variables.Utils = variables.Factory.load('Utils') />
None of these examples are "perfect" ... it all really depends on the
context of your application and your skill and preferences as a developer.
The point is that we're headed down a different path, using some OO
principles to create a more robust, flexible app. What just happened was a
requirements change. You suddenly needed the capability to createObject()
within some ServiceObj. And you got it with no change to Factory's public
interface, load(), and a very minor change to makeServiceObj() and
ServiceObj.init()
If you find yourself battling with "scopes" - you haven't come up with the
right architecture yet!
----------------------------------------------------------
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]