Nando,
The problem is I know I've got it upside down, I'm trying to turn it right
side up! :-)
I realize I'm not supposed to instantiate parent classes, the problem is
how do I initialize a car if I don't know what type of car it is? So back
to my problem, I have a VIN number to a car and I need to know if that car
is a truck and therefore if I can tow a boat with it.
I suppose I could try an instantiate a truck and if that fails then I can
do other things with it, like drive fast....
<cfset myCar = createObject("component","com.truck").init()>
<cfset truckDAO.read(myCar,form.vin_number)>
<cfif myCar.isvalid()> <!--- meaning my truck is a truck --->
<cfset myCar.towBoat()>
<cfelse>
Sorry, you can't tow a boat with your car, maybe you can drive fast
with it.
<cfset myCar = createObject("component","com.sportscar").init()>
<cfset sportscarDAO.read(myCar,form.vin_number)>
<cfif myCar.isvalid()>
<cfset myCar.driveFast()>
<cfelse>
Nope you can't even drive fast in it
</cfif>
</cfif>
Or should I scrap the subclasses and have type of car be an attribute and
towBoat and driveFast both be methods of Car and just perform some logic
that if the Car is not a truck it can't tow a boat and throws an error.
<cffunction name="towBoat">
<cfif instance.type neq 'truck'>
<cfthrow message="You can't tow a boat with your car">
<cfelse>
<!--- do whatever code is needed to tow the boat --->
</cfif>
</cffunction>
I guess what I'm asking is instead of
CAR is a TRUCK
I could use
CAR has a hitch
CAR has a powerful V8 engine
then I can tow a boat.
Hmmm.....
Jason Cronk
[EMAIL PROTECTED]
"Nando"
<[EMAIL PROTECTED] To: [email protected]
.com> cc:
Sent by: Subject: RE: [CFCDev] Question
on class / subclass
[EMAIL PROTECTED]
one.org
04/06/2005 12:58
PM
Please respond to
CFCDev
Jason,
I think you've got things upsidedown. Let me start by saying i had things
upsidedown for awhile too, so i really understand.
My understanding of the situation is that generally speaking, you don't
instantiate parent classes. I know it can sound weird, but in OO, parents
don't know what kind of children they've got.
You wanna design your model so that the common stuff that all trucks,
sports
cars and suv's share is in Car, and instantiate only Truck, SportsCar and
SUV.
If that doesn't work for what you wanna do, then you're probably looking at
composition, not inheritance.
For me, the key to getting this was that you abstract common functionality
to the parent, rather than duplicating code in all the children.
-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
Behalf Of [EMAIL PROTECTED]
Sent: Wednesday, April 06, 2005 6:32 PM
To: [email protected]
Subject: [CFCDev] Question on class / subclass
Hi,
This is probably a simple question but I haven't seen it explicitly
illustrated anywhere.
Lets say I have the classic CAR class and subclasses SportsCar, Truck
and SUV. I get an instance myCAR and I need to determine if myCAR is a
SportsCar, Truck or SUV. Would I call a method in CAR such as
myCar.gettype()? Or in this case is being a SportsCar, Truck or SUV just
an attribute of CAR not a subclass (where I would have sportscar.cfc,
truck,cfc, suv.cfc)? I'd prefer it to be a subclass since those subclasses
have different methods like Truck.towBoat(). But I don't know if I can tow
boat with myCAR because I don't know if myCAR is a Truck.
Some code to illustrate what I'm looking for....
<cfset myCar = createObject("component","com.car").init()>
<cfset carDAO.read(myCar,form.vin_number)>
<cfif myCar.getType() is "truck">
<cfset myCar.towBoat()>
<cfelse>
Sorry, you can't tow a boat with your car
</cfif>
Obviously a big fallacy here would be that myCar is a CAR object and
probably would have to be recast as a Truck object for me to call
myCar.towBoat(). Something like
<cfset myCar = createObject("component","com.truck").init()>
<cfset truckDAO.read(myCar,form.vin_number)>
within the cfif tag.
Am I missing some fundamental property here? How do I refer to a generic
unknown car and yet have subclasses of cars that behave differently?
Jason Cronk
[EMAIL PROTECTED]
----------------------------------------------------------
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]
----------------------------------------------------------
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]
----------------------------------------------------------
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]