Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

UML sequence diagrams

Sequence diagrams are the second most commonly used UML diagrams after class diagrams. The sequence diagram represents the interaction relationship as a two-dimensional graph, with the longitudinal time axis extending down the vertical line. The horizontal axis represents each role in the collaboration, usually a Class object, with a dotted line representing each role’s lifeline, and a rectangular bar indicating whether the lifeline is active or not. Objects can send synchronous or asynchronous messages to each other.

Sequence diagrams may be more valuable for PlantUML than class diagrams

A synchronous message

@startuml
Alice -> Bob: Hi
Bob --> Alice: Hi

Alice -> Bob: Is this a pen?
Bob --> Alice: No! This is an apple!!
@enduml
Copy the code

  • Basic composition of sequence diagram:< role > < message type > < Role > : < message content >
  • In the message type->Represents synchronization messages
  • -->Dashed lines indicate a return message

Asynchronous messaging

@startuml
Alice ->> Bob: Hi
Alice ->> Bob: Is this a pen?
Alice ->> Bob: Is this a pen??
Alice ->> Bob: Is this a pen???
Alice ->> Bob: Is this a pen????

Bob -> Alice: This is an apple!!!
@enduml
Copy the code

–> indicates an asynchronous message

Character lifeline

@startuml
participant Alice
participant Bob
participant Carol

Carol -> Bob: Who is Alice?
Bob -> Alice: Are you Alice?
@enduml
Copy the code

  • multipleparticipantCharacter lifelines are displayed from left to right
  • If there isn’t anyparticipant, the order in which the role appears displays its lifelines from left to right

Role of legend

@startuml
actor Actor
boundary Boundary
control Control
entity Entity
database Database
collections Collections
@enduml
Copy the code

In addition to participant, use other keywords to represent specific role types

A message to yourself

@startuml
Aclie -> Aclie: do something by yourself
Aclie -> Aclie: do something by yourself
Aclie -> Aclie: do something by yourself
Aclie -> Aclie: do something by yourself
@enduml
Copy the code

Message number

@startuml
Alice -> Bob: Hi
autonumber
Bob -> Carol: Hi
Carol -> Dave: Hi
Bob -> Dave: Hi
@enduml
Copy the code

Sometimes messages need to be numbered to indicate order. You can add Autonumber before the first message, and subsequent messages will be numbered automatically.

Start sequence number and increment

@startuml
autonumber 3
Alice -> Bob: Hi
Bob -> Carol: Hi
autonumber 2 3
Carol -> Dave: Hi
Bob -> Dave: Hi
@enduml
Copy the code

Autonumber < start ordinal > < increment > is used to specify the actual ordinal and increment of the ordinal

Message sequence pause

@startuml
autonumber
Alice -> Bob: Hi
autonumber stop
Bob -> Carol: Hi
Carol -> Dave: Hi
autonumber resume
Bob -> Dave: Hi
Carol -> Dave: Hi
@enduml
Copy the code
  • Autonumber stop: stops automatically
  • Autonumber resume: Automatic serial number continues

Message groups

@startuml
Alice -> Bob: Is this a pen?
alt yes
    Alice <-- Bob: Yes! This is a pen!!
else no
    Alice <-- Bob: No! This is an apple!!!!!
end
@enduml
Copy the code

  • When multiple messages are required to represent a set of related logic, you can use preset keywords to represent the various logic, for example
    • alt/else
    • opt
    • loop
    • par
    • break
    • critical
  • Add logical text after keywords, such as yes, no, etc
  • Indentation of message information is not required, but is more readable

Message groups are nested

Other message groups can be nested within a message group, as follows:

@startuml
Alice -> Bob: Is this a pen?
alt yes
    Alice <-- Bob: Yes! This is a pen!!
else no
    Alice <-- Bob: Noooooooo! This is an apple!!!!!
    loop ∞
        Alice -> Bob: Oh sorry! By the way, is this a pen?
        Alice <-- Bob: No!!!!
    end
end
@enduml
Copy the code

Custom message groups

In addition to message groups with preset keywords, you can customize a message group with any name

@startuml
group copy
    Alice -> Bob: Is this a pen?
    Alice <-- Bob: No! This is an apple!!
end
@enduml
Copy the code

Add the name of the message group after group

Lifeline active state

@startuml
activate Alice
Alice -> Bob

activate Bob
Bob -> Carol

activate Carol
Bob <-- Carol

deactivate Carol
Alice <-- Bob

deactivate Bob
@enduml
Copy the code

  • activate <name>The lifeline with the specified name is active
  • deactive <name>The lifeline with the specified name deactivates

Nested active state

@startuml
activate Alice
Alice -> Bob

activate Bob
Bob -> Bob
activate Bob
Bob -> Carol

activate Carol
Bob <-- Carol

deactivate Carol
Alice <-- Bob

deactivate Bob
@enduml
Copy the code

Continue activate within activate to nest active states

Create roles and lifelines

@startuml
Alice -> Bob
create Carol
Bob -> Carol: new
Bob -> Carol
Bob <-- Carol
Alice <-- Bob
@enduml
Copy the code

Create

is used to create a role and its lifeline, at which point the message arrow executes the role legend

Reference, reference

@startuml Alice -> Bob ref over Bob, Carol: ... Alice <-- Bob ref over Alice ... . end ref @endumlCopy the code

You can add reference information to the sequence diagram

  • Ref over < name > :: Reference Scope and reference content
  • ref over ... end ref: You can write reference content on a new line

border

@startuml
== Foo ==
Alice -> Bob
Alice <-- Bob

== Bar ==
Bob -> Carol
Bob <-- Carol
@enduml
Copy the code

==

== Add boundaries that span the lifelines of all characters

External message

@startuml
[-> Alice: Hello
Alice ->]: Hello
@enduml
Copy the code

Message arrows are preceded by [,] to indicate a message from or to an external source

Message interval

@startuml Alice -> Bob Alice <-- Bob Alice -> Bob Alice <-- Bob ||| Alice -> Bob Alice <-- Bob ||80|| Alice -> Bob Alice  <-- Bob @endumlCopy the code

  • Add between messages|||, the message interval will be appropriately separated
  • ||<pixel>||: Pixel specifies the number of pixels in a specific interval

note

@startuml
Alice -> Bob
note left: Hello
Alice <-- Bob
note right: World
Alice -> Alice
note left
Hello
World
end note
@enduml
Copy the code

  • Follow the newsnote leftornote rightNote Note cannot specify top or bottom
  • note <left|right> ... end noteYou can write remarks on a new line
  • The remarks are in the Creole format, which has a syntax similar to Markdown

Creole syntax example

@startuml note left -- title -- = title 1 == Title 2 === Title 3 -- list -- * List 1 * List 2 ** List 2-1 # ordered list 1 # Ordered list 2 # ordered list 2-1 -- font -- ** * bold ** * / / italics / / * "" monospaced font (monospace) - strikethrough -" "* * __ _ __ - form - | = | = Column1 | = Column2 | | 1 | | Value1-1 Value1-2 | | 2 2 | | | Value2-1 Value2 - - HTML - * < color: red > set colors < / color > * < color: # 00 ff00 > color number < / color > * < back: skyblue > background color < / back > * "Size: 18 > size < / size > * < b > bold < / b > - directory - | _build. Gradle | _src | _main | _java | _... | _... |_test end note @endumlCopy the code

Brother:Use PlantUML to draw UML (top) class diagrams