Now I've got a component called "Meal", with an eat() method. Two components called breakfast and lunch inherit from meal.cfc. In the test .cfm page, I create a structure called food that has name and diet. If the meal is breakfast and the diet isn't Atkins, the eat() function returns "Phtwooowey". If the meal is lunch and the diet isn't Atkins, the eat() function doesn't care.
<!--- dspears breakfast ---> <cfcomponent extends="meal" hint="first meal of the day">
<cffunction name="eat" access="public" returntype="string">
<cfargument name="food" type="any" required="true">
<cfif isdefined("food.diet")> <cfif food.diet EQ "Atkins"> <cfset review = "Yum Yum."> <cfelse> <cfset review = "Phtwooowey."> </cfif> <cfelse> <cfset review = "What is this??"> </cfif>
<cfreturn review> </cffunction>
</cfcomponent>
<!--- dspears lunch ---> <cfcomponent extends="meal" hint="mid-day meal">
<cffunction name="eat" access="public" returntype="string">
<cfargument name="food" type="any" required="true">
<cfset review = "Mmmmmmmmmmm.">
<cfreturn review> </cffunction>
</cfcomponent>
<!--- dspears create food ---> <cfset food.name = "Green grapes"> <cfset food.diet = "Supermodel">
<!--- dspears show reaction ---> <cfset todaysMeal = createobject("component","breakfast")> <cfset reaction = todaysMeal.eat("#food#")> <cfoutput>#reaction#</cfoutput>
<cfset todaysMeal = createobject("component","lunch")> <cfset reaction = todaysMeal.eat("#food#")> <cfoutput>#reaction#</cfoutput>
Darcey
At 07:00 PM 12/12/2003 -0500, you wrote:
This may also be of help:
http://en.wikipedia.org/wiki/Polymorphism_(computer_science)
-----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Barney Boisvert Sent: Friday, December 12, 2003 6:48 PM To: [EMAIL PROTECTED] Subject: RE: [CFCDev] Polymorphic component methods? objects?
You don't quite have polymorphism right, but you're pretty close. Here's a simple example illustrating polymorphism with a few simple objects.
We'll start with an object named HTMLDisplayableObject. It has a method named 'display', which displays the object in a generic way (lets say by simply printing "object is here"). Yee-haw, you say, but it's actually VERY helpful.
Now lets say we have another object named HTMLTextObject that extends HTMLDisplayableObject. By default (as part of extension) it has a method named 'display', which is inherited from HTMLDisplayableObject. Now we know that a HTMLTextObject can just echo it's text for display, so we override the display method with a new implementation that writes out the object's text.
Lets make another object named HTMLImageObject, which also extends HTMLDisplayableObject. Again, it starts with the inherited 'display' method from it's parent, but this time we override it with a method that writes out an IMG tag, with a SRC attribute that points to the appropriate image.
Finally, we have one more object named HTMLPageAssembler, which, surprisingly, assembles an HTML page from a collection of HTMLDisplayableObjects. The page we're assembling uses a flow layout (one after another, no organization) of objects stored in an array. Since the layout is simple, we just do a loop over the array and call the 'display' method on each object in turn. We don't know what type of object they actually are, just that they are all HTMLDisplayableObjects (either directly or via extension).
Now onto the acutal polymorphism.
When we're in the loop over the objects, we don't actually know what method we're calling. The code (compile time) says we're calling the 'display' method of the HTMLDisplayableObject CFC, because all we know is that our array contains a series of HTMLDisplableObjects. The specific method we're calling, which very likely comes from a subclass, isn't determined until we actually call it (runtime), and may come from a subclass of HTMLDisplayableObject. That runtime decision about what methods to actually call is polymorphism.
Polymorphism doesn't just apply to displaying stuff either, it's used throughout nearly all OO systems of any complexity, including systems without a user interface at all.
Cheers, Barneyb
PS: A note on the inheritance chain I used. In Java (or the like), I'd start with an HTMLDisplayableObject interface, not a class. It'd probably end up as an abstract class at some point, but that would remain to be seen. CF doesn't provide interfaces with CFCs, so we have to use inheritance from the get-go.
> -----Original Message----- > From: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] On Behalf Of Darcey Spears > Sent: Friday, December 12, 2003 3:20 PM > To: [EMAIL PROTECTED] > Subject: [CFCDev] Polymorphic component methods? objects? > > Hi, > This list has been incredibly helpful to read, and now I'm > hoping someone > has an answer for a question I have.... > > I'm having trouble understanding polymorphism in components. > > I understand polymorphism to be the ability of an object to handle > arguments of different datatypes appropriately depending on > the type passed > in. The clearest explanation I've found uses the example of > an object with > a display() operation that can display a video clip or a text file > depending on what object gets passed in. > > Where I get lost is HOW a component actually handles > different datatypes. > Does the display() method test for the type and then execute > different > code? If I understand the data typing correctly, what would > be passed in is > probably another object, as the datatype. If it is, it is a > component name, > right? How is that set dynamically if you don't know what's coming in? > > Any information or examples would be greatly > appreciated...I'm obviously > way too confused! > > Thanks, > Darcey > > > Darcey Spears > Q3Studios Collaborative Web Development > www.q3studios.com > 520-360-5425 > > > ---------------------------------------------------------- > 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]
.............................................................. Darcey Spears Q3Studios Collaborative Web Development www.q3studios.com 520-360-5425
----------------------------------------------------------
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]