Skip to main content

Class Diagram

Editorial

Purpose of the Class Diagram

The class diagram is the structural blueprint of our Elevator System. It answers:

  • What classes exist and what are their responsibilities?
  • What attributes and methods does each class have?
  • How do the classes relate to each other (composition, association, inheritance, implementation)?

This is the most important diagram for the Elevator System because it captures the full design: the elevator cars, the request model, the dispatcher, the scheduling strategies, and the observer-based display system. Every class we draw derives directly from a requirement (R1–R10) or a state machine state.

Identifying Classes from Requirements

Using the noun/verb extraction technique from Fundamentals S8, we analyze our requirements:

Key Nouns → Candidate Classes:

RequirementNouns FoundCandidate Class
R1: "N floors, M elevator cars"building, floors, elevator, carsElevatorSystem, Elevator, Floor
R2: "Hall calls from any floor"hall call, floor, UP/DOWN buttonRequest (with RequestType.HALL_CALL), HallButton
R3: "Cabin calls from inside"cabin call, destination, panelRequest (with RequestType.CABIN_CALL), ElevatorPanel
R4: "Dispatch to optimal elevator"dispatcher, optimalDispatcher
R6: "Direction: UP, DOWN, IDLE"directionDirection enum
R7: "Interchangeable scheduling algorithm"algorithm, FCFS, SCAN, LOOKSchedulingStrategy interface, concrete implementations
R8: "Door opens and closes"door, statesDoor, DoorStatus enum
R9: "Floor display panel"display, position, directionFloorDisplay

Key Verbs → Candidate Methods:

  • "make a hall call" → Floor.pressUp(), Floor.pressDown() — each presses its HallButton, which creates the Request
  • "make a cabin call" → ElevatorPanel.pressFloor(int)
  • "dispatch elevator" → Dispatcher.dispatch(Request)
  • "move to floor" → Elevator.move(SchedulingStrategy) — one floor per call, toward the next stop the strategy it is handed picks
  • "should stop at floor" → Elevator.shouldStop(int)
  • "open/close doors" → Door.open(), Door.close()
  • "update display" → FloorDisplay.onElevatorMoved(int, int, Direction) — the ElevatorObserver method the elevator calls with its id, floor and direction

Class Inventory

Here are all classes in our design, grouped by responsibility:

Enums (4):

  • Direction — UP, DOWN, IDLE
  • DoorStatus — CLOSED, OPENING, OPEN, CLOSING
  • ElevatorState — IDLE, MOVING_UP, MOVING_DOWN, DOOR_OPEN
  • RequestType — HALL_CALL, CABIN_CALL

Core Domain (4):

  • Request — a floor request with source floor, destination, direction, and type
  • Elevator — an elevator car with current floor, direction, door, and request queue
  • Door — the door mechanism with status and open/close behavior
  • Floor — a building floor with hall buttons and a display

Control (2):

  • ElevatorSystem — the top-level class that composes elevators, floors, and the dispatcher
  • Dispatcher — receives hall calls and assigns each to the best elevator using the scheduling strategy (a cabin call goes straight to its own car)

Strategy (4):

  • SchedulingStrategy — interface defining selectElevator(Request, List<Elevator>) and getNextStop(Elevator)
  • FCFSStrategy — First Come First Served: assigns to the idle elevator with the fewest pending requests and sends each car to its oldest request
  • SCANStrategy — Elevator algorithm: sweeps in its direction to the building's edge (minFloor, maxFloor), serving the requests on the way, then reverses
  • LOOKStrategy — Like SCAN but reverses at the last request instead of the building edge

Observer (2):

  • ElevatorObserver — the interface the elevator notifies: onElevatorMoved(elevatorId, floor, direction)
  • FloorDisplay — displays current elevator positions and directions; implements ElevatorObserver

UI Components (2):

  • HallButton — the UP/DOWN button on each floor
  • ElevatorPanel — the button panel inside each elevator with floor buttons

Total: 18 classes, interfaces and enums (4 enums, 2 interfaces, 12 classes) — appropriate for a Medium difficulty problem (6–12 main classes + supporting enums/interfaces).

Defining Relationships

Now let's determine how these classes connect, with justification for each relationship type:

