Hmmm... well, with that exact suggestion, i get an infinite loop, but in any case i think i get what you're saying and have worked that out in my code awhile back - that i would need to pass an instance of parent into child if i'm employing composition. I don't know if it ever would be useful to mix them in this way - but in my case it was more my fumbling in the dark a few months ago trying to implement the template method pattern i read about in CFDJ.
Basically, what i needed to do, (by now i have it all working with compositon BTW ) is implement a common API via one parent obj to an assortment of child objs - so the child is being called from within the parent - and in practice i found that inheritance doesn't "work" there, even tho' the statement "Child is-a Parent" holds up very well in this particular case. So it was a confusing for me as a newbie to clearly have an Is-A relationship between them but still not be able to get it to work in the code as i thought it should. I'm still a little confused on the theoretical object model level, but the code itself is working well. Maybe this a case where a Manager of some sort would be useful. Maybe the call to getContent() - that's what it is - could go to the manager and the manager would decide which specific contentType (child) to call, instead of giving the job to the parent obj ContentItem. Then probably inheritance would work, as both parent and child would be instantitated within the manager ... In any case, i have something more to ponder today and more things are becoming clear! Thanks Nathan! -----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Behalf Of Nathan Dintenfass Sent: Monday, December 08, 2003 5:55 PM To: [EMAIL PROTECTED] Subject: [CFCDev] Inheritance and Instance Data, WAS: Difficulty in getting the light bulb to turn on... I'm not sure what is meant by the original statement "The instance data isn't inherited", but in the example shown below you need to run the init method on the child in order for the instance variables to be created in it because they are only created in the init method of the parent. Since you are creating a new instance of Child, it won't know anything about your instance of Parent in which it is being created. If you change the line: <cfset variables.theChild = createObject('component','Child')> To <cfset variables.theChild = createObject('component','Child').init()> You will get what you want. Why you would want to mix them in this way is another issue for another thread. > -----Original Message----- > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] > Behalf Of Nando > Sent: Monday, December 08, 2003 8:30 AM > To: [EMAIL PROTECTED] > Subject: RE: [CFCDev] Difficulty in getting the light bulb to turn on... > > > Jon, indeed you are correct with that example. I see i've been > trying to do something > slightly different, following, or maybe misfollowing, the > Template Method pattern > article in CFDJ - and that created part of the impression. > > <cfcomponent name="Parent" > > > <cffunction name="init" access="public" returntype="any"> > <cfset Variables.Name = "I'm an object in a family" > > <cfset variables.theChild = createObject('component','Child')> > <cfreturn this /> > </cffunction> > > <cffunction name="getChild" access="public" returntype="any"> > <cfreturn variables.theChild> > </cffunction> > </cfcomponent> > > <cfcomponent name="Child" extends="Parent"> > <cffunction name="GetName" > > <cfreturn Variables.Name > > </cffunction> > </cfcomponent> > > <cfset family = theParent.getChild().GetName() /> > > This gives me an "Element NAME is undefined in VARIABLES" error > on the GetName method. > So I see you can't "combine" inheritance and composition now in > this way. The child > element needs to be instantiated outside of the parent element, > probably in the same > variable space. I wasn't so clear about that until now. > > Thanks! That cleared something else up for me! > > :^) <sporting-bright-nosed-grin> > > -----Original Message----- > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] > Behalf Of Jon Gunnip > Sent: Monday, December 08, 2003 4:08 PM > To: [EMAIL PROTECTED] > Subject: RE: [CFCDev] Difficulty in getting the light bulb to turn on... > > > > > >>> [EMAIL PROTECTED] 12/08/03 03:04AM >>> > > Another is that i'm beginning to see that inheritance is > "nearly useless" in CF - > > well, that's a broad statement, but for the most part it's > kinda true, because all > > that is inherited is the methods. The instance data isn't > inherited, because it's > only > > class inheritance, not object inheritance. So if you need the > instance data in the > > subtypes, and you probably will, you still need to pass it down > to the subtype using > > ... you got it, composition. > > Component data (vars in the "Variables" scope) is inherited by > "child" components. I > am using it to good effect in a Unit Testing system that I work with. > > Inheritance isn't always "evil" or useless - you should just make > sure it is the best > solution for your problem rather than jumping into it without > much thought. There are > some situations where it provides a very elegant solution over > other options. > > <cfcomponent name="Parent" > > <cfset Variables.Name = "I'm an object in a family" > > </cfcomponent> > > <cfcomponent name="Child" extends="Parent" > > <cffunction name="GetName" > > <cfreturn Variables.Name > // returns "I'm an object in a family" > </cffunction> > </cfcomponent> > > Jon > > > > > ---------------------------------------------------------- > 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] > ---------------------------------------------------------- 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]
