I got an assignment in class (computer science) - I need to design a class diagram for a system that handles a garage for different types of vehicles. The point of this assignment is to use composition and inheritance correctly.
The garage can take any vehicle and handle it based on whether the vehicle is a Fueled vehicle or an electric vehicle, for example a fueled vehicle will be refueled but an electric vehicle will be charged.. so i thought about accomplishing this by creating an abstract Vehicle class and two abstract classes that inherits from it - FueledVehicle and ElectricVehicle, and then there will be concrete classes that inherits from each of those (for example: ElectricCar inherits from ElectricVehicle and FueledCar inherits from FueledVehicle).
My problem is that cars (fueled and electric) have some properties in common like numberOfDoors and I wouldn't want to define all of the shared properties under two different classes separately and have double code. So I came to the conclusion that it needs to be under a different class CarProperties, but now the issue is that I can create an instance of the class CarProperties which in essence shouldn't be concrete but if i make it an abstract class I run into other issues..
I would be grateful if anyone could offer a way to solve this issue either by restructuring the system or by solving the instances issue.
There are a number of ways to go about this and ultimately depends on what the end goal is but I suspect that isn't properly defined and this is really just a classification problem, so this can probably be kept fairly simple but you could expand on each property much more if need be.
First of all, how would I want to instantiate this? I expect to have one garage instance (definition out of scope) with the ability to add or remove vehicles at runtime. So first I need to represent each vehicle type. Below I have created 3 concrete vehicle types:
Vehicle (abstract)
Each vehicle can have a number of power sources:
PowerSource (abstract)
Define our base class where the common properties live. These could be wrapped into another class but I don't see the need.
Therefore our PetrolVehicle, which could be a car, truck or bus etc (this could be defined as a further subclass or just a property of PetrolVehicle), would be something like:
Using the PetrolVehicle class, I can instantiate this like:
From here, you may want to know what's in the garage like: