0 实例

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

1 定义

用例

  • 用例用圆括号括起来(两个圆括号看起来就像椭圆)。
  • 也可以用关键字usecase来定义用例。

角色

角色用两个冒号包裹起来。
也可以用actor关键字来定义角色。

1
2
3
4
5
6
7
8
9
10
11
@startuml

(First usecase)
(Another usecase) as (UC2)
usecase UC3
usecase (Last\nusecase) as UC4
:First Actor:
:Another\nactor: as Man2
actor Woman3
actor :Last actor: as Person1
@enduml

2 角色样式

  • 火柴人样式:默认
  • 用户头像样式:skinparam actorStyle awesome
  • 透明人样式:skinparam actorStyle hollow

3 用例描述

  • –(横线)
  • ..(虚线)
  • ==(双横线)
  • __(下划线)
1
2
3
4
5
6
7
8
9
10
11
12
13
@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

4 用例包

  • package 可以一使用包来对角色或用例进行分组。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@startuml
left to right direction
actor Guest as g
package Professional {
actor Chef as c
actor "Food Critic" as fc
}
package Restaurant {
usecase "Eat Food" as UC1
usecase "Pay for Food" as UC2
usecase "Drink" as UC3
usecase "Review" as UC4
}
fc --> UC4
g --> UC1
g --> UC2
g --> UC3
@enduml

5 继承关系

如果一个角色或者用例继承于另一个,那么可以用<|–符号表示。

1
2
3
4
5
6
7
8
@startuml
:Main Admin: as Admin
(Use the application) as (Use)

User <|-- Admin
(Start) <|-- (Use)

@enduml

6 使用注释

可以用note left of , note right of , note top of , note bottom of等关键字给一个对象添加注释。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@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

7 构造类型

用 << 和 >> 来定义角色或者用例的构造类型。

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

User -> (Start)
User --> (Use)

MySql --> (Use)

@enduml

8 改变箭头方向

默认连接是竖直方向的,用–表示,可以用一个横杠或点来表示水平连接。

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

还可以通过给箭头添加left, right, up或down等关键字来改变方向

1
2
3
4
5
6
@startuml
:user: -left-> (dummyLeft)
:user: -right-> (dummyRight)
:user: -up-> (dummyUp)
:user: -down-> (dummyDown)
@enduml

9 改变风格

  • 改变箭头风格
1
2
3
4
5
6
7
@startuml
actor foo
foo --> (bar) : normal
foo --> (bar1) #line:red;line.bold;text:red : red bold
foo --> (bar2) #green;line.dashed;text:green : green dashed
foo --> (bar3) #blue;line.dotted;text:blue : blue dotted
@enduml
  • 改变用例风格
1
2
3
4
5
6
@startuml
actor a
actor b #pink;line:red;line.bold;text:red
usecase c #palegreen;line:green;line.dashed;text:green
usecase d #aliceblue;line:blue;line.dotted;text:blue
@enduml