[UPHPU] Objected Oriented question

cole at colejoplin.com cole at colejoplin.com
Tue Sep 18 08:50:58 MDT 2007


Quoting Alvaro Carrasco <alvaro at epliant.com>:

> Justin Giboney wrote:
>> I am trying to wrap my head around object oriented programming, but  
>>  I  am having a difficult time trying to figure out how to code   
>> something  like this....
>>
>> A car manufacturing company makes multiple models of cars (like,    
>> Toyota: Sienna, Tundra, Camry). Each model has a low end, medium   
>> end,  and high end version. Each version has a set of options it   
>> can choose  from.
>>
>> Here are my thoughts:
>> There are at least two classes here (car and option), but I don't    
>> know wether to make subclasses of each of the models. Then do I   
>> make  subclasses of each of the models for low, medium, and high end?
>>
>> I could create an array in each of the subclasses for each of the    
>> options available, but what if there are a hundred options?
>>
>> What if an option provides more options (like, if you get leather    
>> seats, then you can get them heated if you like)?
>>
>>
> I would create these classes:
> Car
> Option
> Configuration
>
> and i would probably not create subclasses for each model (inheritance
> is one of many ways to be achieve reuse, and it is usually overused...
> look up composition and association)
> The car object would probably have methods like:
>
> $car->getLowEndConfiguration(); // returns a configuration object,
> configured with the basic options
> $car->getMediumEndConfiguration();
> etc.
>
> these methods would compose a configuration object that would contain
> the options...
>
> I would need more specifics to keep going.
>
> Good luck,
> Alvaro
>

I would recommend starting with a loose form of OOD (diagramming). I  
would envision starting with one class, and then subclassing.

vehicles->car->carSpecificOptions
vehicles-truck->truckSpecificOptions
vehicles->suv->suvSpecificOptions

This way, all vehicle types can inherit general things, like basic 4  
tires, steering housing, rear-view mirror, etc.. Then the next level  
could have things like chassis differences. Then lower, you could have  
things like LCD TVs only available for SUVs. Basically, the more that  
can be shared higher up the better. Then subclass the specialization,  
for as many classes and subclasses that make sense. But the attitude  
of inheritance and polymorphism should remain at all levels.

-- Cole






More information about the UPHPU mailing list