Case study · Advanced

Design an Elevator System

Multiple elevators, hall and cabin requests, and a scheduler that decides which car goes where — modelled with a direction State machine and a pluggable dispatch Strategy.

Asked atAmazonGoogleUber
step 1 / 8
1/2Model the system
elevators *dispatch
ElevatorSystem
+ request(floor, dir)
«interface»
Dispatcher
+ pick(req): Elevator
Request
- floor
- dir
Elevator
- floor
- dir
- stops
+ addStop(f)
Button
- floor
Model the system
1class ElevatorSystem {
2 List<Elevator> elevators;
3 DispatchStrategy dispatch;
4 void request(int floor, Dir dir) {
5 Elevator e = dispatch.pick(new Request(floor, dir));
6 e.addStop(floor);
7 }
8}
State
requirementroute requests to cars

ElevatorSystem is the entry point. It receives hall requests and delegates the genuinely hard "which car?" decision rather than deciding inline.