Sequence Diagrams
This tutorial covers sequence diagrams. First, we cover how to use PlantUML to create these diagrams, followed by one small exercise to practice drawing these diagrams.
More information on using PlantUML can be found on the official PlantUML website.
More information on sequence diagrams in UML can be found in section 17.8 of the UML specification.
Creating a Participant and Lifetime
You can create a participant and the associated lifetime using the keyword participant
followed by the name of the participant.
You can also use other keywords such as actor
and database
to change the shape of the participant.
@startuml participant
participant Participant
actor Actor
database Database
@enduml
Messages
We distinguish different message types by the arrow type.
@startuml messages
' Define participants
participant Source
participant Target
' Simple message which can be either synchronous or asynchronous
Source ->> Target: Simple (sync or async)
Source <<-- Target: Simple return (optional)
' Synchronous
Source -> Target: Synchronous
Source <-- Target: Synchronous return
' Asynchronous (other variants exist)
Source -\ Target: Asynchronous
@enduml
More arrow variants of each message type exist, but try to use these ones for the assignments. Refer to the slides if you are confused.
Activation
Participants are activated for a specific period of time to show they are performing an action. Use the keywords activate
and deactivate
to display the activation on the diagram.
@startuml activation
participant A
participant B
participant C
A ->> B: do something
activate B
B ->> C: ask for help
activate C
C -->> B
deactivate C
B -->> A
deactivate B
@enduml
Opt - Loop - Alt
The conditional statement expresses an operation that is executed
only if a certain condition is true. You can use the keyword opt
to create a conditional statement.
To create a loop statement where you can repeatably execute a fragment of your system,
you can use the keyword loop
.
To create a group of alternative conditions you can use the keyword alt
.
When you have reached the end of the block close it with end
.
@startuml opt-loop-alt
participant "User" as user
participant "Website" as web
participant "Course Database" as course
participant "My Computer" as pc
opt request help
user ->> web: request_help
activate web
web -->> user: give_tutorial
deactivate web
end
loop for each subject in the course
user ->> web: get_course_description
activate web
web ->> course: get_description
activate course
course -->> web
deactivate course
web -->> user
deactivate web
end
alt print description document
user->Printer: get_print
activate Printer
Printer-->user: print_document
deactivate Printer
else download description document
user -> pc: download_document
activate pc
pc --> user: open_document
deactivate pc
end
@enduml
Exercises
1. ATM Cash Withdraw
Please create a Sequence diagram based on the following description:
After the customer inserts the debit card into the ATM and selects the option of “withdraw cash”, the ATM will ask the customer for the required withdrawal amount. The ATM will access the customer’s bank account and check the current balance. If the required amount is more than the current balance, the ATM will display an “insufficient balance” message to the customer and return to the main menu. Otherwise, the ATM will output the case, update the user’s balance, and print a receipt if the customer required it.