Skip to content
Tutorials Center
My learning

Topics

Lesson 48 of 57 ยท Low-Level Design Projects

Project: Elevator System

Design an elevator system with the State pattern for each car, a scheduling strategy for dispatching, a request queue and an observer for floor displays, in runnable Node.js.

An elevator looks simple until you try to model it. A car moves up and down, opens doors, remembers which buttons were pressed, and must decide what to do next when it is idle, when it is full of requests, or when someone presses a button behind it. With several cars, a second question appears: which one should answer a call from floor 6? That is why the elevator system is a favorite low-level design problem. It combines at least three patterns in a natural way, and none of them is added for show.

In this project you follow the process from How to Approach a Low-Level Design Problem. You use State for the car's behavior, Strategy for choosing a car, a request queue in a controller, and Observer for the floor displays.

Who does what

Elevator One car with a floor, a set of stops and a state.
Controller Owns the request queue and picks a car for each call.
Displays Watch cars and show floor and direction.
1 / 2

Step 1: requirements

  • A building with floors numbered from 0 and several elevator cars.
  • A person on a floor makes a hall call (floor and desired direction). A person inside presses a destination button.
  • A car stops at requested floors. While moving, it keeps its direction as long as there are stops ahead, then reverses. This is the well-known SCAN policy (also called the "elevator algorithm"), which avoids zig-zagging.
  • Doors open at each stop for one tick, then close.
  • Displays show a car's current floor and direction.
  • Out of scope: door sensors, weight limits, emergency mode, speed and acceleration. Time is modeled as discrete ticks: on each tick, a car moves one floor or does one step of door handling.

Step 2: entities and states

Nouns: elevator, request, floor, controller, display. Behavior depends on what the car is doing, which is a clear sign of State. The car has four states:

stateDiagram-v2
    [*] --> Idle
    Idle --> MovingUp : stop above
    Idle --> MovingDown : stop below
    Idle --> DoorsOpen : stop on this floor
    MovingUp --> DoorsOpen : reached a stop
    MovingDown --> DoorsOpen : reached a stop
    DoorsOpen --> MovingUp : more stops above
    DoorsOpen --> MovingDown : more stops below
    DoorsOpen --> Idle : nothing left to do

The life of one car. Each arrow is a decision made by the state object itself.

The elevator (the context) stores the data: its floor and a set of pending stops. Each state class decides what a tick means: MovingUp goes up one floor and checks whether this is a stop, DoorsOpen decides where to go next. There is no if (state === "movingUp") anywhere, which is the benefit described in the State lesson.

The elevator also keeps a list of listeners. Whenever it passes a floor or changes state it sends an event to them, and any number of displays can subscribe. This is Observer.

Step 3: the code for one car

The Controller and the strategies are included in this first example too, because the full model is built from the same pieces. Here the demo uses only the car and a display. While the car goes up to floor 5, someone on floor 1 presses a button. The car does not turn around mid-trip. It finishes its upward stops first and then goes back down, exactly what SCAN specifies.

Node.js One elevator car with states and a display

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Read the event log from the top. The first tick only changes the state (Idle to MovingUp) without moving. Then the car climbs one floor per tick, stops at 3 and 5, reverses and serves floor 1 on the way down. The new stop at floor 1 was added at tick 3 while the car was moving up, yet the car kept its direction, which is the SCAN policy in pickDirection. The whole trip took 13 ticks.

Step 4: dispatching with a strategy

With several cars, the controller has to decide which one answers a hall call. The choice is isolated behind a choose(elevators, call) method, so a policy is one small class. The model has two:

  • NearestElevator picks the car with the smallest distance to the call floor.
  • DirectionAware also counts how many stops a car already has and adds a penalty when the car is moving away from the call, because distance alone is misleading: a car one floor away but heading the other way is a poor choice.

Real systems have more policies (zoning, energy saving, peak-hour rules), and this is exactly why the choice is a strategy. The controller also holds a request queue: calls are queued as they arrive and assigned at the start of each tick. Queueing separates "someone pressed a button" from "we decided which car goes", so assignment logic can run in one place and in order.

Step 5: comparing strategies

The next example runs one scenario twice, once with each strategy: two cars (E1 at floor 0, E2 at floor 9) and five hall calls arriving over the first ticks. It measures how long each caller waits until a car opens its doors on their floor. Treat it as a demonstration of how to measure a policy, not as proof that one is better in general. Real evaluation uses many random scenarios.

Node.js Comparing two dispatch strategies

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

With these calls the direction-aware strategy has a slightly lower average wait (3.20 ticks against 3.40) because the last caller is served one tick sooner. The point is not the size of the difference. It is that, because the policy is a separate object, you could test ten policies on the same recorded scenario without touching the car or the controller.

Design notes and trade-offs

  • State objects or an enum? Here each state has real logic (MovingUp changes the floor, DoorsOpen picks a direction), so objects pay off. If the states only differed in allowed transitions, a table would be enough (see State).
  • A set of stops. A Set ignores duplicate presses of the same button. A more detailed model stores the direction with each hall call, so an elevator going up does not pick up someone who wants to go down on the way.
  • Who owns the queue? The controller. If each car had its own queue, nobody could rebalance.
  • Testing. Time is discrete ticks, so tests need no clock or sleeps. Call tick() N times and assert on the floor and state.
  • Concurrency. In a real building each car runs its own control loop and the controller receives calls from many panels. The queue (and any shared stop set) must be protected: a lock, a thread-safe queue, or a single thread that owns all state and receives messages. The parking-lot lesson shows how a check-then-act race arises (Project: Parking Lot).
  • Extensions. Maintenance mode is a new state. Weight limits are a guard in DoorsOpen. Express elevators are a new strategy plus a restriction on which floors a car may stop at.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why is State a good fit for an elevator car?

    Behavior that depends on the current status, with transitions between statuses, is exactly what State is for.
  2. 2. What does the SCAN policy do?

    Continuing in one direction avoids zig-zagging and keeps waiting time reasonable for everyone.
  3. 3. Why is choosing which car answers a call a Strategy?

    Dispatch policies get tuned and replaced. A shared choose() interface keeps the controller unchanged.
  4. 4. What benefit does Observer give the display panels?

    The elevator publishes events and does not know who listens, so displays and loggers can be added freely.
  5. 5. Why does the controller keep a request queue?

    A queue decouples input from decisions. It is also the natural place to add rules later, such as batching or priorities.

Summary

  • The car is the context of a State pattern: Idle, MovingUp, MovingDown and DoorsOpen each decide what a tick means and what comes next.
  • SCAN keeps the direction while stops remain ahead, which avoids zig-zagging.
  • The controller owns a request queue and delegates "which car?" to a Strategy such as nearest or direction-aware.
  • Displays subscribe as observers, so the car never knows who is watching.
  • Discrete ticks make the model deterministic and easy to test; real systems add locking around shared queues.

Next lesson: Project: Library Management System.