2

Help me in understanding the power of OOPS in this famous Parking Lot example. I read this post yesterday trying to figure out perfect lower level design.

ParkingLot - ParkingLot has ParkingSpaces ( List of ParkingSpaces) - exit() - entrance() - Update() - int capacity

ParkingSpace -long id -String type (2 wheeler, 4 wheeler etc) -Vehicle (has Vechicle Reference)

Vehicle (interface- any kind of vehicle (two wheeler, four wheeler) - park() - Unpark() abstract methods - has reference of ParkingLot and ParkingSpace

Car,Truck different kind of vehicles implements Vehicle.

Is there anything am I missing here...is there any design pattern I need to follow here.. How are we achieving polymorphism in this example.

Community
  • 1
  • 1
SPD
  • 247
  • 2
  • 8
  • 23

1 Answers1

2

Object-oriented programming helps in this situation because your vehicle interface allows you to define similar actions for many different vehicles without having to write those methods/functions for each individual type of vehicle.

It sounds like you've got a pretty good handle on your classes (ParkingLot, ParkingSpace, Car, Truck) and your interface (Vehicle). If different types of parking spaces had different fields, you could consider a ParkingSpace interface which is then implemented by MotorcycleSpace, CompactSpace, etc. but how you have it now isn't incorrect. Polymorphism comes in as well with your interface.

EDIT: With regard to composition, you have a ParkingLot that has ParkingSpaces. If your Vehicle class had Wheels or Options or some other subclass, that would be additional composition.

Alex Mullans
  • 1,699
  • 2
  • 17
  • 34