1. Requirements & Entity Modeling
A parking lot consists of multiple floors, each equipped with dedicated spot types: Motorcycle (Small), Compact (Medium), Large (Bus/Truck), and EV Charging spots. The system handles ticketing at entry gates and fee calculation at exit gates.
- Vehicles: Motorcycle, Car, Van, Truck, EV Car
- Spot Types: TwoWheeler, Compact, Large, Electric
- Pricing Models: Hourly, Flat Rate, Time-of-Day Surge
- Concurrency: Thread-safe spot allocation preventing double-parking
2. Applying Design Patterns: Strategy & Factory
We use the Factory Method Pattern to instantiate vehicle and spot objects based on sensor telemetry. The Strategy Pattern decouples fee calculation (HourlyFeeStrategy, SurgeFeeStrategy, WeekendFeeStrategy) from ticket checkout.
For spot allocation, a `ParkingStrategy` interface allows swapping between 'Nearest to Entrance' (min-heap) and 'Even Floor Distribution'.
3. Concurrency & Thread-Safe Spot Allocation
When multiple gates issue tickets simultaneously, two cars might be assigned the exact same parking slot. We protect slot allocation using fine-grained locks (e.g. `ReentrantLock` per floor or atomic compare-and-swap on slot status).
abstract class Vehicle {
constructor(public licensePlate: string, public type: string) {}
}
class Car extends Vehicle {
constructor(licensePlate: string) { super(licensePlate, 'CAR'); }
}
interface PricingStrategy {
calculateFee(durationHours: number): number;
}
class HourlyPricingStrategy implements PricingStrategy {
constructor(private hourlyRate: number) {}
calculateFee(hours: number): number {
return Math.ceil(hours) * this.hourlyRate;
}
}
class ParkingSpot {
private isOccupied: boolean = false;
constructor(public id: string, public floor: number, public type: string) {}
public occupy(): boolean {
if (this.isOccupied) return false;
this.isOccupied = true;
return true;
}
public vacate() { this.isOccupied = false; }
}Model this architecture on the HLDForge Canvas
Turn static box diagrams into living, collaborative systems. Drag and drop microservices, Kafka clusters, and cache tiers. Run automated SPOF detection and STRIDE threat modeling in real time.
Written by HLDForge Architecture Team
Lead Machine Coding Specialist
The HLDForge Architecture Team publishes practical engineering guides, real-world case studies, and high-concurrency blueprints to help engineers build scalable and fault-tolerant software.