What is a Sequence Diagram?
A Sequence Diagram shows how objects interact with each other in a specific scenario, arranged in time order (top to bottom). It's the most common interaction diagram and is excellent for visualizing the flow of a particular use case or algorithm.
Think of it as a movie script β it shows who does what, in what order, and what happens as a result. Each object is a "character" and the messages are their "lines."
Sequence Diagram Notation
Basic Elements:
1. Lifeline (dashed vertical line)
ββββββββββββββββββββββββββββββββββ
Represents an object existing over time.
βββββββ
β obj β
ββββ¬βββ
β
β β lifeline (dashed line)
β
β
2. Activation Bar (thin rectangle on lifeline)
βββββββββββββββββββββββββββββββββββββββββββββ
Shows when an object is actively processing.
βββββββ
β obj β
ββββ¬βββ
β
β ββββ
β β β β activation bar
β β β
β ββββ
β
3. Message Types:
ββββββββββββββ
βββββββββΆ Synchronous call (object waits for response)
- - - - -βΆ Asynchronous call (object continues immediately)
βββββββββΆ Return message (dashed arrow, optional)
4. Combined Fragment:
ββββββββββββββββββ
βββββββββββββββββββββββ
β alt / opt / loop β β Special constructs
β β
βββββββββββββββββββββββ
Reading a Sequence Diagram
Let's walk through a simple example β a user logging into a system:
βββββββββ ββββββββββββ ββββββββββββ ββββββββββββ
β User β βLoginForm β β Auth β β Database β
βββββ¬ββββ ββββββ¬ββββββ ββββββ¬ββββββ ββββββ¬ββββββ
β β β β
β 1. enter β β β
β credentials β β β
βββββββββββββββββΆβ β β
β β β β
β β 2. validate() β β
β ββββββββββββββββββββΆβ β
β β β β
β β β 3. findUser() β
β β ββββββββββββββββββββΆβ
β β β β
β β β 4. user data β
β β βββββββββββββββββββββ
β β β β
β β 5. valid/invalid β β
β βββββββββββββββββββββ β
β β β β
β 6. success/ β β β
β error message β β β
ββββββββββββββββββ β β
β β β β
The diagram reads top-to-bottom, left-to-right. The User enters credentials (1), the form validates them (2), the auth system queries the database (3-4), returns validation result (5), and the form shows success/error (6).
Combined Fragments
Combined fragments handle complex logic like conditions, loops, and alternatives:
alt (Alternative β if/else)
ββββββββββββββββββββββββββββββββββββββ
β alt β
β β
β [password valid] β
β ββββββββββββββββΆ β
β show dashboard β
β β
ββββββββββββββββββββββββββββββββββββββ€
β else β
β β
β [password invalid] β
β ββββββββββββββββΆ β
β show error β
β β
ββββββββββββββββββββββββββββββββββββββ
opt (Optional β if condition is true)
ββββββββββββββββββββββββββββββββββββββ
β opt [rememberMe checked] β
β β
β save token to cookie β
β β
ββββββββββββββββββββββββββββββββββββββ
loop (Loop β repeat)
ββββββββββββββββββββββββββββββββββββββ
β loop [for each item in cart] β
β β
β calculate item total β
β add to grand total β
β β
ββββββββββββββββββββββββββββββββββββββ
ref (Reference β reuse another diagram)
ββββββββββββββββββββββββββββββββββββββ
β ref β
β Login Use Case β
ββββββββββββββββββββββββββββββββββββββ
Complex Example: Online Shopping
ββββββββββββ ββββββββββββ ββββββββββββ ββββββββββββ ββββββββββββ
β Customer β β Cart β β Payment β β Inventoryβ β Email β
ββββββ¬ββββββ ββββββ¬ββββββ ββββββ¬ββββββ ββββββ¬ββββββ ββββββ¬ββββββ
β β β β β
β checkout() β β β β
ββββββββββββββββΆβ β β β
β β β β β
β β getTotal() β β β
β ββββββββββββββββΆβ β β
β β β β β
β β total β β β
β βββββββββββββββββ β β
β β β β β
β β processPayment() β β
β ββββββββββββββββββββββββββββββββΆβ β
β β β β β
β β ββββββββββββββββββββββββββββββββββββββββββ β
β β β alt [payment successful] β β
β β β β β
β β β charge card β β
β β β decreaseStock() β β
β β βββββββββββββββββββββββββββββββββββββββββΆβ β
β β β β β
β β β stock updated β β
β β ββββββββββββββββββββββββββββββββββββββββββ β
β β β β β
β β β sendConfirmation() β β
β β βββββββββββββββββββββββββββββββββββββββββββββββββββΆβ
β β β β β
β β β confirmation sent β β
β β ββββββββββββββββββββββββββββββββββββββββββββββββββββ
β β β β β
β β ββββββββββββββββββββββββββββββββββββββββββ€ β
β β β else [payment failed] β β
β β β β β
β β β show error β β
β β β β β
β β ββββββββββββββββββββββββββββββββββββββββββ β
β β β β β
β orderComplete β β β β
βββββββββββββββββ β β β
β β β β β
When to Use Sequence Diagrams
Use Sequence Diagrams When:
β You want to show the step-by-step flow of a specific scenario
β You need to understand the order of method calls
β You're documenting a complex algorithm or workflow
β You need to identify which objects participate in a use case
β You're debugging interaction issues between components
Sequence Diagrams Are Less Useful When:
β You need to show the overall system structure (use Class Diagram)
β You want to show many scenarios at once (gets too cluttered)
β You need to show concurrent activities (use Activity Diagram)
β You want to show state changes (use State Diagram)