PlantUML工具书之一(序列图|用例图|类图)

PlantUML可以让你通过近似编程的方式完成程序开发过程中各种图形的绘制,完全可以满足我们在软件开发过程中的各种需求。

然而因为它有自己的一套语法体系,所以学习成本比较高,我的通常做法就是对写基础图形中做简单的修改,就可以满足自己的要求。总结起来我认为PlantUML有一下几个优点:

  1. 通过逻辑代码完成绘图,可以梳理自己开发过程中的逻辑思路是否合理,理顺业务的同时画图也随之完成;
  2. 开发者可以只专注在开发业务的逻辑关系上,不用分心关注绘图细节,例如图形,位置,颜色,连线都一概不管;
  3. 修改方便,生成图片样式统一。

下面我就罗列一些模板,方便在开发过程中依样画葫芦^_^.

序列图


1
2
3
4
5
6
@startuml
Alice -> Bob: Authentication Request
Bob --> Alice: Authentication Response
Alice -> Bob: Another authentication Request
Alice <-- Bob: another authentication Response
@endum

1
2
3
4
5
6
7
8
9
10
11
@startuml
actor Foo1
boundary Foo2
control Foo3
entity Foo4
database Foo5
Foo1 -> Foo2 : To boundary
Foo1 -> Foo3 : To control
Foo1 -> Foo4 : To entity
Foo1 -> Foo5 : To database
@enduml

1
2
3
4
5
6
7
8
9
10
11
12
13
@startuml
actor Bob #red
' The only difference between actor
'and participant is the drawing
participant Alice
participant "I have a really\nlong name" as L #99FF99
/' You can also declare:
participant L as "I have a really\nlong name" #99FF99
'/
Alice ->Bob: Authentication Request
Bob->Alice: Authentication Response
Bob->L: Log transaction
@enduml

1
2
3
4
5
6
7
@startuml
Alice -> "Bob()" : Hello
"Bob()" -> "This is very\nlong" as Long
' You can also declare:
' "Bob()" -> Long as "This is very\nlong"
Long --> "Bob()" : ok
@enduml

1
2
3
@startuml
Alice ->Alice: This is a signal to self.\nIt also demonstrates\nmultiline \ntext
@enduml

1
2
3
4
5
6
7
8
9
10
11
12
@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

