State Machine Diagram

The UML picture of one object over its lifetime: the states it can be in, the events that move it, and the guards that decide whether a move is allowed.

Overview

A state machine diagram describes one object, not a process or a conversation. It lists the states the object can be in, the events that move it from one state to another, and the conditions under which each move is allowed. An order is Created, then Paid, then Shipped, then Delivered; it cannot go from Created to Shipped, and it can be Canceled only before it ships. Those sentences are a state machine, and the diagram is the exact way to write them down.

The notation is in clause 14 of UML 2.5.1. A state is a rectangle with rounded corners. The initial pseudostate is a small solid circle; the final state is a small solid circle inside a larger circle. A transition is an arrow labeled trigger [guard] / effect: the event that fires it, a condition in square brackets that must be true, and an action that runs during the move. A state may also name behavior that runs on entry, on exit, or while it is active (entry, exit and do).

In LLD interviews, state machines are the tool for anything with a lifecycle: orders, payments, elevators, vending machines, connections, documents under review. Drawing the machine first makes the invalid transitions explicit, and those are exactly the bugs an interviewer will probe for.

The parts

Each element has a fixed drawing and a fixed meaning. The first five appear in almost every machine.

  • State: a rounded rectangle with the name. It is the object's condition between events.
  • Initial pseudostate: a small solid circle. It marks where the machine starts.
  • Final state: a solid circle inside a circle. The object's life is over.
  • Transition: an arrow labeled trigger [guard] / effect, which gives the event, the condition and the action.
  • Entry, exit and do: lines inside the state, written entry / action, exit / action and do / activity. They tie behavior to the state rather than to a transition.

The rest appear when a machine grows.

  • Composite state: a state that contains its own smaller machine. It groups states that share transitions.
  • Choice: a diamond. It branches on a condition evaluated during the transition.
  • Fork and join: short heavy bars. They enter or leave parallel regions together.
  • History: a small circle with H (shallow) or H* (deep). It re-enters a composite state where you left it.
  • Terminate: a cross. The machine stops without reaching a final state.

Interview questions

  • What are composite states, and when do you use them?
  • Design the state machine for an order, including cancellation and refunds.
  • How do you implement a state machine in code?
  • How do timeouts and other time-based events fit a state machine?
  • How does a state machine diagram help with testing?

Sign up free to read the full topic

More Low-Level Design topics

Start free on Cariara · Coding problems · System design studio