7. Design Elevator System
Difficulty: Medium Topics: State Design Pattern, Strategy Pattern, Scheduling Algorithm (SCAN) Key Concepts: Concurrency, Request Optimization, State Management.
Phase 1: Requirements Gathering
Goals
Design a smart elevator system for a building (M floors, N elevators).
Efficiently schedule elevators to minimize wait time.
Handle concurrent requests safely.
1. Who are the actors?
Passenger: Presses buttons inside or outside the elevator.
System: Dispatches elevators and manages their state.
2. What are the must-have features? (Core)
External Request: User presses Up/Down button on a floor.
Internal Request: User presses a specific floor button inside the elevator.
Scheduling: Algorithm to decide which elevator services a request.
Door Control: Open/Close logic based on state.
Safety: Don't change direction mid-flight unless idle; respect capacity (optional).
3. What are the constraints?
Real-time: Decisions must be made instantly.
Optimization: Prefer minimizing wait time vs. minimizing power (usually wait time).
Phase 2: Use Cases
UC1: Request Elevator (External)
Actor: Passenger Flow:
Passenger on Floor X presses 'UP'.
System receives
ExternalRequest(Floor X, UP).Dispatcher selects the best elevator (e.g., closest moving UP or Idle).
Elevator adds Floor X to its stop list.
Elevator arrives at Floor X, opens doors.
UC2: Select Destination (Internal)
Actor: Passenger Flow:
Passenger enters elevator and presses 'Floor Y'.
System receives
InternalRequest(ElevatorID, Floor Y).Elevator adds Floor Y to its stop list.
Elevator moves to Floor Y.
Phase 3: Class Diagram
Step 1: Core Entities
ElevatorSystem: Singleton controller.
Elevator: The physical car.
Request: External (Floor, Direction) or Internal (Floor).
Dispatcher: Implementation of scheduling logic.
State: Enum (IDLE, MOVING_UP, MOVING_DOWN).
UML Diagram
Phase 4: Design Patterns
1. Strategy Pattern
Description: Defines a family of algorithms, encapsulates each one, and makes them interchangeable.
Why used: The elevator dispatching logic (
DispatchStrategy) can vary (First-Come-First-Serve, Shortest Seek Time First, SCAN/Elevator Algorithm). Strategy allows hot-swapping these algorithms based on traffic patterns (e.g., Morning Rush vs. Idle time).
2. State Pattern
Description: Allows an object to alter its behavior when its internal state changes.
Why used: An Elevator has different valid actions depending on its state (e.g., cannot
move()ifDoorsOpen, cannotopenDoors()ifMoving). State pattern encapsulates this logic, preventing invalid transitions.
Phase 5: Code Key Methods
Java Implementation
Phase 6: Discussion
Algorithms
Q: Explain Scheduling Algorithms?
FCFS: First Come First Serve. Simple but inefficient (bad throughput).
SSTF: Shortest Seek Time First. Goes to nearest request. Can cause starvation for distant floors.
SCAN: Elevator moves all the way UP, then all the way DOWN. Efficient but suboptimal average wait.
LOOK: Similar to SCAN, but reverses direction as soon as there are no more requests in the current direction. Preferred.
Capacity & Safety (SDE-3 Concept)
Q: How to handle max weight realistically?
A: "In real life, elevators don't track the number of people, algorithms track weight. Add a
WeightSensorclass (Observer pattern) that publishesWeightExceededEvent. When triggered, transition to anOVERLOADEDstate, trigger an alarm, and disable door close mechanisms until the event resolves. Software should fail-safe."
Thread Management (SDE-3 Concept)
Q: How do you handle multiple elevators moving concurrently?
A: "Each
Elevatorshould implementRunnableand run in its own thread, managed by aScheduledExecutorService. TheDispatchercalculates the route, while theElevatorthread independently checks itsStopSetandState, sleeping between floors to simulate travel time safely."
Optimization
Q: How to handle Peak Hours (Morning Rush)?
A: "Use Zoning. Dedicate specific elevators to serve only Ground -> Even Floors or Ground -> Odd Floors, or specific High/Low rise banks to reduce stops."
SOLID Principles Checklist
S (Single Responsibility):
Elevatormoves/stops,Controllerdispatches.O (Open/Closed): Can implement new
DispatchStrategy(e.g.,PeakHourStrategy) without changing Controller code.L (Liskov Substitution):
Elevatorbehaviors are consistent.I (Interface Segregation):
DispatchStrategyis focused.D (Dependency Inversion):
ElementSystemshould depend onDispatchStrategyinterface.
Last updated