UML类图

概述

《大话设计模式》中UML类图的图示。

使用功具plantUML。

https://plantuml.com/zh/

plantUML依赖

java jdk的安装

GraphViz的安装

都安装完成后,可以下载plantuml.jar并执行,即可访问plantuml的图形用户界面使用。也可以使用一些编程工具的插件比如vscode/idea都有插件支持。

GraphViz in max

1
2
brew update
brew install graphviz

学习

学习目标

要会读(传统的、plantuml),要会写

元素声明 或者 类声明

大话设计模式讲解中只需要几种类,plantuml提供的更细一点。

一般需要 接口、抽象类、类。

传统的接口类图中上面<<interface>> 标注,plantuml 是 显示字母I。

抽象类 类名为斜体。

第一层是类名

第二层是字段/属性

第三层是方法

字段与方法之前有符号:+ 表示public - 表示private # 表示protected。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@startuml
abstract class "抽像类"
annotation "注解类"
circle "讲人话接口"
class "类"{
+name
-age
#gender
+getName()
+setName()
}
entity "实体"
enum "枚举"
exception "异常"
interface "接口"
@enduml

继承

继承关系用空心三角形+实线来表示。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@startuml
abstract class "动物"{
+有生命
+新陈代谢(氧气,水)
+繁殖()
}
class "鸟"{
+羽毛
+有角质喙没有牙齿
+下蛋()
}

动物 <|-- 鸟
@enduml
1
2
3
4
5
6
7
8
9
10
11
12
@startuml
abstract class "动物"{
+有生命
+新陈代谢(氧气,水)
+繁殖()
}
class "鸟" extends "动物"{
+羽毛
+有角质喙没有牙齿
+下蛋()
}
@enduml

实现接口

实现接口用空心三角形 + 虚线来表示。

1
2
3
4
5
6
7
8
9
@startuml
interface 飞翔 {
+ 飞()
}
class 大雁 implements 飞翔 {
+ 下蛋()
+ 飞()
}
@enduml
1
2
3
4
5
6
7
8
9
10
@startuml
interface 飞翔 {
+ 飞()
}
class 大雁 {
+ 下蛋()
+ 飞()
}
飞翔 <|.. 大雁
@enduml
1
2
3
4
5
6
7
8
9
@startuml
' 棒棒糖表示法
circle 飞翔
class 大雁 {
+ 下蛋()
+ 飞()
}
飞翔 <|.. 大雁
@enduml

关联

企鹅需要”知道”气候的变化,需要”了解”气候规律。当一个类”知道”另一类时,可以用关联(association)。关联关系用实线箭头来表示。

1
2
3
4
5
@startuml
class 企鹅
class 气候
气候 <-- 企鹅
@enduml

聚合

聚合表示一种弱的”拥有”关系,体现的是A对象可以包含B对象,但B对象不是A对象的一部分。聚合关系用空心的菱形+实线箭头来表示。

1
2
3
4
5
@startuml
class 雁群
class 大雁
大雁 <--o 雁群
@enduml

组合

合成(composition, 也叫组合)是一种强的”拥有”关系,体现了严格的部分和整体的关系,部分和整体的生命周期一样。合成关系用实心的菱形+实线箭头来表示。

1
2
3
4
5
@startuml
class 鸟
class 翅膀
鸟 *--> 翅膀
@enduml

依赖

动物依赖氧气和水。依赖关系用虚线箭头来表示。

1
2
3
4
5
6
7
8
9
10
11
@startuml
class 氧气
class 水
abstract class 动物{
+有生命
+新陈代谢(氧气,水)
+繁殖()
}
氧气 <.. 动物
水 <.. 动物
@enduml

关系上的标签

比如 鸟拥有双个翅膀。

1
2
3
4
5
@startuml
class 鸟
class 翅膀
"1" *--> "2" 翅膀 : 拥有
@enduml

备注

你可以使用note left of , note right of , note top of , note bottom of这些关键字来添加备注。

你还可以在类的声明末尾使用note left, note right,note top, note bottom来添加。

此外,单独用note这个关键字也是可以的,使用 .. 符号可以作出一条连接它与其它对象的虚线。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@startuml
class 氧气
note left of 氧气: 氧气是动物生存的必需物质
class 水
note right of 水: 水是动物生存的必需物质
abstract class 动物{
+有生命
+新陈代谢(氧气,水)
+繁殖()
}
note left: 动物是地球上最复杂的生物
氧气 <.. 动物
水 <.. 动物
note "依赖关系" as N1
氧气 .. N1
水 .. N1
动物 .. N1
@enduml

可以在属性(field、attribute、member)或方法上添加注释。

1
2
不能与topbottom同时使用 (只支持leftright)
不能与表示命名空间的分隔符::同时使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@startuml
abstract class 动物{
+有生命
+新陈代谢(氧气,水)
+繁殖()
+繁殖(动物)
}
note left of 动物::有生命
属性注释
end note
note right of 动物::新陈代谢
方法注释
end note
note right of 动物::繁殖(动物)
同名方法注释
end note
@enduml

在定义链接之后,你可以用 note on link 给链接添加注释

如果想要改变注释相对于标签的位置,你也可以用 note left on linknote right on linknote bottom on link。(对应位置分别在label的左边,右边,下边)

1
2
3
4
5
6
7
8
9
10
11
12
13
@startuml
class 鸟
class 翅膀
鸟 *--> 翅膀 : 拥有
note on link #Blue
鸟有翅膀
end note

a --> b : 标签
note bottom on link
下侧注释
end note
@enduml

更多

更多展示效果的编写需要查看plantuml官网。


UML类图
http://hanqichuan.com/2025/04/26/系统设计/UML类图/
作者
韩启川
发布于
2025年4月26日
许可协议