Sequence, state and activity diagrams
Show behavior over time, the life of one object, and the flow of a whole process.
What you will learn
- 01Structure is not behavior
- 02Sequence diagrams: who talks to whom, in order
- 03State diagrams: the life of one object
- 04Activity diagrams: the flow of a process
- 05Object diagrams
- 06Choosing the diagram
Structure is not behavior
A class diagram shows what exists. It does not show what happens, in what order, or how an object changes over its life. Three more diagrams cover that. Use whichever answers the question in front of you.
Sequence diagrams: who talks to whom, in order
A sequence diagram shows objects as vertical lifelines and messages as horizontal arrows, with time running downward. It answers: for this use case, which calls happen and in what order?
For Pay and exit: the driver gives the ticket to the gate, the gate asks the lot for the fee, and the lot asks the payment service to charge. The reply travels back along the same path.
Swipe sideways to pan · drag a box to move it
Swipe sideways to pan · drag a box to move it
Swipe sideways to pan · drag a box to move it
Useful notation: a solid arrow is a call, a dashed arrow is a return, and boxes labeled alt and loop wrap alternatives and repetition (for example, alt payment fails, retry). Sequence diagrams expose design problems quickly. A lifeline with a flood of arrows is doing too much, and a long chain of calls suggests a Demeter violation.
State diagrams: the life of one object
Some objects behave differently depending on what has already happened. A state diagram (a state machine) shows the states an object can be in, the events that move it, and optional guards, conditions that must hold.
A ticket is issued, becomes paid once payment succeeds, and is exited when the gate opens. A refund can return it from paid to issued.
Swipe sideways to pan · drag a box to move it
Swipe sideways to pan · drag a box to move it
Swipe sideways to pan · drag a box to move it
The value is in the missing arrows. There is no path from issued straight to exited, which states the rule that nobody leaves without paying. If you find yourself writing many boolean flags such as isPaid and isExited, you probably have a state machine hiding in the code. The State pattern, coming soon, implements it cleanly.
Activity diagrams: the flow of a process
An activity diagram is a flowchart with extras. It has actions, decision diamonds with guard labels, and fork and join bars for steps that happen in parallel. It suits business processes and algorithms: Pay and exit might fork into "open gate" and "print receipt" and join afterwards.
Use it when the question is about the process as a whole, especially where branches and parallel work matter.
Object diagrams
An object diagram is a snapshot of real instances at one moment, such as lot1 holding ticket42 for plate KA01AB1234. It is handy to check that a class diagram can actually represent a tricky example.
Choosing the diagram
- What exists and how is it related? Class diagram.
- Who calls whom, in what order? Sequence diagram.
- How does one object change over time? State diagram.
- What is the overall flow, with branches and parallel work? Activity diagram.
- Does my structure fit a concrete case? Object diagram.
Draw fewer, better diagrams. One clear sequence diagram of the hardest use case teaches more than ten complete ones.