Composition (◆ — part cannot exist without the whole):

  • ElevatorSystem ◆→ Elevator [1..*] — Elevators are part of the system. Destroying the system destroys its elevators.
  • ElevatorSystem ◆→ Floor [1..*] — Floors are part of the building structure.
  • ElevatorSystem ◆→ Dispatcher [1] — The dispatcher is an internal component of the system.
  • Elevator ◆→ Door [1] — Every elevator has exactly one door. The door doesn't exist independently.
  • Elevator ◆→ ElevatorPanel [1] — The control panel is physically inside the elevator.
  • Floor ◆→ HallButton [1..2] — Each floor has 1–2 hall buttons (UP only on ground, DOWN only on top, both on middle floors).
  • Floor ◆→ FloorDisplay [1] — Each floor has a display panel.

Association (── reference, independent lifecycle):

  • Dispatcher ── SchedulingStrategy — The dispatcher uses a strategy but the strategy can be swapped. Neither owns the other.
  • Elevator ── Request [0..*] — An elevator holds a queue of requests. Requests can exist before assignment.
  • Dispatcher ── Elevator [1..*] — The dispatcher references all elevators to make dispatch decisions.

Implementation (⤑ dashed triangle):

  • FCFSStrategy ⤑ SchedulingStrategy
  • SCANStrategy ⤑ SchedulingStrategy
  • LOOKStrategy ⤑ SchedulingStrategy
  • FloorDisplay ⤑ ElevatorObserver — each floor's display implements the interface; ElevatorSystem registers every floor's display with every elevator.

