概述
用例图主要用在需求分析阶段,通过反映用户和软件系统的交互,描述系统的功能需求。
在本文中,我们来演示一下如何使用 PlantUML 进行用例图的绘制。
用例
在用例图中,最基本的元素就是用例和人(角色)了。我们先来看一下如何在一个用例图中表示一个用例:
(First usecase)(Another usecase) as (UC2)usecase UC3usecase (Last\nusecase) as UC4
可以看到,在用例同中,表示一个用例的方式有如下两种:
- 使用 usecase 标识
- 用 () 进行包围
角色
除了用例之外,我们来看一下如何表示一个角色:
skinparam actorStyle hollow:First Actor::Another\nactor: as Man2skinparam actorStyle awesomeactor Woman3actor :Last actor: as Person1
与用例的表示方式一样,角色也有两种表示方式:
- 使用 actor 进行标识
- 使用 :: 进行包围。
同时,它也支持使用 as 将角色标识为一个别名。
此外,可以通过 skinparam 来设置角色的显示样式,支持 awesome(头像)和 hollow(透明人)等几种显示模式。
用例描述
如果想定义跨越多行的用例描述,可以用双引号将其裹起来。
还可以使用这些分隔符:
- —(横线)
- ..(虚线)
- ==(双横线)
- __(下划线)
并且还可以在分隔符中间放置标题。
usecase UC1 as "You can useseveral lines to define your usecase.You can also use separators.--Several separators are possible.==And you can add titles:..Conclusion..This allows large description."actor CC --> UC1
PS:需要注意的是,当 as 后为 “” 包围时,”” 内部的内容为用户描述信息, as 之前的内容会作为用例的标识。
使用线框包围
在用例图中,可以使用线框来对一组角色和用例进行分组,示例如下:
left to right directionactor Guest as gpackage Professional {actor Chef as cactor "Food Critic" as fc}package Restaurant {usecase "Eat Food" as UC1usecase "Pay for Food" as UC2usecase "Drink" as UC3usecase "Review" as UC4}fc --> UC4g --> UC1g --> UC2g --> UC3
箭头连接
在用例图中,可以使用 —> 来连接角色和用例。
其中:
- 横杠越多表示箭头越长;
- 在箭头后增加一个冒号和文字可以连对箭头增加标签;
- 默认情况下,箭头的竖直方向的,当一个 - 号时,会显示为水平连接;
- 使用 . 来替换 - 时,箭头的显示样式从实线会变为虚线;
- <— 时会翻转箭头方向进行显示;
- 还可以使用在箭头中添加 left, right, up 或 down 等关键词来改变方向; ``` User -> (Start) User —> (Use the application) : A small label :Main Admin: —-> (Use the application) : This is\nyet another\nlabel :XXXX: ..> (Use the application) (Use the application) <— :aaa:
:user: -left-> (dummyLeft) :user: -right-> (dummyRight) :user: -up-> (dummyUp) :user: -down-> (dummyDown)
此外,如果一个角色或者用例继承于另一个,那么可以用 <|--符号表示:
:Main Admin: as Admin (Use the application) as (Use) User <|— Admin (Start) <|— (Use)
在箭头表示中,可以定制箭头颜色和风格,基本的格式如下:
color;line.[bold|dashed|dotted];text:color
```actor foofoo --> (bar) : normalfoo --> (bar1) #red;line.bold;text:red : red boldfoo --> (bar2) #green;line.dashed;text:green : green dashedfoo --> (bar3) #blue;line.dotted;text:blue : blue dotted
注释
可以用 note left of , note right of , note top of , note bottom of 等关键字给一个对象添加注释。
注释还可以通过 note 关键字来定义,然后用.. 或者 — 等连接其他对象。
: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 alsobe on several linesend notenote "This note is connected\nto several objects." as N2(Start) .. N2N2 .. (Use)
构造类型
在用例图中,可以使用 << 和 >> 来定义角色或者用例的构造类型。
User << Human >>:Main Database: as MySql << Application >>(Start) << One Shot >>(Use the application) as (Use) << Main >>User -> (Start)User --> (Use)MySql --> (Use)
样式调整
在用例图中,可以使用 skinparam 来改变字体和颜色,例如:
skinparam handwritten trueskinparam usecase {BackgroundColor DarkSeaGreenBorderColor DarkSlateGrayBackgroundColor<< Main >> YellowGreenBorderColor<< Main >> YellowGreenArrowColor OliveActorBorderColor blackActorFontName CourierActorBackgroundColor<< Human >> Gold}User << Human >>:Main Database: as MySql << Application >>(Start) << One Shot >>(Use the application) as (Use) << Main >>User -> (Start)User --> (Use)MySql --> (Use)
除了通过 skinparam 来进行样式定制之外,还可以像箭头一样,在行内对角色、用例进行样式定制:
actor aactor b #pink;line:red;line.bold;text:redusecase c #palegreen;line:green;line.dashed;text:greenusecase d #aliceblue;line:blue;line.dotted;text:blue
