Say I run a used car dealership. So I have a class for Vehicle.
A new vehicle arrives, so I create an instance of the Vehicle class:
v = new Vehicle();
Then I'm told this vehicle is blue:
v.Color = blue;
Then I'm told this vehicle is a motorcycle with chrome handlebars. My Motorcycle class is an extension of the base class Vehicle, but now I have to abandon my Vehicle class v
and create an instance of Motorcycle:
m = new Motorcycle();
and copy over the color:
m.Color = v.Color;
m.handlebars = Chrome;
This doesn't seem right. I shouldn't have to abandon my vehicle class v
and start a new class m
. Yet it wouldn't be appropriate to have a "handlebar" field in my Vehicle class.
This could continue. Say I'm then told this is a Kawasaki. Now I have to abandon my m
instance of Motorcycle, create an instance of class Kawasaki which extends class Motorcycle, and copy over all the info once again.
What I'm really
doing is receiving a message, which I thought I'd create an instance of a Message class and put the message in it. Only problem is there are several different types of messages, I don't know what type of message I have until I parse it, but it seems from a design point of view that the parsing should be done inside the Message class, but I don't know what kind of Message class instance to create until I parse the message. Catch 22.
How does one deal with this from a design point of view?