Dependency (⇢ dashed arrow — uses another type without keeping it):

  • Elevator ⇢ SchedulingStrategy «uses» — move(strategy) asks the strategy for the next stop. The elevator is handed the strategy for that one call (ElevatorSystem.step() passes the dispatcher's) and keeps no reference to it.
  • HallButton ⇢ Request, ElevatorPanel ⇢ Request «creates» — pressing a button makes the Request; the button keeps no reference to it.
  • Elevator ⇢ Direction, Elevator ⇢ ElevatorState, Door ⇢ DoorStatus, Request ⇢ RequestType — the enums are the types of their fields.

Observer relationship:

  • Elevator ── ElevatorObserver [0..*] — Elevator notifies its observers when its position or direction changes: it keeps a list of ElevatorObserver references and calls onElevatorMoved() on each — an association with observer semantics, through the interface, so the elevator never names FloorDisplay.

Why Composition between ElevatorSystem and Elevator (not Aggregation)?
An elevator car is physically installed in the building's shaft — it cannot exist independently or be moved to another building. The elevator's lifecycle is bound to the system. This is hallmark composition: destroy the system, destroy the elevators.

Why Association between Dispatcher and SchedulingStrategy (not Composition)?
The scheduling strategy can be replaced at runtime without destroying the dispatcher. Multiple dispatchers could share the same strategy instance. The strategy exists independently of any dispatcher — it's a pluggable algorithm, not a physical part.

Build the Class Diagram

Build the diagram step by step, starting with the core entities and expanding outward to the strategy and observer components. Place each step's classes and predict each relationship the lesson gives a reason for — its kind, then how many — and read the reason. Then open any class in Explore to see its members, or run the Code Implementation's usage example in Objects live: the floors with their buttons and displays, the two elevators with their doors, panels and request queues, and the Request objects the cars and the dispatcher share.

Interactive: the Elevator System's complete class diagram, built and explored live, on the code's own classes. Build places five steps' classes in turn — the car (Elevator, its Door and ElevatorPanel, its enums), floors and requests (Floor, HallButton, Request), control (ElevatorSystem, Dispatcher), the scheduling strategy (SchedulingStrategy and FCFS, SCAN, LOOK), the observer (ElevatorObserver, FloorDisplay) — and asks the kind of each relationship the lesson gives a reason for (composition or association, composition or aggregation, association or dependency, realization or inheritance) and its multiplicity, then shows the lesson's reason; the rest draw themselves with a note on the notation. Explore unfolds any class into its UML box with every member and lists its relationships with their reasons. Objects live runs the usage example a step at a time in Python or Java: the floors with their lit hall buttons and what each display was told, the two elevators with their door, panel and request queue, the dispatcher with its strategy and pending hall calls, and the Request objects — each in one colour wherever it is held, so a hall call in a car's queue and in the dispatcher's pending set shows as one object; each step's statement, the calls it makes into the classes (repeats folded), and stdout. Five predictions: which car takes floor 7 ▼, how many displays a move notifies, whether floor 3 can call again after its stop, which object move() asks for the next stop, what floor 5's display shows at the end. Below 760px the diagram shows one step's classes at a time. Six missions.

Complete Class Diagram

UML class diagram of the Elevator System in the code's Java names. ElevatorSystem (floors, elevators, dispatcher; addHallCall, addCabinCall, step, the private hallCallServed, displayStatus and getters) composes one or more Floors (has), one or more Elevators (manages) and one Dispatcher (owns). The Dispatcher (elevators, strategy, pendingRequests; setStrategy, getStrategy, dispatch, markServed) is associated with the Elevators (schedules) and with a SchedulingStrategy (uses), the interface with selectElevator and getNextStop that FCFSStrategy, SCANStrategy (minFloor, maxFloor) and LOOKStrategy implement. Elevator (id, currentFloor, direction, state, door, requests, panel, observers; getters, addObserver, removeObserver, the private notifyObservers, addRequest, removeRequestForFloor, hasRequests, move(strategy), shouldStop, stop) composes one Door (status; getStatus, isClosed, open, close) and one ElevatorPanel (elevator, pressedFloors; pressFloor, clearFloor), serves zero or more Requests, notifies zero or more ElevatorObservers (the interface with onElevatorMoved that FloorDisplay implements) and uses the SchedulingStrategy it is handed. Floor (floorNumber, upButton, downButton, display; getFloorNumber, getDisplay, pressUp, pressDown, resetButton) composes one or two HallButtons (floor, direction, isPressed; press, reset, isPressed) and one FloorDisplay (floor, elevatorPositions, elevatorDirections; onElevatorMoved, display). HallButton and ElevatorPanel create Requests (sourceFloor, destinationFloor, direction, requestType, timestamp, with getters). Elevator uses the Direction and ElevatorState enums, Door the DoorStatus enum, Request the RequestType enum.

Key Design Decisions

1. Why a unified Request class instead of separate HallCall and CabinCall classes?

Both hall calls and cabin calls share the same core data: source floor, destination, and direction. The only difference is the type field (HALL_CALL vs CABIN_CALL) and how they're created (from a floor button vs from an elevator panel). A single Request class with a RequestType enum keeps the elevator's side clean — its request queue, shouldStop() and every strategy's getNextStop() handle Request objects uniformly; the dispatcher sees only hall calls. If we needed significantly different behavior, inheritance would be justified. Here, the difference is data-only, not behavioral.

2. Why does the Dispatcher hold a reference to SchedulingStrategy (not the ElevatorSystem)?

The dispatcher is the class that decides with the scheduling algorithm and swaps it (setStrategy()); the elevators only borrow it — each step, ElevatorSystem.step() hands the dispatcher's strategy to every Elevator.move(strategy) for its next stop, so one swap reaches every car. Putting the strategy reference on ElevatorSystem would force all strategy-related calls to go through the system — adding unnecessary indirection. The dispatcher directly uses the strategy to decide which elevator handles a request. This follows the Information Expert principle (Fundamentals S7): place behavior on the class with the most relevant data.

3. Why Composition between Elevator and Door but Association between Elevator and Request?

The door is physically part of the elevator — it's installed in the car and cannot exist independently. Destroying the elevator means destroying its door. This is composition.

Requests, however, exist independently — a request is created when a button is pressed, assigned to an elevator, and then removed when served. The request's lifecycle is not bound to the elevator's. This is association: the elevator references requests, but doesn't own them.

4. Why SchedulingStrategy has both selectElevator() and getNextStop()?

These are two distinct questions the strategy answers:

  • selectElevator(request, elevators) — WHICH elevator should handle a new hall call? (dispatch-time decision)
  • getNextStop(elevator) — WHERE should this elevator go next? (per-move decision)

FCFS answers these differently than SCAN. FCFS assigns to the idle elevator with the fewest pending requests and sends each car to its oldest request. SCAN assigns to the nearest elevator that is idle or already moving toward the call in its direction, and serves requests in sweep order. Both questions go through the strategy interface, keeping the algorithm fully encapsulated.

5. Why FloorDisplay as an Observer rather than polling?

A floor display must update when any elevator moves. With polling, every display would need to periodically check all elevators — wasteful when nothing has changed. With Observer (Fundamentals S12), the elevator notifies all displays when it moves or goes idle. The display is guaranteed to show current data with no polling delay. This also decouples the elevator from the display — the elevator doesn't know what's observing it; it just fires notifications.