Let's consider below are the requirements for parking lot. Based on these requirements we will have to design a parking lot.
- The parking lot has multiple slots.
- The parking lot can park motorcycles, cars, and buses.
- The parking lot has small slots, compact slots, and large slots.
- A motorcycle can park in any slot.
- A car can park in either a single compact slot or a single large slot.
- A bus can park only in a single large slot.
We can devise below use cases from above requirement.
- Any vehicle enters and asks for free parking slot ID.
- If any appropriate parking slot is available, vehicle will get assigned parking slot and that parking slot will be blocked for parking till vehicle leaves the slot.
- Any vehicle when leaves the parking slot, the same slot will be free for parking other vehicles.
- If appropriate parking slot (to fit the vehicle) is not available, parking lot will return parking full message.
Lets break up the use cases in different module of functionality.
- Parking lot has multiple slots.
- Parking lot can display is parking is full.
- Parking lot can tell if given vehicle can be parked or not.
- Parking lot can allot slot for vehicle.
- Parking lot can clear allotted slot for vehicle.
- Parking lot has Parking slot allotter.
Parking slot allotter
- Parking slot allotter allots the parking slot based on below algorithm
- motorcycle can park in any slot
- car can park in either a single compact slot or a single large slot.
- bus can park only in a single large slot.
- If no slot is free for vehicle return, Parking Full message.
- Parking slot has type.
- Parking slot has status.
- Parking slot has ID.
- Parking slot can tell if given vehicle can be fit in the slot.
- Vehicle has a size (small, compact, large), which states how much space required to park
- Vehicle has type.
- Vehicle has park and leave parking functionality.
We need to take care that we should not derive individual vehicle type from vehicle, like Car, Bus etc.. Because tomorrow if slot is supporting 10 additional types of vehicle there will be class explosion and we are not much interested in vehicle types here but we are only interested in size of vehicle. Same analogy applies for the parking slot.