Home
7UML Sequence & Communication Diagrams
7

Topic 7 of 9 · Lab VIII

UML Sequence & Communication Diagrams

Lifelines, activations and the eight message types; guards and combined fragments (alt, opt, loop); turning a use case into a sequence diagram; and the equivalent communication diagram.

← → move between slides · F full screen · M slide list

Before we start

Why this matters

Your use case says “the customer withdraws cash”. But who checks the PIN? Who knows the balance? Who dispenses the money — and in what order?

Sequence diagrams answer those questions one message at a time, and every message you draw becomes an operation some class must provide. Communication diagrams show the same story as a network of objects.

Together they connect the user’s view (use cases) with the structure of the code (class diagrams).

Outline · Lab VIII

Topics in this chapter

Select a topic to jump to the slide that explains it.

  1. Interaction diagrams
  2. Elements
  3. The ATM example
  4. Building a sequence diagram
  5. Communication diagrams
  6. Common mistakes

Concept 1 of 12

Interaction diagrams

A use case says what the user achieves. An interaction diagram shows how objects cooperate, message by message, to make it happen.

DiagramEmphasises
Sequencethe time order of messages; time runs down the page
Communicationthe links between objects; order is shown by numbers
Timing, interaction overviewless common; not in this course
  • Model the logic behind a complex function or procedure.
  • Show the details of one use case scenario.
  • Visualise how messages and tasks move between components.
  • Find missing operations: every message becomes a method on the receiving class (topic 8).

Concept 2 of 12

Lifelines, actors and activations

  • An actor (stick figure) is outside the system, as in the use case diagram.
  • A lifeline is one participant: a box with its name, and a dashed line going down. Name it instanceName : ClassName, e.g. atm1 : ATM, or just :ATM for an unnamed instance. The name is underlined because it is an object.
  • An activation bar (thin rectangle) shows when the participant is busy handling a message.
  • A note (folded-corner box) adds a comment.
  • Time flows downward; horizontal position has no meaning.

Concept 2 of 12

Lifelines, actors and activations (cont.)

Two lifelines inside loop and opt fragments (explained on the next slides).

Elements

Message types

Every message type in one diagram.

Elements

Message types (cont.)

MessageDrawn asMeaning
Synchronoussolid line, filled arrowheadthe sender waits for the reply (most method calls)
Reply / returndashed line, open arrowheadthe answer to a synchronous call
Asynchronoussolid line, open arrowheadthe sender continues without waiting
SelfU-shaped arrow back to the same lifelinean object calls its own operation
Createdashed arrow to the new object’s box, «create»a new object is instantiated
Deletemessage ending in ✕ on the lifelinethe object is destroyed
Foundarrow from a filled dotsender unknown (e.g. a hardware interrupt)
Lostarrow to a filled dotreceiver unknown (e.g. a warning broadcast)

Elements

Guards and combined fragments

A guard is a condition in square brackets, e.g. [balance > 0]: the message is sent only if it is true. A combined fragment is a box around part of the diagram with an operator in the top-left corner:

OperatorMeaningLike
altalternatives: exactly one operand runs, chosen by guards; operands separated by a dashed lineif / else
optthe content runs only if the guard is trueif
looprepeat while the guard holdsfor / while
paroperands run in parallelthreads
refrefers to another sequence diagrama function call
breakleave the enclosing fragmentbreak
Use fragments instead of drawing two separate diagrams for success and failure. One alt keeps both paths together.

Concept 5 of 12

The ATM withdrawal as a sequence diagram

Customer, ATM, bank server and account cooperate to withdraw cash. The alt fragment holds the success and “insufficient funds” paths.

Concept 5 of 12

The ATM withdrawal as a sequence diagram (cont.)

A cleaner version of the manual’s Figure VIII.4. The manual repeats the verify–withdraw messages as 10.4–10.6 after an error; an alt (or a loop for retries) expresses this more clearly.

Interactive

Step through the messages

Concept 7 of 12

From a use case to a sequence diagram

  1. Pick one use case and one scenario (usually the main success scenario).
  2. List the participants: the actor, a boundary object (screen, ATM), a control object (the logic, e.g. BankServer or TransactionController) and entity objects (Account, Card).
  3. Place the actor on the left and participants in the order they first appear.
  4. Turn each step of the scenario into a message; name it like a method call with parameters: withdraw(amount).
  5. Add replies where a value comes back.
  6. Wrap alternatives in alt, optional parts in opt, repetition in loop.
  7. Check every message has a matching operation in the class diagram.
Boundary – control – entity: the actor talks only to the boundary; the boundary talks to the control; the control talks to entities. This keeps the diagram readable and the design clean.

Concept 8 of 12

Communication diagrams

A communication diagram (called a collaboration diagram in UML 1) shows the same interaction as a sequence diagram, but arranged as a network of objects. Lines are links; messages are written beside the links with a sequence number and a small arrow for direction.

  • Numbers give the order: 1, 2, 3 … ; nested calls use decimals: 8.1 happens inside 8.
  • Guards and loops can be written in the label: 2 [amount ≤ balance]: debit(amount), 3*: scan(book).
  • Good for seeing which objects must know each other — useful for the class diagram.

Concept 8 of 12

Communication diagrams (cont.)

The ATM withdrawal as a communication diagram.

Communication diagrams

Sequence or communication?

