State Machine Diagram
A state-machine diagram expresses legal lifecycle states and the events that transition between them. A guard restricts a transition; a status field alone does not enforce the model.
What you’ll understand
- A state-machine diagram expresses legal lifecycle states and the events that transition between them. A guard restricts a transition; a status field alone does not enforce the model.
- Render explicit state nodes with event-labeled transitions and compare the recorded event sequence against them.
- Evaluate the tradeoff: A small table works well for a simple lifecycle. Independent state dimensions can cause a combinatorial explosion; separate them when they evolve independently.
Useful first: Classes and Objects
The idea and when to recognize it
A state-machine diagram expresses legal lifecycle states and the events that transition between them. A guard restricts a transition; a status field alone does not enforce the model.
- Requirement: An order should move from new to paid to shipped, rejecting events that do not fit its current state.
- Change to anticipate: Review legal transitions and verify that invalid events leave the order unchanged.
- Design goal: Render explicit state nodes with event-labeled transitions and compare the recorded event sequence against them.
Start with the concrete problem
An order should move from new to paid to shipped, rejecting events that do not fit its current state.
Review legal transitions and verify that invalid events leave the order unchanged.
| Direct implementation | Revised design |
|---|---|
| Connecting every state to every other state hides the business rules the diagram is supposed to explain. | Render explicit state nodes with event-labeled transitions and compare the recorded event sequence against them. |
| The baseline case can hide the problem. Compare the extension and boundary fixtures before accepting this design. | A small table works well for a simple lifecycle. Independent state dimensions can cause a combinatorial explosion; separate them when they evolve independently. |
Responsibilities and contracts
Render explicit state nodes with event-labeled transitions and compare the recorded event sequence against them.
- Order: Owns current lifecycle state. Public operations: handle(event)
- New: Allows payment. Public operations: pay → paid
- Paid: Allows shipment. Public operations: ship → shipped
- Shipped: Terminal state. Public operations: reject further changes
Read the design
Select a participant to inspect its responsibility and public operations. Follow the labeled relationships; an arrow indicates direction, while the legend distinguishes ownership, substitution, and temporary use.
Preparing the design walkthrough…
Follow the interaction
Choose a supplied workload and compare the direct and revised implementations. Step through the same operation in both languages. Object values are recorded after each highlighted statement; previous steps remain unchanged.
Preparing the design walkthrough…
Implementation and expected output
Read the complete example alongside its input and expected output. The implementation selector changes the displayed program; the language selector preserves your current scenario and playback position.
Preparing the design walkthrough…
Try the idea in another setting
A reservation can move through held, confirmed, expired, and canceled states with time-based guards.
- Name the new client contract before adapting the example.
- Identify which state belongs to each object and who controls its lifetime.
- Write one normal case and one boundary case before extending the design.
Tradeoffs and interview takeaways
A small table works well for a simple lifecycle. Independent state dimensions can cause a combinatorial explosion; separate them when they evolve independently.
Common mistake: Connecting every state to every other state hides the business rules the diagram is supposed to explain.
- Explain the choice: Render explicit state nodes with event-labeled transitions and compare the recorded event sequence against them.
- Challenge your choice: A small table works well for a simple lifecycle. Independent state dimensions can cause a combinatorial explosion; separate them when they evolve independently.
- Describe a requirement that would make the simpler implementation sufficient.
Design challenge and knowledge check
Preparing the design walkthrough…