You are correct that there are two steps involved--that is there are two
explicit steps that the programmer must complete.  In other OO languages
these two steps also exist: instanciation and initialization.  The only
difference is that they abstract it into on operation ("new" in Java or
C++).  If you are really interested in reducing these two steps into one
function call, then what you need to implement is a FACTORY method (or
class).  Here is what I mean:

   <cfcomponent name="personFactory">

      <cffunction name="newPerson" returntype="cfc.Person" access="public"
output="false">
         <cfargument name="firstname" type="string" required="true"/>
         <cfargument name="lastname" type="string" required="true"/>

         <cfset var person = CreateObject("component", "cfc.Person")>
         <cfset var person.init(arguments.firstname, arguments.lastname)>

         <cfreturn person/>
      </cffunction>

      ... Other methods ...

   </cfcomponent>


When you need a new Person object, simply call this code:


   <cfset person = factory.newPerson("John", "Doe")>


You might already do this, but it is common practice in other OO languages
that implicitly combine the two steps (Java, C++, ...) for a factory method
(or class) to be used to further abstract object creation from the rest of
the code.  In general it is a better practice since it isolates the details
of initanciation to one section of code, and you can do this in just the
same way using CF.




Paul Kenney
WebMaster, CorporateWarriors.com
916-630-4545


-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf
Of Jeffry Houser
Sent: Friday, December 12, 2003 5:30 AM
To: [EMAIL PROTECTED]
Subject: Re: RE: [CFCDev] Creating an Instance of a component



  In this approach, wouldn't you be making two calls instead of one?  First 
you create the object and then you call init (although it is done on one 
line).

  This seems the same as "cfobject" and then "cfinvoke".  In CFScript there 
is no way (that I know of) to invoke a component method w/o already having 
the object.

