Object-Oriented Design• Patterns: Singleton, Factory Method
Parking Lot System
Medium
Problem Summary
Design a multi-level parking lot that can accommodate different vehicle types, handle ticketing, and process billing dynamically.
Functional Scope
- Support multiple floors and spot types (Compact, Large, Motorcycle).
- Process vehicle check-in (issue ticket, assign spot) and check-out (compute fee, release spot).
- Thread-safe singleton control for the main ParkingLot instance.
- Support dynamic fare calculation based on hour counts and vehicle types.
Entity-Relationship (ER) Schema
ParkingLot [1] <---> [*] ParkingFloor ParkingFloor [1] <---> [*] ParkingSpot ParkingTicket [1] <---> [1] ParkingSpot ParkingTicket [1] <---> [1] Vehicle
Design Approach
Define a clean class hierarchy for Vehicles and ParkingSpots. Ensure concurrency control on spot allocation (synchronized methods) so multiple gates don't double-book the same physical spot.
Core Classes & Models
ParkingLot (Singleton)ParkingFloor (List of Spots)ParkingSpot (Base class for CompactSpot, LargeSpot, etc.)Vehicle (Base class for Car, Motorcycle, Truck)ParkingTicket (Tracks entry/exit times and spot references)
Code Blueprint
public class ParkingLot {
private static ParkingLot instance;
private ParkingLot() {}
public static synchronized ParkingLot getInstance() {
if (instance == null) instance = new ParkingLot();
return instance;
}
}