System Design / State• Patterns: State, Strategy (Dispatching)
Elevator System
Hard
Problem Summary
Design a controller for a bank of elevators optimizing request handling, speed, and energy across multiple floors.
Functional Scope
- Elevators must go Up, Down, or stay Idle.
- Dispatch algorithm should pick the optimal elevator for internal and external requests.
- Handle state transitions cleanly (doors opening, closing, moving, emergency stops).
- Support request queues with direction optimization (e.g. SCAN algorithm).
Entity-Relationship (ER) Schema
ElevatorController [1] <---> [*] ElevatorCar ElevatorCar [1] <---> [1] ElevatorState ElevatorCar [1] <---> [*] Request
Design Approach
Use the State pattern to decouple direction & floor transition logics from the main Elevator class. Maintain separate sorted sets (upQueue and downQueue) for SCAN-based request handling.
Core Classes & Models
ElevatorController (Manages multiple cars)ElevatorCar (Current state, floor, direction)Request (Source floor, target floor, direction)ElevatorState (Interface for IdleState, MovingState, StoppedState)
Code Blueprint
public enum Direction { UP, DOWN, IDLE }
public interface ElevatorState {
void handleRequest(ElevatorCar car, int floor);
}