1. Introduction
- Sequence diagram is a very important kind of diagram in programming flow description. Usually the tools used to draw sequence diagrams are StartUml, Astah, …. I will introduce to you another tool to draw sequences is PlantUML.
2. Let’s get started!
a. Basic
- The “->” symbol is used to draw a message between two participants. Participants do not need to be explicitly declared.
- Use the -> symbol to represent an object’s response message to another object. eg:
1 2 3 4 5 6 7 8 | @startuml Alice -> Bob: Authentication Request Bob --> Alice: Authentication Response Alice -> Bob: Another authentication Request Alice <-- Bob: Another authentication Response @enduml |
Result:
b. Object declaration
If the keyword “participant” is used to declare an object. The order in which objects are declared will be the order in which they are displayed. Using other keywords to declare the audience will change according to the declared keyword:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | @startuml participant participant as Foo actor actor as Foo1 boundary boundary as Foo2 control control as Foo3 entity entity as Foo4 database database as Foo5 collections collections as Foo6 queue queue as Foo7 Foo -> Foo1 : To actor Foo -> Foo2 : To boundary Foo -> Foo3 : To control Foo -> Foo4 : To entity Foo -> Foo5 : To database Foo -> Foo6 : To collections Foo -> Foo7: To queue @enduml |
Result:
You can also rename the declared object with the keyword: as. Change the background color of the object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | @startuml actor Bob #red ' The only difference between actor 'and participant is the drawing participant Alice participant "I have a reallynlong name" as L #99FF99 /' You can also declare: participant L as "I have a reallynlong name" #99FF99 '/ Alice->Bob: Authentication Request Bob->Alice: Authentication Response Bob->L: Log transaction @enduml |
Result:
You can use the order keyword to customize the display order of your objects.
1 2 3 4 5 6 | @startuml participant Last order 30 participant Middle order 20 participant First order 10 @enduml |
result:
You can use double quotes to define the object. And you can use the as keyword to give those objects an alias.
1 2 3 4 5 6 7 8 | @startuml Alice -> "Bob()" : Hello "Bob()" -> "This is verynlong" as Long ' You can also declare: ' "Bob()" -> Long as "This is verynlong" Long --> "Bob()" : ok @enduml |
Result:
c. Message to Self
The object can send the message to itself.
1 2 3 4 | @startuml Alice->Alice: This is a signal to self.nIt also demonstratesnmultiline ntext @enduml |
Result:
d. Text alignment
You can place the response message’s text below the arrow, using the command skinparam responseMessageBelowArrow true.
1 2 3 4 5 6 | @startuml skinparam responseMessageBelowArrow true Bob -> Alice : hello Alice -> Bob : ok @enduml |
Result:
e. Change the arrow style
You can change the arrow style in a number of ways: Add a last x to indicate a lost message Use or / instead of <or> to only have the bottom or upper part of a Repeat arrow arrowhead (for example, head >> or //) for a thin drawing Use – instead – to have a dotted arrow Add the last “o” at the beginning of the arrow Use the two-dimensional arrow < ->
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | @startuml Bob ->x Alice Bob -> Alice Bob ->> Alice Bob - Alice Bob \- Alice Bob //-- Alice Bob ->o Alice Bob o\-- Alice Bob <-> Alice Bob <->o Alice @enduml |
Result:
f. Change arrow color
You can change the color of individual arrows by using symbols.
1 2 3 4 5 | @startuml Bob -[#red]> Alice : hello Alice -[#0000FF]->Bob : ok @enduml |
Result:
Conclusion
Today, I would like to stop here. The next content I would like to share in the next section.