PlantUML 总结

PlantUML 总结

1. 概述

PlantUML 是一个开源工具,允许用户通过简单的文本描述来生成各种UML图表。它支持多种图表类型,包括但不限于序列图、用例图、类图、活动图等。

2. 基本概念

2.1 开始和结束标记
  • @startuml@enduml:用于标记PlantUML代码块的开始和结束。
plantuml 复制代码
@startuml
...
@enduml
2.2 标题
  • title:为图表添加标题。
plantuml 复制代码
title My First PlantUML Activity Diagram

3. 活动图的基本元素

3.1 开始与结束
  • startstop:表示流程的开始和结束。
plantuml 复制代码
start
:Begin Process;
stop
3.2 动作(Action)
  • :ActionName;:表示一个具体的操作或步骤。
plantuml 复制代码
start
:Initialize System;
stop
3.3 条件分支
  • if (condition) then (yes)else:用于条件判断。
plantuml 复制代码
start
if (Is Valid Input?) then (yes)
    :Process Data;
else (no)
    :Log Error;
endif
stop
3.4 循环
  • while (condition)endwhile:用于循环操作。
plantuml 复制代码
start
while (More Data to Process?)
    :Fetch Data;
endwhile
stop
3.5 并发处理
  • forkend fork:用于表示并发执行的操作。
plantuml 复制代码
start
fork
    :Task A;
fork again
    :Task B;
end fork
stop

4. 高级用法

4.1 分区(Partition)
  • partition "Name":用于将流程分组,便于组织和可视化。
plantuml 复制代码
partition "Data Processing" #LightBlue {
    :Fetch Data;
    :Process Data;
}
4.2 注释
  • note leftnote right:用于在图表中添加注释。
plantuml 复制代码
start
:Initialize System;
note right: This is the initialization step.
stop
4.3 参与者(Actor)
  • actor ActorName:用于表示系统外部的人或实体。
plantuml 复制代码
actor User
participant "Web Browser" as WB
participant "Server" as S

User -> WB: Open Home Page
WB -> S: Request Home Page
S -> WB: Return Home Page Content
WB -> User: Show Home Page
4.4 返回箭头
  • 在序列图中,可以明确显示返回值。
plantuml 复制代码
actor User
participant "Web Browser" as WB
participant "Server" as S

User -> WB: Login Request
activate WB
WB -> S: Forward Login Request
activate S
S -> WB: Authentication Result
return Authenticated Successfully
deactivate S
WB -> User: Display Dashboard
deactivate WB

5. 示例

5.1 简单流程图
plantuml 复制代码
@startuml
title Simple Process Example

start

partition "Process A" #LightBlue {
    :Step 1;
    if (Condition?) then (yes)
        :Step 2;
    else (no)
        :Step 3;
    endif
}

partition "Process B" #LightGreen {
    fork
        :Step 4;
    fork again
        :Step 5;
    end fork
}

stop

@enduml
5.2 包含异常处理的流程图
plantuml 复制代码
@startuml
title Exception Handling Example

start
:Start Process;
if (Is Valid Input?) then (yes)
    :Process Data;
else (no)
    :Log Error;
    stop
endif
:Finish Process;
stop

@enduml
5.3 复杂条件分支和循环
plantuml 复制代码
@startuml
title Complex Conditions and Loops

start
while (More Data to Process?)
    :Fetch Data;
    if (Data Valid?) then (yes)
        :Process Data;
    else (no)
        :Log Error;
    endif
endwhile
:Finalize Processing;
stop

@enduml

6. 自定义皮肤参数

你可以通过设置皮肤参数来自定义图表的外观。

plantuml 复制代码
@startuml
skinparam monochrome true
skinparam backgroundColor #EEEBDC
skinparam sequence {
    ArrowColor DeepSkyBlue
    ActorBorderColor DeepSkyBlue
    LifeLineBorderColor blue
    LifeLineBackgroundColor #A9DCDF
}

actor User
participant "Web Browser" as WB
participant "Server" as S

User -> WB: Login Request
activate WB
WB -> S: Forward Login Request
activate S
S -> WB: Authentication Result
return Authenticated Successfully
deactivate S
WB -> User: Display Dashboard
deactivate WB
@enduml

7. 更多图表类型

除了活动图,PlantUML还支持其他多种图表类型,如用例图、类图、时序图等。

7.1 用例图
plantuml 复制代码
@startuml
actor User
actor Admin

usecase UC1 as "Login"
usecase UC2 as "Register"
usecase UC3 as "Manage Users"

User --> UC1
Admin --> UC1
Admin --> UC2
Admin --> UC3
@enduml
7.2 类图
plantuml 复制代码
@startuml
class Animal {
    +name: String
    +age: int
    +eat()
}

class Dog {
    +bark()
}

Animal <|-- Dog
@enduml
7.3 时序图
plantuml 复制代码
@startuml
actor User
participant "Web Browser" as WB
participant "Server" as S

User -> WB: Login Request
activate WB
WB -> S: Forward Login Request
activate S
S -> WB: Authentication Result
return Authenticated Successfully
deactivate S
WB -> User: Display Dashboard
deactivate WB
@enduml
相关推荐
dhxhsgrx10 分钟前
PYTHON训练营DAY25
java·开发语言·python
风逸hhh3 小时前
python打卡day25@浙大疏锦行
开发语言·python
刚入门的大一新生3 小时前
C++初阶-string类的模拟实现与改进
开发语言·c++
chxii4 小时前
5java集合框架
java·开发语言
老衲有点帅4 小时前
C#多线程Thread
开发语言·c#
C++ 老炮儿的技术栈4 小时前
什么是函数重载?为什么 C 不支持函数重载,而 C++能支持函数重载?
c语言·开发语言·c++·qt·算法
IsPrisoner5 小时前
Go语言安装proto并且使用gRPC服务(2025最新WINDOWS系统)
开发语言·后端·golang
Python私教5 小时前
征服Rust:从零到独立开发的实战进阶
服务器·开发语言·rust
chicpopoo5 小时前
Python打卡DAY25
开发语言·python