使用PlantUML绘制指定样式分层软件架构图的最佳方法
用PlantUML绘制分层软件架构图的最佳实践
我希望用PlantUML绘制分层软件架构图,参考某分层架构示例图(多层横向布局的组件分组,层间有清晰的依赖连线),请问创建这类图的最佳方法是什么?
我已经完成了一个初始版本,代表包含若干通用组件的三层架构,初始版本效果为三层横向布局的组件分组,层间有不同样式的依赖连线,代码如下:
@startuml LAYER_3 -[hidden]down- LAYER_2 LAYER_2 -[hidden]down- LAYER_1 package LAYER_3 { component Comp30 { } component Comp31 { } component Comp32 { } component Comp33 { } } package LAYER_2 { component Comp20 { } component Comp21 { } component Comp22 { } component Comp23 { } component Comp24 { } component Comp25 { } component Comp26 { } component Comp27 { } } package LAYER_1 { component Comp10 { } component Comp11 { } component Comp12 { } component Comp13 { } component Comp14 { } component Comp15 { } component Comp16 { } component Comp17 { } } Comp23 -[#6666ff,dotted]> Comp10 Comp21 -[dotted,#red]> Comp10 Comp20 -> Comp10 Comp20 -> Comp16 @enduml
最佳实现方法及优化建议
1. 用PlantUML原生layer语法替代隐藏连线
PlantUML支持layer关键字直接定义分层布局,比手动添加隐藏连线更简洁且易维护,还能保证层的顺序一致性:
@startuml ' 定义分层顺序(从上到下) layer 表现层 layer 业务层 layer 数据层 ' 将组件分配到对应层,同时添加有意义的别名 component Comp30 as "用户界面A" belongs to 表现层 component Comp31 as "用户界面B" belongs to 表现层 component Comp32 as "用户界面C" belongs to 表现层 component Comp33 as "用户界面D" belongs to 表现层 component Comp20 as "业务服务A" belongs to 业务层 component Comp21 as "业务服务B" belongs to 业务层 component Comp22 as "业务服务C" belongs to 业务层 component Comp23 as "业务服务D" belongs to 业务层 component Comp24 as "业务服务E" belongs to 业务层 component Comp25 as "业务服务F" belongs to 业务层 component Comp26 as "业务服务G" belongs to 业务层 component Comp27 as "业务服务H" belongs to 业务层 component Comp10 as "数据访问A" belongs to 数据层 component Comp11 as "数据访问B" belongs to 数据层 component Comp12 as "数据访问C" belongs to 数据层 component Comp13 as "数据访问D" belongs to 数据层 component Comp14 as "数据访问E" belongs to 数据层 component Comp15 as "数据访问F" belongs to 数据层 component Comp16 as "数据访问G" belongs to 数据层 component Comp17 as "数据访问H" belongs to 数据层 ' 给依赖连线添加注释,明确交互类型 Comp23 -[#6666ff,dotted]> Comp10 : 读取配置 Comp21 -[dotted,#red]> Comp10 : 异常回写 Comp20 -> Comp10 : 同步调用 Comp20 -> Comp16 : 查询数据 @enduml
2. 优化可读性细节
- 给组件添加业务化别名(用
as关键字),替代无意义的CompXX命名 - 给层使用清晰的业务名称(如“表现层”“业务层”“数据层”)
- 给依赖连线添加注释,明确交互的业务含义
3. 规范依赖连线样式
统一不同类型依赖的视觉表现,建议约定:
->:实线,代表直接同步调用-dotted->:虚线,代表间接依赖(如配置、接口依赖)- 用颜色区分特殊场景(如红色代表异常流程、蓝色代表配置依赖)
4. 可选:添加图例说明
如果图中存在多种连线样式,可添加图例让读者快速理解:
@startuml ' 省略分层和组件定义... ' 添加右侧图例 legend right |==| 直接同步调用 |..| 间接依赖 |<color:#6666ff>..</color>| 配置依赖 |<color:#red>..</color>| 异常流程 endlegend @enduml
内容的提问来源于stack exchange,提问作者nix
相关产品推荐
相关产品推荐

