1. Requirements (The Scope)
- Functional:
- Support multiple floors.
- Support different vehicle types (Bike, Car, Truck).
- Multiple entry and exit points.
- Flexible pricing based on time.
- Non-Functional:
- Thread-safety (Multiple cars entering at once).
- High extensibility for new vehicle types.
2. Entity Identification
ParkingLot: Singleton managing the entire system.Floor: Collection of slots.ParkingSlot: Individual unit with a type (Small, Medium, Large).Vehicle: Abstract base class.Ticket: Issued at entry.PaymentService: Strategy-based calculator.
3. The Class Diagram (Logic)
A. The Vehicle Hierarchy
public abstract class Vehicle {
private String licensePlate;
private VehicleType type;
// Getters
}
public class Car extends Vehicle {
public Car(String plate) { super(plate, VehicleType.CAR); }
}
B. The Slot and Strategy
public class ParkingSlot {
private int id;
private VehicleType type;
private boolean isFree;
private Vehicle currentVehicle;
public void park(Vehicle v) { this.currentVehicle = v; this.isFree = false; }
}
4. Addressing Thread Safety
In a real interview, the senior signal is discussing Concurrency.
- Use
ConcurrentHashMapfor slot mapping. - Use
ReentrantLockorsynchronizedblocks when assigning a car to a slot to prevent two cars from taking the same spot.
Final Takeaway
Focus on the Interfaces. Use the Strategy pattern for pricing and the Factory pattern for creating vehicles. This shows you understand OCP and SRP.