State Machine Diagram
Purpose of This Diagram
An elevator is a stateful object — its behavior changes fundamentally depending on whether it's idle, moving up, moving down, or has its doors open. When idle, it accepts any request and starts moving toward the target. When moving, it stops at every floor one of its requests is for. When its doors are open, it must close them before it can move.
The state machine diagram captures this lifecycle: every state the elevator can be in, every event that triggers a transition, and every guard condition that determines which transition fires. The diagram could map to the State Pattern (Fundamentals S12), with a class per state; our implementation keeps the state in the ElevatorState enum instead, which move() and stop() set — four states are few enough not to need a class each.
We'll model two state machines for the Elevator System:
- Elevator State — the primary state machine (Idle, Moving Up, Moving Down, Door Open)
- Door State — the sub-state machine for door operation (Closed, Opening, Open, Closing)
Like the Vending Machine (DP6), the elevator has distinct behavior in each state. But unlike the Vending Machine's simple linear flow, the elevator has bidirectional cycling — it goes up, then down, then up again — making the state transitions more complex.
Identifying Elevator States
From our requirements, we can identify the distinct states an elevator can be in:
Elevator States (ElevatorState, from R5, R6 and R8):
IDLE — The elevator is stationary with no pending requests. It can accept any new request (hall call or cabin call) and begin moving in the required direction.
- Entry action:
direction = IDLE;notifyObservers()— the floor displays show it idle - Behavior: Waits for the next request. A request changes nothing yet: the car leaves IDLE at the next step of the clock (
move()).
MOVING_UP — The elevator is traveling upward, serving requests in ascending floor order. It stops at every floor one of its requests is for, whatever the request's direction.
- Entry action:
direction = UP;notifyObservers()— the floor displays show it going up - Each step (
move()) takes it one floor:currentFloor++ - Behavior: At each floor, checks
shouldStop(). If yes, transitions to DOOR_OPEN. Otherwise, continues up.
MOVING_DOWN — The elevator is traveling downward, serving requests in descending floor order.
- Entry action:
direction = DOWN;notifyObservers() - Each step takes it one floor down:
currentFloor-- - Behavior: Mirror of MOVING_UP but in the opposite direction.
DOOR_OPEN — The elevator has stopped at a floor. Doors are open for passengers to enter/exit. The elevator cannot move in this state: it leaves it by closing its doors first.
- Entry action:
door.open();removeRequestForFloor(currentFloor)— every request for this floor is served (and a served hall call frees its button) - The doors stay open until the next step (
move()); a real elevator would hold them for a timer instead - Exit action:
door.close()
Why DOOR_OPEN is a separate state (not a sub-state of MOVING_UP/MOVING_DOWN):
When the doors are open, the elevator's behavior is identical regardless of its previous direction. It doesn't matter if the elevator arrived from below or above — it opens doors, waits, closes doors, and then decides what to do next. Making DOOR_OPEN a distinct state simplifies the diagram and avoids duplicate transitions.
Defining Transitions
Now let's define every transition between states. Each transition has an event (what triggers it), an optional guard (condition that must be true), and an optional action (what happens during the transition). In our code there is one event: move(), one step of the clock — ElevatorSystem.step() calls it on every elevator. A transition with no event fires as soon as its state is entered and its guard holds. nextStop is the floor the scheduling strategy picks (getNextStop()).
From IDLE:
move() [nextStop > currentFloor] / currentFloor++→ MOVING_UP — the next stop is above.move() [nextStop < currentFloor] / currentFloor--→ MOVING_DOWN — the next stop is below.move() [nextStop == currentFloor]→ DOOR_OPEN — A request for the current floor (passenger is already here).
A request does not change the state by itself: addRequest() only queues it, and the car leaves IDLE at the next move().
From MOVING_UP:
[shouldStop(currentFloor)]→ DOOR_OPEN — The elevator reaches a floor with a pending request. It is checked as the car arrives — and if a request for the floor the car is on comes in before the next step, thatmove()stops it there.move() [nextStop > currentFloor] / currentFloor++→ MOVING_UP (self-transition) — The elevator passes a floor with no pending request.move() [nextStop < currentFloor] / currentFloor--→ MOVING_DOWN — direction reversal without a stop: only with SCAN, which sweeps on to the top floor and turns there.
From MOVING_DOWN:
[shouldStop(currentFloor)]→ DOOR_OPEN — Mirror of MOVING_UP.move() [nextStop < currentFloor] / currentFloor--→ MOVING_DOWN (self-transition)move() [nextStop > currentFloor] / currentFloor++→ MOVING_UP — only with SCAN, at floor 1.
A moving elevator never goes straight to IDLE: its last request is a stop, so it passes through DOOR_OPEN.
From DOOR_OPEN:
move() [nextStop > currentFloor] / currentFloor++→ MOVING_UP — the doors close (the exit action), and the elevator resumes upward or reverses upward.move() [nextStop < currentFloor] / currentFloor--→ MOVING_DOWN — the doors close and the elevator goes down.move() [nextStop == currentFloor] / removeRequestForFloor(currentFloor)— an internal transition: a new request for this floor is served and the doors stay open.[!hasRequests()]→ IDLE — the stop served the last request: the doors close at once and the elevator goes idle.
Notice how the DOOR_OPEN state acts as a decision point — after serving a floor, the elevator must decide whether to continue in the same direction, reverse, or go idle. This decision depends on the scheduling algorithm's output.
Door Sub-State Machine
The door has its own state machine, and it runs inside the DOOR_OPEN state of the elevator: the doors open as the elevator enters DOOR_OPEN and close as it leaves. This is a nested state machine (Fundamentals S5).
Door States (DoorStatus):
- CLOSED — Default state. The elevator can move.
- OPENING — The door is in motion, transitioning from closed to open. Takes ~2 seconds in a real elevator; in our model it is instant.
- OPEN — Fully open. Passengers can enter/exit.
- CLOSING — The door is in motion, transitioning from open to closed.
Door Transitions (in our code, each pair is one call):
- CLOSED → OPENING → OPEN:
open()—stop()calls it as the elevator enters DOOR_OPEN; it does nothing unless the door is CLOSED. - OPEN → CLOSING → CLOSED:
close()—move()calls it before the car leaves the floor, andstop()when it has served the last request; it does nothing unless the door is OPEN.
What a real door adds (not in our code — the second figure shows them in grey):
do / activateMotor(OPEN)in OPENING anddo / activateMotor(CLOSE)in CLOSING, withfullyOpenedandfullyClosedevents- a hold-open timer in OPEN (
entry / startTimer()) and OPEN → CLOSING ontimerExpiredorcloseDoorButton - OPEN → OPEN:
openDoorButton / resetTimer()— a passenger holds the door open, the timer resets - CLOSING → OPENING:
obstacleDetected / reverseMotor()— the safety reversal
The obstacle detection transition (CLOSING → OPENING) is a critical safety feature in real elevators. When the infrared sensor detects an obstruction, the door immediately reverses. This self-transition-like behavior (CLOSING → OPENING → OPEN) prevents the door from closing on passengers.
Run the State Machines
The state machines below run on the Code Implementation's own move() and stop(), with one elevator of the usage example's 10-floor building. Press floors — beside them in the shaft, or inside the car — and step the clock: each move() carries the token along the arrow the code takes, the door's machine opens and closes inside DOOR_OPEN, and the lines of move() and stop() that ran light up in Python or Java. Switch to SCAN to see a direction reversal at the building's edge, and turn on Ask me first to predict each step's arrow before it runs.
![Interactive: the Elevator System's two state machines run live on the lesson's code — one elevator of the usage example's 10-floor building, a token on the elevator's chart (IDLE, MOVING_UP, MOVING_DOWN, DOOR_OPEN) and one on the door's (CLOSED, OPENING, OPEN, CLOSING). Hall buttons beside each floor and the car's own floor buttons queue requests; a request changes nothing until the next step. Each Step is one move(): the token travels the arrows it takes, in order — IDLE to a moving state on [above] or [below] (or straight to DOOR_OPEN on [here]), a moving state's self-loop past a floor, [shouldStop] into DOOR_OPEN as it reaches a requested floor, DOOR_OPEN out on the next move() with its doors closing first, or to IDLE [no requests] once the last request is served; the doors stay open while it is DOOR_OPEN. SCAN sweeps on to floor 10 (or 1) and reverses there without a stop; LOOK and FCFS reverse at a stop. The lines of move() and stop() that ran are lit in Python or Java, and a legend gives each guard's code (above: nextStop > currentFloor, and so on); on a phone the arrows carry their ids, with a key below. Ask me first asks which arrow move() will take before each step, with the strategy's next stop given. Undo, Reset; eight missions.](https://pub-a1b3030acfb94e84ba8a89fb182c53bc.r2.dev/public/lld-content-9966f977-fc31-40b3-981e-14ecb32539ed/elevator_state_machine_complete_v2.webp)
Complete Elevator State Machine Diagram
![UML state machine of one elevator as the code moves it. The initial state enters IDLE (entry / direction = IDLE; notifyObservers()). In a row below: MOVING_UP and MOVING_DOWN (entry / direction = UP or DOWN; notifyObservers()) and, between them, DOOR_OPEN (entry / door.open(); removeRequestForFloor(currentFloor); an internal move() [here] / removeRequestForFloor(currentFloor), the doors staying open; exit / door.close()). Each step of the clock is move(): from IDLE to MOVING_UP [above] / currentFloor++, to MOVING_DOWN [below] / currentFloor--, or to DOOR_OPEN [here]; a moving state loops on itself a floor at a time and enters DOOR_OPEN [shouldStop] at a requested floor; DOOR_OPEN leaves on move() [above] or [below] and goes to IDLE [no requests] once the last request is served; MOVING_UP and MOVING_DOWN turn into each other only with SCAN, at the top and bottom floors. A legend gives each guard's code: above is nextStop > currentFloor, below is nextStop < currentFloor, here is nextStop == currentFloor, shouldStop is shouldStop(currentFloor), no requests is !hasRequests().](https://pub-a1b3030acfb94e84ba8a89fb182c53bc.r2.dev/public/lld-content-9966f977-fc31-40b3-981e-14ecb32539ed/elevator_state_machine_complete_v2.webp)
Complete Door Sub-State Machine Diagram

Key Design Decisions
1. Why DOOR_OPEN as a separate state instead of a sub-state of MOVING_UP/MOVING_DOWN?
When the doors are open, the elevator's behavior is identical regardless of its previous direction. A passenger gets on, and at the next step the doors close. Making DOOR_OPEN a distinct state avoids duplicating transitions ("door open while going up" + "door open while going down" would have identical behavior). The previous direction is remembered as an attribute, not a state.
2. Why two separate state machines instead of one combined diagram?
The door state machine operates at a different granularity than the direction state machine. Direction changes happen across multiple floors; door states change within a single floor stop. Combining them into one diagram would create a state explosion (MOVING_UP_DOOR_CLOSED, MOVING_UP_DOOR_OPENING, etc. — 4 × 4 = 16 states instead of 4 + 4 = 8). Separate diagrams with the door machine nested inside DOOR_OPEN keeps the design clean.
3. Why do MOVING_UP and MOVING_DOWN have self-transitions?
When the elevator arrives at a floor but doesn't need to stop (no pending requests for that floor), it doesn't change state — it stays in MOVING_UP or MOVING_DOWN. The self-transition makes this explicit: the arrivedAtFloor event fires at every floor, but the [!shouldStop()] guard keeps the elevator in the same state. Without the self-transition, it would appear that the elevator "skips" floors silently.
4. How does DOOR_OPEN decide the next direction?
This is where the scheduling algorithm connects to the state machine. At the next step, the elevator asks the scheduling strategy for its next stop, and closes its doors before it moves:
- Are there requests in my current direction? → Continue same direction
- No requests in current direction but requests in opposite? → Reverse (SCAN first sweeps on to the building's edge)
- No requests left? → It went idle at the stop itself, its doors closing at once
The scheduling algorithm (FCFS, SCAN, LOOK) determines WHICH requests to serve next, and the state machine executes the resulting direction. This separation of concerns is key — the state machine doesn't know the algorithm; it just follows the direction it's given.
5. Connection to the State Pattern (Fundamentals S12)
The State Pattern would make each state a concrete class — IdleState, MovingUpState, MovingDownState, DoorOpenState — with the Elevator as the Context, delegating to its current state object, as the Vending Machine (DP6) does. Our Code Implementation keeps the four states in the ElevatorState enum instead: move() and stop() set it, and each state's behavior is a few lines of those two methods. The Design Patterns tutorial weighs the two.