Sequence diagramCommunication diagram
Shows bestthe order of messages over timethe structure of links between objects
Ordertop to bottom — easy to readby sequence numbers — harder to follow
Alternatives and loopscombined fragments (alt, opt, loop)guards and * in labels
Spacewide with many objects, long with many messagescompact
Use it forexplaining a scenario step by stepchecking which classes need associations
Both show the same information and most tools convert one into the other. Your lab asks for both: draw the sequence diagram first, then derive the communication diagram from it.

Tools

Drawing tools

In Visual Paradigm (online or desktop): create a Sequence Diagram, add an Actor and Lifelines from the toolbar, then drag from a lifeline’s resource icon to create a message to another lifeline. Right-click a message to change its type; select several messages and choose Create combined fragment for alt, opt or loop.

Tools

Drawing tools (cont.)

StarUML, Lucidchart and draw.io work the same way. PlantUML lets you type it, and draws the diagram for you:

Tools

Drawing tools (cont.)

PlantUML source
@startuml
actor Customer
participant ":ATM" as ATM
participant ":BankServer" as Bank
Customer -> ATM : insertCard()
ATM -> Customer : requestPIN()
Customer -> ATM : enterPIN(pin)
ATM -> Bank : verifyPIN(card, pin)
Bank --> ATM : valid
alt amount <= balance
  Bank --> ATM : approved
  ATM -> Customer : cash, receipt
else insufficient funds
  ATM -> Customer : showError()
end
@enduml
Output · the diagram PlantUML draws
Sequence diagram drawn by PlantUML: Customer, ATM and BankServer exchanging insertCard, requestPIN, enterPIN and verifyPIN, then an alt fragment for approved or insufficient funds

Concept 11 of 12

Common mistakes

MistakeFix
The actor talks straight to the database or entity objectsgo through a boundary and control object
Messages written as sentences (“the ATM checks the PIN”)write operation calls: verifyPIN(card, pin)
No reply for a value that is used lateradd a dashed reply arrow
Two diagrams for success and failureone diagram with an alt fragment
Lifeline names like “ATM system”name objects and classes: :ATM
Messages that do not exist as operations in the class diagramadd the operation, or rename the message
Showing a whole system in one diagramone diagram per use case scenario

Your turn

Your turn: Library Management System

  1. Choose one use case from your topic-6 diagram, e.g. Borrow book or Return book.
  2. Write its main scenario as numbered steps.
  3. Decide the participants (actor, boundary, control, entities such as Member, Book, Loan).
  4. Turn each step into a message; add replies.
  5. Where does the scenario branch? (book already reserved, overdue fine) → alt or opt.
  6. Draw the communication diagram with the same numbers.
Keep the numbering identical in both diagrams — examiners check that they describe the same interaction.

Worked example 1 of 3

Worked example 1

Turn this scenario into messages: “The librarian scans the book. The system finds the loan, marks it returned, and, if it is late, calculates the fine and shows it.”
Show the solution
  1. Librarian → :LibrarySystem : scan(bookId)
  2. :LibrarySystem → :Loan : findActive(bookId), reply loan
  3. :LibrarySystem → :Loan : markReturned()
  4. opt [loan is overdue]: :LibrarySystem → :LibrarySystem : computeFine() (self), then :LibrarySystem → Librarian : showFine(amount)

Worked example 2 of 3

Worked example 2

Which fragment models “try the PIN up to three times”?
Show the solution
A loop fragment with a guard such as [attempts < 3 and PIN not valid], containing enterPIN and verifyPIN; after it, an opt [attempts = 3] with retainCard().

Worked example 3 of 3

Worked example 3

In a communication diagram you see “8: withdraw(acct, amount)” and “8.1: getBalance()”. What does 8.1 mean?
Show the solution
8.1 is sent during the handling of message 8: while processing withdraw, the receiver first calls getBalance(). Decimal numbers show nesting.

Practice

Practice (1 of 2)

Try each one on paper first, then check your answer.

1. How is a synchronous message drawn, and how does it differ from an asynchronous one?
Show the answer
Synchronous: solid line with a filled arrowhead; the sender waits for a reply. Asynchronous: solid line with an open arrowhead; the sender continues without waiting.
2. What does the name c1 : Customer on a lifeline mean?
Show the answer
An object named c1 of class Customer.
3. Which combined fragment would you use for “if the member has a fine, ask for payment”?
Show the answer
opt with the guard [fine > 0].

Practice

Practice (2 of 2)

Try each one on paper first, then check your answer.

4. Lab task hint: which objects might take part in “Reserve book” in the Library Management System?
Show the answer
For example the Member actor, a boundary (ReservationScreen), a control (LibrarySystem or ReservationController) and entities Book and Reservation. Your choice must match your class diagram.
5. Why must every message in a sequence diagram match an operation in the class diagram?
Show the answer
A message is a call to an operation of the receiving object; if the class has no such operation, the design is inconsistent and cannot be implemented as drawn.
6. Give one advantage of a communication diagram over a sequence diagram.
Show the answer
It shows at a glance which objects are linked to which, so it directly suggests associations in the class diagram, and it stays compact.

Check yourself

Quiz

Summary

Summary

  • Sequence diagrams show messages between lifelines in time order; communication diagrams show the same messages on links, numbered.
  • Lifelines are named instance : Class; activation bars show when an object is busy.
  • Synchronous = filled head; reply = dashed; asynchronous = open head; plus self, create, delete, found and lost.
  • Guards [condition] and fragments alt, opt, loop, par, ref model logic.
  • Build one diagram per use case scenario, via boundary, control and entity objects.
  • Every message must exist as an operation in the class diagram.