At 07:08 PM 12/11/2003, you wrote:
>To Paul's point:
>
><cfset variables.person = createObject( "component",
>"cfc.Person" ).init( "John", "Doe" ) />
>
><cfdump var="#variables.person.getName( )#"/" />
>
>
>regards,
>Rob
>
>
>
>
>
>---- On Thu, 11 Dec 2003, Paul Kenney
>([EMAIL PROTECTED]) wrote:
>
> > You could do your instanciation and initialization in one step
>using
> > CFINVOKE.  Here is some code to show how:
> >
> > Client Code:
> >       <cfinvoke component="cfc.Person" method="init"
> > returnvariable="variables.person">
> >               <cfinvokeargument name="firstname" value="John"/>
> >               <cfinvokeargument name="lastname" value="Doe"/>
> >       </cfinvoke>
> >
> >       <cfdump var="#variables.person.getName()#"/>
> >
> >
> > Person component:
> >       <cfcomponent display="cfc.Person" output="false">
> >               <cffunction name="init" returntype="cfc.Person"
> > access="public" output="false">
> >                       <cfargument name="firstname" type="string"
> > required="true"/>
> >                       <cfargument name="lastname" type="string"
> > required="true"/>
> >
> >                       <cfset variables.fFirstname = arguments.firstname>
> >                       <cfset variables.fLastname = arguments.lastname>
> >
> >                       <cfreturn this/>
> >               </cffunction>
> >
> >               <cffunction name="getName" returntype="string"
> > access="public" output="false">
> >                       <cfreturn variables.fFirstname & " " &
> > variables.fLastname/>
> >               </cffunction>
> >
> >               ...
> >       </cfcomponent>
> >
> > I hope this helps some.
> >
> >
> > Paul Kenney
> > WebMaster, CorporateWarriors.com
> > 916-630-4545
> >
> >
> > -----Original Message-----
> > From: [EMAIL PROTECTED]
>[mailto:[EMAIL PROTECTED] On Behalf
> > Of Nathan Dintenfass
> > Sent: Thursday, December 11, 2003 2:56 PM
> > To: [EMAIL PROTECTED]
> > Subject: RE: [CFCDev] Creating an Instance of a component
> >
> >
> > "THIS" is a reference to the public scope of the instance, so
>you aren't
> > actually creating a new instance in memory -- just passing a
>reference to
> > the public scope of the existing instance.
> >
> > In your example, though, you are not using CFINVOKE on the
>same instance
> > created by your CFOBJECT -- so you end up with two different
>instances of
> > your Stack CFC because your CFINVOKE is creating its own
>instance of Stack.
> > If you pass your instance created with CFOBJECT into your
>CFINVOKE you'd
> > find that MyStackInvoked and MyStackObject are actually the
>same instance.
> >
> >
> >
> >
> > > -----Original Message-----
> > > From: [EMAIL PROTECTED]
>[mailto:[EMAIL PROTECTED]
> > > Behalf Of Jeffry Houser
> > > Sent: Thursday, December 11, 2003 1:33 PM
> > > To: cfcdev
> > > Subject: [CFCDev] Creating an Instance of a component
> > >
> > >
> > >
> > > Hi,
> > >
> > >   The "standard" way I would use to create a component
>instance is use
> > > cfobject.  And then I'd use cfinvoke to call various methods
>of the
> > > component.  Often the first cfinvoke is an init type of
>method.
> > >
> > >   If create an init method that will returns this, it seems
>to create a
> > > component instance in memory the same way that cfobject
>would.  Is this
> > > what actually happens?
> > >
> > >   I attached a test component and the file I was using to
>test this.  The
> > > tickcount wasn't much help since they were zero most of the
>time.
> > >  The dump
> > > seemed to randomly change the order of methods in the
>component.
> > >
> > >
> >
> > ----------------------------------------------------------
> > You are subscribed to cfcdev. To unsubscribe, send an email
> > to [EMAIL PROTECTED] with the words 'unsubscribe cfcdev'
> > in the message of the email.
> >
> > CFCDev is run by CFCZone (www.cfczone.org) and supported
> > by Mindtool, Corporation (www.mindtool.com).
> >
> > An archive of the CFCDev list is available at
> > www.mail-archive.com/[EMAIL PROTECTED]
> >
> >
> >
> > ----------------------------------------------------------
> > You are subscribed to cfcdev. To unsubscribe, send an email
> > to [EMAIL PROTECTED] with the words 'unsubscribe cfcdev'
> > in the message of the email.
> >
> > CFCDev is run by CFCZone (www.cfczone.org) and supported
> > by Mindtool, Corporation (www.mindtool.com).
> >
> > An archive of the CFCDev list is available at
>www.mail-archive.com/[EMAIL PROTECTED]
> >
> >
>
>
>________________________________________________
>Get your own "800" number
>Voicemail, fax, email, and a lot more
>http://www.ureach.com/reg/tag
>----------------------------------------------------------
>You are subscribed to cfcdev. To unsubscribe, send an email
>to [EMAIL PROTECTED] with the words 'unsubscribe cfcdev'
>in the message of the email.
>
>CFCDev is run by CFCZone (www.cfczone.org) and supported
>by Mindtool, Corporation (www.mindtool.com).
>
>An archive of the CFCDev list is available at 
>www.mail-archive.com/[EMAIL PROTECTED]

--
Jeffry Houser, Web Developer <mailto:[EMAIL PROTECTED]>
Aaron Skye, Guitarist / Songwriter <mailto:[EMAIL PROTECTED]>
--
AIM: Reboog711  | Phone: 1-203-379-0773
--
My Books: <http://www.instantcoldfusion.com>
Recording Music: <http://www.fcfstudios.com>
Original Energetic Acoustic Rock: <http://www.farcryfly.com> 


----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email
to [EMAIL PROTECTED] with the words 'unsubscribe cfcdev' 
in the message of the email.

CFCDev is run by CFCZone (www.cfczone.org) and supported
by Mindtool, Corporation (www.mindtool.com).

An archive of the CFCDev list is available at
www.mail-archive.com/[EMAIL PROTECTED]



----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email
to [EMAIL PROTECTED] with the words 'unsubscribe cfcdev'
in the message of the email.

CFCDev is run by CFCZone (www.cfczone.org) and supported
by Mindtool, Corporation (www.mindtool.com).

An archive of the CFCDev list is available at www.mail-archive.com/[EMAIL PROTECTED]

Reply via email to