1
2
3
4
@startuml
Bob -[#red]> Alice : hello
Alice -[#0000FF]->Bob : ok
@enduml

1
2
3
4
5
@startuml
autonumber
Bob -> Alice : Authentication Request
Bob <- Alice : Authentication Response
@enduml

1
2
3
4
5
6
7
8
9
10
11
@startuml
autonumber
Bob -> Alice : Authentication Request
Bob <- Alice : Authentication Response
autonumber 15
Bob -> Alice : Another authentication Request
Bob <- Alice : Another authentication Response
autonumber 40 10
Bob -> Alice : Yet another authentication Request
Bob <- Alice : Yet another authentication Response
@enduml

1
2
3
4
5
6
7
8
9
10
11
@startuml
autonumber "<b>[000]"
Bob -> Alice : Authentication Request
Bob <- Alice : Authentication Response
autonumber 15 "<b>(<u>##</u>)"
Bob -> Alice : Another authentication Request
Bob <- Alice : Another authentication Response
autonumber 40 10 "<font color=red><b>Message 0 "
Bob -> Alice : Yet another authentication Request
Bob <- Alice : Yet another authentication Response
@enduml

1
2
3
4
5
@startuml
title Simple communication example
Alice -> Bob: Authentication Request
Bob --> Alice: Authentication Response
@enduml

1
2
3
4
5
6
7
@startuml
Alice -> Bob : Hello
legend right
Short
legend
endlegend
@enduml

1
2
3
4
5
6
7
8
9
10
@startuml
Alice -> Bob : message 1
Alice -> Bob : message 2
newpage
Alice -> Bob : message 3
Alice -> Bob : message 4
newpage A title for the\nlast page
Alice -> Bob : message 5
Alice -> Bob : message 6
@enduml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@startuml
Alice -> Bob: Authentication Request
alt successful case
Bob -> Alice: Authentication Accepted
else some kind of failure
Bob -> Alice: Authentication Failure
group My own label
Alice -> Log : Log attack start
loop 1000 times
Alice -> Bob: DNS Attack
end
Alice -> Log : Log attack end
end
else Another type of failure
Bob -> Alice: Please repeat
end
@enduml

1
2
3
4
5
6
7
8
9
10
11
12
@startuml
Alice ->Bob : hello
note left: this is a first note
Bob->Alice : ok
note right: this is another note
Bob->Bob : I am thinking
note left
a note
can also be defined
on several lines
end note
@enduml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@startuml
participant Alice
participant Bob
note left of Alice #aqua
This is displayed
left of Alice.
end note
note right of Alice: This is displayed right of Alice.
note over Alice: This is displayed over Alice.
note over Alice , Bob #FFAAAA: This is displayed\n over Bob and Alice.
note over Bob, Alice
This is yet another
example of
a long note.
end note
@endum

1
2
3
4
5
6
7
8
9
@startuml
caller -> server : conReq
hnote over caller : idle
caller <- server : conConf
rnote over server
"r" as rectangle
"h" as hexagon
endrnote
@enduml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
@startuml
participant Alice
participant "The **Famous** Bob" as Bob
Alice -> Bob : hello --there --
... Some ~~long delay~~ ...
Bob -> Alice : ok
note left
This is **bold**
This is //italics//
This is ""monospaced""
This is --stroked --
This is __underlined__
This is ~~waved~~
end note
Alice -> Bob : A //well formatted// message
note right of Alice
This is <back:cadetblue ><size:18>displayed </size ></back>
__left of__ Alice.
end note
note left of Bob
<u:red>This </u> is <color #118888>displayed </color >
**<color purple >left of</color > <s:red>Alice </strike > Bob**.
end note
note over Alice , Bob
<w:#FF33FF >This is hosted </w> by <img sourceforge.jpg>
end note
@enduml

1
2
3
4
5
6
7
8
@startuml
== Initialization ==
Alice -> Bob: Authentication Request
Bob --> Alice: Authentication Response
== Repetition ==
Alice -> Bob: Another authentication Request
Alice <-- Bob: another authentication Response
@enduml

1
2
3
4
5
6
7
8
9
10
@startuml
participant Alice
actor Bob
ref over Alice , Bob : init
Alice -> Bob : hello
ref over Bob
This can be on
several lines
end ref
@enduml

1
2
3
4
5
6
7
@startuml
Alice -> Bob: Authentication Request
...
Bob --> Alice: Authentication Response
...5 minutes latter...
Bob --> Alice: Bye !
@enduml

1
2
3
4
5
6
7
@startuml
Alice -> Bob: Authentication Request
...
Bob --> Alice: Authentication Response
...5 minutes latter...
Bob --> Alice: Bye !
@enduml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@startuml
participant User
User -> A: DoWork
activate A
A -> B: << createRequest >>
activate B
B -> C: DoWork
activate C
C --> B: WorkDone
destroy C
B --> A: RequestCreated
deactivate B
A -> User: Done
deactivate A
@enduml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@startuml
participant User
User -> A: DoWork
activate A #FFBBBB
A -> A: Internal call
activate A #DarkSalmon
A -> B: << createRequest >>
activate B
B --> A: RequestCreated
deactivate B
deactivate A
A -> User: Done
deactivate A
@enduml

1
2
3
4
5
6
7
8
9
@startuml
Bob -> Alice : hello
create Other
Alice -> Other : new
create control String
Alice -> String
note right : You can also put notes!
Alice --> Bob : ok
@enduml

1
2
3
4
5
6
7
8
9
10
11
@startuml
[-> A: DoWork
activate A
A -> A: Internal call
activate A
A ->] : << createRequest >>
A<--] : RequestCreated
deactivate A
[<- A: Done
deactivate A
@enduml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@startuml
[-> Bob
[o-> Bob
[o->o Bob
[x-> Bob
[<- Bob
[x<- Bob
Bob ->]
Bob ->o]
Bob o->o]
Bob ->x]
Bob <-]
Bob x<-]
@enduml

1
2
3
4
5
@startuml
participant "Famous Bob" as Bob << Generated >>
participant Alice << (C,#ADD1B2) Testable >>
Bob->Alice: First message
@enduml

1
2
3
4
5
6
@startuml
skinparam guillemet false
participant "Famous Bob" as Bob << Generated >>
participant Alice << (C,#ADD1B2) Testable >>
Bob->Alice: First message
@enduml

1
2
3
4
5
@startuml
participant Bob << (C,#ADD1B2) >>
participant Alice << (C,#ADD1B2) >>
Bob->Alice: First message
@enduml

1
2
3
4
5
6
7
8
9
10
@startuml
title __Simple__ **communication** example
Alice -> Bob: Authentication Request
Bob -> Alice: Authentication Response


newpage title __Simple__ communication example\non several lines
Alice -> Bob: Authentication Request
Bob -> Alice: Authentication Response
@enduml

1
2
3
4
5
6
7
8
9
@startuml
title
<u>Simple </u> communication example
on <i>several </i> lines and using <font color=red>html </font>
This is hosted by <img:sourceforge.jpg>
end title
Alice -> Bob: Authentication Request
Bob -> Alice: Authentication Response
@enduml

1
2
3
4
5
6
7
8
9
10

@startuml
box "Internal Service" #LightBlue
participant Bob
participant Alice
end box
participant Other
Bob -> Alice : hello
Alice -> Other : hello
@enduml

1
2
3
4
5
6
@startuml
hide footbox
title Footer removed
Alice -> Bob: Authentication Request
Bob --> Alice: Authentication Response
@enduml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
@startuml
skinparam backgroundColor #EEEBDC
skinparam sequence {
ArrowColor DeepSkyBlue
ActorBorderColor DeepSkyBlue
LifeLineBorderColor blue
LifeLineBackgroundColor #A9DCDF
ParticipantBorderColor DeepSkyBlue
ParticipantBackgroundColor DodgerBlue
ParticipantFontName Impact
ParticipantFontSize 17
ParticipantFontColor #A9DCDF
ActorBackgroundColor aqua
ActorFontColor DeepSkyBlue
ActorFontSize 17
ActorFontName Aapex
}
actor User
participant "First Class" as A
participant "Second Class" as B
participant "Last Class" as C
User -> A: DoWork
activate A
A -> B: Create Request
activate B
B -> C: DoWork
activate C
C --> B: WorkDone
destroy C
B --> A: Request Created
deactivate B
A --> User: Done
deactivate A
@enduml

用例图


1
2
3
4
5
6
@startuml
(First usecase)
(Another usecase) as (UC2)
usecase UC3
usecase (Last\nusecase) as UC4
@enduml

1
2
3
4
5
6
7

@startuml
:First Actor:
:Another\nactor: as Men2
actor Men3
actor :Last actor: as Men4
@enduml

1
2
3
4
5
6
7
8
9
10
11
@startuml
usecase UC1 as "You can use
several lines to define your usecase.
You can also use separators.
--
Several separators are possible.
==
And you can add titles:
..Conclusion..
This allows large description."
@enduml

1
2
3
4
5
6

@startuml
User -> (Start)
User --> (Use the application) : A small label
:Main Admin: ---> (Use the application) : This is\nyet another\nlabel
@enduml

1
2
3
4
5
6
7

@startuml
:Main Admin: as Admin
(Use the application) as (Use)
User <|-- Admin
(Start) <|-- (Use)
@enduml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

@startuml
:Main Admin: as Admin
(Use the application) as (Use)
User -> (Start)
User --> (Use)
Admin ---> (Use)
note right of Admin : This is an example.
note right of (Use)
A note can also
be on several lines
end note
note "This note is connected\nto several objects." as N2
(Start) .. N2
N2 .. (Use)
@enduml

1
2
3
4
5
6
7
8
9
@startuml
User << Human >>
:Main Database: as MySql << Application >>
(Start) << One Shot >>
(Use the application) as (Use) << Main >>
User -> (Start)
User --> (Use)
MySql --> (Use)
@enduml

1
2
3
4
@startuml
:user: --> (Use case 1)
:user: -> (Use case 2)
@enduml

1
2
3
4
5

@startuml
(Use case 1) <.. :user:
(Use case 2) <- :user:
@enduml

1
2
3
4
5
@startuml
:actor1: --> (Usecase1)
newpage
:actor2: --> (Usecase2)
@enduml

1
2
3
4
5
6
@startuml
'default
top to bottom direction
user1 --> (Usecase 1)
user2 --> (Usecase 2)
@enduml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@startuml
skinparam handwritten true
skinparam usecase {
BackgroundColor DarkSeaGreen
BorderColor DarkSlateGray
BackgroundColor << Main >> YellowGreen
BorderColor << Main >> YellowGreen
ArrowColor Olive
ActorBorderColor black
ActorFontName Courier
ActorBackgroundColor << Human >> Gold
}
User << Human >>
:Main Database: as MySql << Application >>
(Start) << One Shot >>
(Use the application) as (Use) << Main >>
User -> (Start)
User --> (Use)
MySql --> (Use)
@enduml

1
2
3
4
5
6
7
8
9
10
11
12
@startuml
left to right direction
skinparam packageStyle rect
actor customer
actor clerk
rectangle checkout {
customer -- (checkout)
(checkout) .> (payment) : include
(help) .> (checkout) : extends
(checkout) -- clerk
}
@enduml

类图


1
2
3
4
5
6
7
8
9
10
11
12
@startuml
left to right direction
skinparam packageStyle rect
actor customer
actor clerk
rectangle checkout {
customer -- (checkout)
(checkout) .> (payment) : include
(help) .> (checkout) : extends
(checkout) -- clerk
}
@enduml

1
2
3
4
5
@startuml
Class01 "1" *-- "many" Class02 : contains
Class03 o-- Class04 : aggregation
Class05 --> "1" Class06
@enduml

1
2
3
4
5
6
@startuml
class Car
Driver - Car : drives >
Car *- Wheel : have 4 >
Car -- Person : < owns
@enduml

1
2
3
4
5
6
@startuml
Object <|-- ArrayList
Object : equals()
ArrayList : Object[] elementData
ArrayList : size()
@enduml

1
2
3
4
5
6
7
8
9
10
@startuml
class Dummy {
String data
void methods()
}
class Flight {
flightNumber : Integer
departureTime : Date
}
@enduml

1
2
3
4
5
6
7
8
@startuml
class Dummy {
-field1
#field2
~method1()
+method2()
}
@enduml

1
2
3
4
5
6
7
8
9
@startuml
skinparam classAttributeIconSize 0
class Dummy {
-field1
#field2
~method1()
+method2()
}
@enduml

1
2
3
4
5
6
@startuml
class Dummy {
{static} String id
{abstract} void methods()
}
@enduml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
@startuml
class Foo1 {
You can use
several lines
..
as you want
and group
==
things together.
__
You can have as many groups
as you want
--
End of class
}
class User {
.. Simple Getter ..
+ getName()
+ getAddress()
.. Some setter ..
+ setName()
__ private data __
int age
-- encrypted --
String password
}
@enduml

1
2
3
4
5
6
7
8
9
10
11
@startuml
class Object << general >>
Object <|--- ArrayList
note top of Object : In java, every class\nextends this one.
note "This is a floating note" as N1
note "This note is connected\nto several objects." as N2
Object .. N2
N2 .. ArrayList
class Foo
note left: On last defined class
@enduml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@startuml
class Foo
note left: On last defined class
note top of Object
In java, <size:18>every </size> <u>class </u>
<b>extends </b>
<i>this </i> one.
end note
note as N1
This note is <u>also </u>
<b><color:royalBlue >on several </color >
<s>words </s> lines
And this is hosted by <img:sourceforge.jpg>
end note
@enduml

1
2
3
4
5
6
7
8
9
10
11

@startuml
class Dummy
Dummy --> Foo : A link
note on link #red: note that is red
Dummy --> Foo2 : Another link
note right on link #blue
this is my note on right link
and in blue
end note
@enduml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@startuml
abstract class AbstractList
abstract AbstractCollection
interface List
interface Collection
List <|-- AbstractList
Collection <|-- AbstractCollectio
Collection <|- List
AbstractCollection <|- AbstractLi
AbstractList <|-- ArrayList
class ArrayList {
Object[] elementData
size()
}
enum TimeUnit {
DAYS
HOURS
MINUTES
}
annotation SuppressWarnings
@enduml

1
2
3
4
5
@startuml
class "This is my class" as class1
class class2 as "It works this way too"
class2 *-- "foo/dummy" : use
@enduml

1
2
3
4
5
6
@startuml
class Foo1
class Foo2
Foo2 *-- Foo1
hide Foo2
@enduml

1
2
3
4
5
6
7

@startuml
class Foo<? extends Element > {
int size()
}
Foo *- Element
@enduml

1
2
3
4
@startuml
class System << (S,#FF7700) Singleton >>
class Date << (D,orchid) >>
@enduml

1
2
3
4
5
6
7
8
9
@startuml
package "Classic Collections" #DDDDDD {
Object <|-- ArrayList
}
package net.sourceforge.plantuml {
Object <|-- Demo1
Demo1 *- Demo2
}
@enduml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@startuml
package foo1 <<Node>> {
class Class1
}
package foo2 <<Rect>> {
class Class2
}
package foo3 <<Folder >> {
class Class3
}
package foo4 <<Frame >> {
class Class4
}
package foo5 <<Cloud >> {
class Class5
}
package foo6 <<Database >> {
class Class6
}
@enduml

1
2
3
4
5
6
7
8
9
@startuml
skinparam packageStyle rect
package foo1.foo2 {
}
package foo1.foo2.foo3 {
class Object
}
foo1.foo2 +-- foo1.foo2.foo3
@enduml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@startuml
class BaseClass
namespace net.dummy #DDDDDD {
.BaseClass <|-- Person
Meeting o-- Person
.BaseClass <|- Meeting
}
namespace net.foo {
net.dummy.Person <|- Person
.BaseClass <|-- Person
net.dummy.Meeting o-- Person
}
BaseClass <|-- net.unused.Person
@enduml

1
2
3
4
5
6
@startuml
set namespaceSeparator ::
class X1::X2::foo {
some info
}
@enduml

1
2
3
4
@startuml
class foo
bar ()- foo
@enduml

1
2
3
4
@startuml
Room o- Student
Room *-- Chair
@enduml

1
2
3
4
5

@startuml
Student -o Room
Chair --* Room
@enduml

1
2
3
4
@startuml
title Simple <b>example </b>\nof title
Object <|-- ArrayList
@enduml

1
2
3
4
5
6
7
@startuml
Object <|- ArrayList
legend right
<b>Object </b> and <b>ArrayList </b>
are simple class
endlegend
@enduml

1
2
3
4
5
6
7
8
9
10
11
@startuml
class Student {
Name
}
Student "0..*" - "1..*" Course
(Student , Course) .. Enrollment
class Enrollment {
drop()
cancel()
}
@enduml

1
2
3
4
5
6
7
8
9
10
11
12

@startuml
class Student {
Name
}
Student "0..*" -- "1..*" Course
(Student , Course) . Enrollment
class Enrollment {
drop()
cancel()
}
@enduml

1
2
3
4
5
6
7
8
9
10
11

@startuml
skinparam class {
BackgroundColor PaleGreen
ArrowColor SeaGreen
BorderColor SpringGreen
}
skinparam stereotypeCBackgroundColor YellowGreen
Class01 "1" *-- "many" Class02 : contains
Class03 o-- Class04 : aggregation
@enduml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@startuml
' Split into 4 pages
page 2x2
class BaseClass
namespace net.dummy #DDDDDD {
.BaseClass <|-- Person
Meeting o-- Person
.BaseClass <|- Meeting
}
namespace net.foo {
net.dummy.Person <|- Person
.BaseClass <|-- Person
net.dummy.Meeting o-- Person
}
BaseClass <|-- net.unused.Person
@enduml

1
2
3
4
5
6
7
8
9
10
11
12
@startuml
skinparam backgroundcolor AntiqueWhite/Gold
skinparam classBackgroundColor Wheat|CornflowerBlue
class Foo #red-green
note left of Foo #blue\9932CC {
this is my
note on this class
}
package example #GreenYellow/LightGoldenRodYellow {
class Dummy
}
@enduml