Parking Lot — Detailed#
classDiagram
direction TB
class ParkingLot {
-levels: List~Level~
-ticketRepo: TicketRepo
-strategy: AllocationStrategy
-paymentSvc: PaymentService
+park(v: Vehicle) Ticket
+unpark(t: Ticket) Receipt
+findSlot(v: Vehicle) Slot
}
class Level {
-slots: List~Slot~
-id: int
+freeSlots(type) List~Slot~
}
class Slot {
-id: String
-type: SlotType
-occupied: boolean
+occupy(v: Vehicle)
+release()
}
class Vehicle {
<<abstract>>
+regNo: String
+type(): VehicleType
}
class Car
class Bike
class Truck
class EV {
+chargingPort: boolean
}
class Ticket {
+id: String
+entryTs: Instant
+slot: Slot
+vehicle: Vehicle
}
class Receipt {
+ticketId: String
+exitTs: Instant
+duration
+amount
+paymentRef
}
class AllocationStrategy {
<<interface>>
+pick(slots, vehicle) Slot
}
class NearestFirst
class CompactSlot
class ByVehicleType
class PricingPolicy {
<<interface>>
+cost(duration, vehicle) Money
}
class FlatHourly
class PeakOffPeak
class DayPass
class PaymentService {
+charge(amount, method) PaymentRef
}
class GateController {
-entryGates
-exitGates
+open(g)
+close(g)
}
class DisplayBoard {
+update(level, free)
}
class TicketRepo {
<<interface>>
+save(t)
+findById(id) Ticket
}
ParkingLot "1" *-- "many" Level
Level "1" *-- "many" Slot
Vehicle <|-- Car
Vehicle <|-- Bike
Vehicle <|-- Truck
Vehicle <|-- EV
ParkingLot --> AllocationStrategy
AllocationStrategy <|.. NearestFirst
AllocationStrategy <|.. CompactSlot
AllocationStrategy <|.. ByVehicleType
ParkingLot --> PricingPolicy
PricingPolicy <|.. FlatHourly
PricingPolicy <|.. PeakOffPeak
PricingPolicy <|.. DayPass
ParkingLot --> PaymentService
ParkingLot --> TicketRepo
ParkingLot --> GateController
ParkingLot --> DisplayBoard
Ticket --> Slot
Ticket --> Vehicle
Design notes#
- Strategy pattern for allocation and pricing (swap without code change).
- Repository pattern hides DB; tests use in-memory.
- Observer pattern for DisplayBoard (subscribes to slot changes).
- State on Slot prevents double-occupy; CAS in repo.
Glossary & fundamentals#
Concepts referenced in this design. Each row links to its canonical page; the tag column shows whether it is a high-level (HLD) or low-level (LLD) concept.
| Tag | Concept | What it is | Page |
|---|---|---|---|
LLD |
Testing strategy | pyramid, doubles, TDD, contracts | testing-strategy |
LLD |
Behavioural patterns | Strategy, Observer, State, Command, Chain | behavioral-patterns |
LLD |
Concurrency primitives | mutex, semaphore, RW lock, atomic, CAS | concurrency-primitives |