Course map

Topic 046 / Core

Sequence and state diagrams, state machines

Chapter 4.2 · Modeling and Communicating Design

What and why

C4 (→ 45) fixes the zoom level; it says nothing about how an interaction unfolds over time or how one entity's lifecycle is constrained. A sequence diagram shows the calls between participants in time order, for one specific interaction. A state diagram shows every legal state of one entity and the transitions between them. A state machine turns that diagram into enforced code: a fixed set of states, a fixed set of legal transitions, and a rejection — not a silent no-op — for any other transition. Builds on: → 45.

Diagram

Sequence — "user places an order"             State machine — order entity
───────────────────────────────────           ────────────────────────────
Client → API: place order                      placed ──► paid ──► fulfilled ──► delivered
API → OrderModule: create order                  │          │
OrderModule → PaymentModule: charge               └────► cancelled ◄────┘ (from placed or paid)
PaymentModule → OrderModule: charged                                 refunded (from paid, after cancel)
OrderModule → InventoryModule: reserve stock
OrderModule → API: order confirmed              Illegal: placed → delivered directly —
API → Client: 201 Created                       no transition skips a state.

How it works

  1. For a sequence diagram, draw one lifeline per participant and one arrow per call, including only the calls relevant to the single question being answered.
  2. For a state diagram, draw one node per legal state of the entity and one edge per transition; every edge should map to exactly one method or event in the code.
  3. Turn the diagram into a state machine: a table or switch of (current_state, event) → next_state, where any combination not in the table is rejected, not ignored.
  4. Attach guards to transitions where needed (e.g. delivered is reachable only from fulfilled, never directly from placed) — the guard lives with the transition, not scattered across callers.
  5. Cross-check: trace the transition against the sequence diagram of the operation that triggers it, to confirm the triggering event actually occurs at that point in the flow.

Practical example

(illustrative). The order entity's states — placed, paid, fulfilled, delivered, cancelled, refunded — and its legal transitions (placed → paid → fulfilled → delivered; placed or paid → cancelled; paid-then-cancelled → refunded) are exactly the state machine Project 6 enforces in its domain model, and the payment entity's own authorize/capture/refund states are the state machine Project 8 enforces for payments.

When to use it

  • Use when: an entity has more than two states, or any bug has ever come from an illegal transition slipping through.
  • Avoid when: the entity is a true two-state flag (is_active) — a boolean and one guard clause need no diagram or state-machine abstraction.
  • Failure scenarios: a transition enforced only in a UI form, so a direct API or database call skips the guard entirely and the entity reaches an impossible state; a sequence diagram drawn once and never checked against the code it was supposed to describe.
  • Simpler alternative: for a two-state flag, skip the state machine; add one only once a third state or a disputed transition actually appears.

Practice

(20–30 min; no lab services — any language; merges this chapter's state- machine and sequence-diagram exercise). Draw the sequence diagram for "user places an order" (client, API, order module, payment, inventory); then implement the order state machine with legal transitions only, attempt an illegal transition (placed → delivered directly), and confirm it is rejected rather than silently applied (predict-then-verify). Done when the illegal transition raises an error instead of succeeding.

Key takeaway

Sequence diagrams capture one interaction over time; state diagrams and their enforced state machines capture every legal shape of one entity's lifecycle. Done when you can: name, for any lifecycle-bearing entity in your system, its full set of states and point to the code that rejects an illegal transition. Used next in → 55 (aggregates enforce these same transitions from inside the entity), → 71 (concurrent updates to a state-bearing entity); Project 6.

References

None — this topic makes no external claims.

Source directory

Search the guide