Skip to content
Tutorials Center
My learning

Topics

Lesson 17 of 50 ยท Modeling with UML

Sequence and Activity Diagrams

Learn UML sequence diagrams (lifelines, messages, alt and loop fragments) and activity diagrams (flows, decisions, swimlanes), with runnable stand-ins.

A class diagram shows what a system is made of. Two other diagrams show what it does. A sequence diagram shows how participants exchange messages in time order, which makes it the best way to explain one scenario such as "a customer checks out". An activity diagram shows a flow of steps, decisions and parallel work, which makes it the best way to explain a process such as "handle a refund".

Both are drawings of ideas, so the diagrams here are read-only. The runnable examples are JavaScript stand-ins: one replays a sequence of messages and checks it against a model of the participants, the other walks an activity flow and counts handoffs between roles. They imitate what modeling and simulation tools do; they are not UML tools.

Sequence diagrams

Parts of a sequence diagram

Participants Boxes across the top.
Lifelines Dashed lines going down. Time flows down.
Messages Arrows between lifelines, top to bottom.
Fragments Boxes for alt, loop and opt.

Read top to bottom. Higher means earlier.

1 / 2

Each participant (an object, a service, a person or a whole system) sits at the top of a vertical dashed lifeline. Time runs down the page. A message is an arrow from one lifeline to another:

  • A solid line with a filled head is a synchronous call: the sender waits for the answer.
  • A solid line with an open head is an asynchronous message: the sender carries on.
  • A dashed line is a reply (return value).

A thin activation bar on a lifeline shows when a participant is busy handling a call. A message to oneself (an arrow that loops back) shows internal work.

Combined fragments are boxes that add logic to the flow:

  • alt has alternatives separated by a dashed line, each with a guard in brackets, like if/else.
  • opt is an alternative with only one branch, like an if without an else.
  • loop repeats its content while a condition holds.
  • par shows parts that happen in parallel.
sequenceDiagram
    actor C as Customer
    participant W as Web shop
    participant P as Payment service
    C->>W: checkout(cart)
    loop for each item
        W->>W: reserve stock
    end
    W->>P: charge(total)
    alt payment approved
        P-->>W: approved
        W-->>C: order confirmation
    else payment declined
        P-->>W: declined
        W-->>C: show error
    end

Checkout: the shop reserves stock for each item, then asks the payment service to charge. An alt fragment shows the two outcomes.

Notice what the diagram does well: it shows who talks to whom and in what order, including the failure branch. It says nothing about data types or class structure, because that is a different question. A good sequence diagram tells one story. If you need to show five scenarios, draw five diagrams rather than one huge one.

Sequence diagrams are also excellent for design review. Walking through the arrows, a teammate quickly spots a missing reply, an unexpected dependency ("why does the web shop call the database directly?"), or a call chain that is too long.

A stand-in that replays message sequences

To make the idea concrete, this stand-in stores a sequence as a list of messages and a model of what each participant can do. It then replays the messages in order, printing each arrow, and reports any message that sends something the receiver does not offer. That check mirrors a real design rule: a sequence diagram and a class diagram must agree.

JS Replay a message sequence

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The last message asks PaymentService to refund, but the model lists only charge, so the replay flags it. In a real project this is the moment to decide: add the operation to the design, or fix the scenario. Either way, the two diagrams stop contradicting each other.

Activity diagrams

An activity diagram models a flow of work. It looks like a flowchart with a few extra symbols:

  • A filled circle is the start; a circle with a ring (a "bullseye") is the end.
  • A rounded rectangle is an action (one step).
  • A diamond is a decision, with guarded outgoing arrows such as [in stock] and [out of stock]; a diamond where flows rejoin is a merge.
  • A thick bar is a fork (start parallel flows) or a join (wait for all parallel flows).
  • Swimlanes (also called partitions) divide the diagram into columns or rows, one per actor or department, so you can see who does what.

Activity diagrams suit business processes, workflows and algorithms. They are also handy for describing one use case scenario step by step. Unlike a sequence diagram, they focus on the steps and decisions, not on messages between objects.

Mermaid has no activity diagram type, but a flowchart with subgraphs approximates one, including swimlanes:

flowchart TD
    start(("start")) --> request
    subgraph customer["Customer"]
        request["Request refund"]
        ship["Ship item back"]
    end
    subgraph support["Support"]
        review{"Within 30 days?"}
        reject["Send rejection"]
        approve["Approve refund"]
    end
    subgraph finance["Finance"]
        pay["Pay refund"]
    end
    request --> review
    review -- "no" --> reject
    review -- "yes" --> approve
    approve --> ship
    ship --> pay
    reject --> done(("end"))
    pay --> done

A refund process with three swimlanes. The lane crossings show handoffs between roles.

The lanes make an important thing visible: every arrow that crosses a lane boundary is a handoff, and handoffs are where delays and misunderstandings creep in. A process with eleven handoffs between three departments is a candidate for simplification, whatever its steps look like.

A stand-in that walks an activity flow

This stand-in stores the refund process as data (each step has a lane and the next step; decisions choose by a condition). It walks the flow for a given case and counts the lane handoffs on the path it took. It is a toy executor, similar in spirit to how workflow engines follow a process definition.

JS Walk the refund flow and count handoffs

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The accepted refund crosses lanes three times (Customer to Support, Support back to Customer, then Customer to Finance), while the rejected case crosses only once. Counting them shows how much coordination each case needs, and which case to make simpler first.

Which diagram when?

Question Diagram
In what order do these participants talk in one scenario? Sequence
What are the steps and decisions of a process, and who does each? Activity with swimlanes
Who uses the system and why? Use case
What types exist and how are they related? Class

Using both together is common: an activity diagram for the overall process, then a sequence diagram for the technically tricky step inside it.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In a sequence diagram, what does the vertical direction represent?

    Lifelines run downward and time flows with them, so a message lower on the page happens later.
  2. 2. Which fragment shows an if/else choice between two outcomes?

    alt has alternatives with guards, like if/else. opt has only one branch, loop repeats and par runs parts in parallel.
  3. 3. What do swimlanes in an activity diagram show?

    Each lane belongs to one role, so you can see who does what and where work is handed over.
  4. 4. What does a thick bar with several outgoing flows represent?

    A fork starts parallel flows; a join waits for all of them. A decision is a diamond.

Summary

  • A sequence diagram shows messages between participants in time order; fragments (alt, opt, loop, par) add branching, repetition and parallelism.
  • Draw one scenario per sequence diagram and keep it consistent with the class diagram's operations.
  • An activity diagram shows steps, decisions, forks and joins; swimlanes assign steps to roles.
  • Lane crossings are handoffs, a useful measure of how complex a process is.
  • Use sequence diagrams for how participants interact and activity diagrams for how work flows.

Next: State, Component and Deployment Diagrams.