plantuml泳道图
时间: 2025-01-03 07:36:33 浏览: 234
### 使用 PlantUML 创建泳道图
在 PlantUML 中创建泳道图主要通过定义不同的 swimlanes 来实现。swimlane 是一种用于区分不同参与者或角色活动区域的方式,在流程图中非常有用。
#### 定义 Swimlane 的基本语法
为了定义一个 swimlane,可以使用 `|` 符号来指定新的 swimlane 开始位置,并给定名称。下面是一个简单的例子:
```plantuml
@startuml
|Swimlane1|
start
:foo1;
|#AntiqueWhite|Swimlane2|
:foo2;
:foo3;
|Swimlane1|
:foo4;
|Swimlane2|
:foo5;
stop
@enduml
```
此代码片段展示了两个 swimlane 如何交互工作[^2]。第一个 swimlane 执行了一个操作 (`foo1`) 后切换到第二个 swimlane (命名为 "Swimlane2") 并设置了背景颜色为古董白继续执行其他动作(`foo2`, `foo3`)。之后再次回到第一个 swimlane 进行另一项任务(`foo4`)最后由第二个 swimlane 结束整个过程(`foo5`)。
#### 更复杂的泳道图实例
更复杂一点的例子可能涉及多个参与者以及它们之间的消息传递:
```plantuml
@startuml
actor User
participant "Web Server" as WS
participant Database
User -> WS : Request Page
activate WS
WS -> Database : Query Data
activate Database
Database --> WS : Return Results
deactivate Database
WS --> User : Show Page
deactivate WS
@enduml
```
在这个案例里虽然没有显式的声明 swimlane ,但是可以通过添加额外的分隔线来增强可读性和逻辑划分:
```plantuml
@startuml
title A sequence diagram with swimlanes
actor User
participant "Web Server" as WS
participant Database
== Web Interaction ==
User -> WS : Request Page
activate WS
== Backend Processing ==
WS -> Database : Query Data
activate Database
Database --> WS : Return Results
deactivate Database
== Response to Client ==
WS --> User : Show Page
deactivate WS
@enduml
```
上述示例不仅限于 web 应用场景;任何具有清晰阶段性的业务流都可以采用这种方式表示出来。
阅读全文
相关推荐

















