I have a car maintenance garage program that has an abstract Vehicle
class that has several derived classes like Car
MotorCycle
, etc. Each of those derived vehicles in turn is either a fuel or an electric vehicle. So instead of having a FuelCar
, ElectricCar
, FuelMotorCycle
, ElectricMotorCycle
etc, I'm trying to implement it using composition.
The problem now is that I would have to check all the time if it's an electric or fuel vehicle, and then do as cast
(that's like reinterpret_cast from c++) accordingly, or would have to wrap each as cast
with try catch
. It adds complexity to the code.
Another option was removing the EnergySource
empty class and have each vehicle to hold both Fuel
and Electric
and to initialize only one in the ctor, but it doesn't seem right to have a fuel car that also have an electric part (even if that part is null). Although this seems the simplest of the three.
Any suggestions on what is the best approach?
Here's the UML diagram of what I currently have: