简介 / Introduction
用例图(Use Case Diagram)用于描述系统功能需求,以角色(Actor)和用例(Use Case)为核心,辅以系统边界和关系线表达业务场景。本篇对比 PlantUML 与 Mermaid 两种语法的用例图写法。
PlantUML 版
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| @startuml left to right direction
actor "顾客" as Customer actor "收银员" as Cashier actor "系统" as System
rectangle "线上点餐系统" { Customer -->> (下单) Customer -->> (取消订单) Cashier -->> (处理退款) (下单) .> (生成订单) : include (下单) .> (发送通知) : include (取消订单) .> (发送通知) : include } @enduml
|
Mermaid 版
1 2 3 4 5 6 7 8 9
| flowchart LR subgraph "线上点餐系统" A1(("顾客")) --下单--> B1["下单"] A1(("顾客")) --取消订单--> B2["取消订单"] A2(("收银员")) --处理退款--> B3["处理退款"] B1 -->|include| B4["生成订单"] B1 -->|include| B5["发送通知"] B2 -->|include| B5 end
|
对比 / Side-by-side
| 要素 |
PlantUML |
Mermaid |
| 角色声明 |
actor "名称" as 别名 |
A(("名称")) 节点 |
| 系统边界 |
rectangle "名称" { ... } |
subgraph "名称" ... end |
| 关联关系 |
Actor -- (用例) |
A -- B(节点间连接) |
| include |
(用例) .> (子用例) : include |
`B –> |
| 方向控制 |
left to right direction |
flowchart LR |
小结 / Wrap-up
PlantUML 用例图语义更丰富,支持 include/extend 关系和明确的系统边界矩形;Mermaid 用 flowchart 模拟用例图,胜在轻量且与状态图无缝衔接。两者均可导出 SVG 用于文档。实际项目中角色多、业务复杂时推荐 PlantUML,临时演示或嵌入 Markdown 时 Mermaid 更便捷。