Labs ICT
⭐ Pro Login

Sequence Diagrams

Modeling object interactions over time with sequence diagrams.

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)

πŸ§ͺ Quick Quiz

What does a sequence diagram primarily model?