I am reading about OOD and came across Parking lot design problem.Parking lot has parking floors which has parking spots.The parking spot class looks as follow:
public enum ParkingSpotType {
HANDICAPPED, COMPACT, LARGE, MOTORBIKE, ELECTRIC
}
public abstract class ParkingSpot {
private String number;
private boolean free;
private Vehicle vehicle;
private final ParkingSpotType type;
public boolean IsFree();
public ParkingSpot(ParkingSpotType type) {
this.type = type;
}
public boolean assignVehicle(Vehicle vehicle) {
this.vehicle = vehicle;
free = false;
}
public boolean removeVehicle() {
this.vehicle = null;
free = true;
}
}
public class HandicappedSpot extends ParkingSpot {
public HandicappedSpot() {
super(ParkingSpotType.HANDICAPPED);
}
}
public class CompactSpot extends ParkingSpot {
public CompactSpot() {
super(ParkingSpotType.COMPACT);
}
}
public class LargeSpot extends ParkingSpot {
public LargeSpot() {
super(ParkingSpotType.LARGE);
}
}
public class MotorbikeSpot extends ParkingSpot {
public MotorbikeSpot() {
super(ParkingSpotType.MOTORBIKE);
}
}
public class ElectricSpot extends ParkingSpot {
public ElectricSpot() {
super(ParkingSpotType.ELECTRIC);
}
}
The parameters are self explanatory.So here why do we need separate classes for each ENUM value?What is the advantage of this?I thought of having only one ParkingSpot class type with one field representing type of parking slot.But why do we need to have separate classes for each ENUM value along with storing Enum as a field also?
Note: For complete design please refer https://www.educative.io/courses/grokking-the-object-oriented-design-interview/gxM3gRxmr8Z