James

You don't necessarily need to return an object, an alternative is to use
composition as a form of encapsulation:

Example, you have a cfc responsible for validation

<cfcomponent output="no" hint="The central component">

  <cfset variables.oObject1 = "" >
  <cfset variables.oObject2 = "" >

  <cffunction name="init" output="false" returntype="void" access="public"
hint="The method to instantiate a new object">
    <cfset variables.oObject1 = CreateObject("component","Component1")>
    <cfset variables.oObject2 = CreateObject("component","Component2")>
  </cffunction>

  <cffunction name="doSomething" >
        <cfargument name= "personname" >
        <cfargument name="personAge" >
        <cfset rs = variables.oObject1.doSomethingElse(personName,PersonAge)
>
    <cfreturn rs >
  </cffunction>
 
In the above example you can swap out the functionality by changing the
object held in variables.oObject1 but keep the interface for your cfc the
same (i.e. you do not need to change the arguments passed to doSomething
Even though the object you pass the arguments to internally has changed.

  <cffunction name="doSomething" >
        <cfargument name= "personname" >
        <cfargument name="personAge" >
        <cfset rs = variables.NewObject.doSomethingNew(personName,PersonAge)
>
    <cfreturn rs >
  </cffunction>


HTH

Kola  



> -----Original Message-----
> From: James Holmes [mailto:[EMAIL PROTECTED]
> Sent: 16 March 2005 11:54
> To: CF-Talk
> Subject: Object composition and calling objects
> 
> I'd like to check on some of the reading I've been doing.
> 
> I am starting to use composition in my CFCs (because I only use
> inheritance
> for an "is-a" relationship). I want to reuse the objects I've instantiated
> in the central object from the outside; the reading I've done suggests the
> best way to do this is to use a getter method that returns the
> instantiated
> object.
> 
> E.g. I have some sort of central component
> 
> <cfcomponent output="no" hint="The central component">
> 
>   <cffunction name="init" output="false" returntype="void" access="public"
> hint="The method to instantiate a new object">
>     <cfset oObject1 = CreateObject("component","Component1")>
>     <cfset oObject2 = CreateObject("component","Component2")>
>   </cffunction>
> 
>   <cffunction name="getObject1">
>     <cfreturn _oObject1>
>   </cffunction>
> 
>   <cffunction name="getObject2">
>     <cfreturn _oObject2>
>   </cffunction>
> 
>    ...etc etc
> 
> </cfcomponent>
> 
> I could then, in the CF page that uses the central component, do this:
> 
> <cfset oMyCentralObject =
> CreateObject("component","CentralComponent").init()>
> <!--- now call a method1 in object1--->
> <cfset MyMethodReturn = oMyCentralObject.getObject1().Method1()
> 
> This is how some articles say it should be done - any feedback?
> 
> 
> 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Discover CFTicket - The leading ColdFusion Help Desk and Trouble 
Ticket application

http://www.houseoffusion.com/banners/view.cfm?bannerid=48

Message: http://www.houseoffusion.com/lists.cfm/link=i:4:198929
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

Reply via email to