类图的设计语法与编程语言的传统语法相似。这种相似性为开发人员提供了一个熟悉的环境,从而使创建图表的过程更简单、更直观。
这种设计方法不仅简洁,而且还能创建既简洁又富有表现力的表述。此外,它还允许通过与序列图相呼应的语法来描绘类之间的关系,为流畅而深刻地描绘类之间的交互铺平了道路。
除了结构和关系表示法,类图语法还支持进一步的丰富,如包含注释和应用颜色,使用户能够创建信息丰富且视觉上吸引人的图表。
您可以了解更多有关PlantUML 中一些常用命令的信息,以增强您的图表创建体验。
元素声明
@startuml
!include https://f.umlcn.com/c4.puml
abstract abstract
abstract class "abstract class"
annotation annotation
circle circle
() circle_short_form
class class
class class_stereo <<stereotype>>
diamond diamond
<> diamond_short_form
entity entity
enum enum
exception exception
interface interface
metaclass metaclass
protocol protocol
stereotype stereotype
struct struct
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
abstract abstract
abstract class "abstract class"
annotation annotation
circle circle
() circle_short_form
class class
class class_stereo <<stereotype>>
diamond diamond
<> diamond_short_form
entity entity
enum enum
exception exception
interface interface
metaclass metaclass
protocol protocol
stereotype stereotype
struct struct
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
类之间的关系
类之间的关系是用以下符号定义的。
关系类型 | 符号 |
---|---|
泛化关系 | `< |
组合关系 | *-- |
聚合关系 | o-- |
可以用..
来代替--
,会显示为虚线。
@startuml
!include https://f.umlcn.com/c4.puml
Class01 <|-- Class02
Class03 *-- Class04
Class05 o-- Class06
Class07 .. Class08
Class09 -- Class10
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
Class01 <|-- Class02
Class03 *-- Class04
Class05 o-- Class06
Class07 .. Class08
Class09 -- Class10
@enduml
2
3
4
5
6
7
8
9
@startuml
!include https://f.umlcn.com/c4.puml
Class11 <|.. Class12
Class13 --> Class14
Class15 ..> Class16
Class17 ..|> Class18
Class19 <--* Class20
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
Class11 <|.. Class12
Class13 --> Class14
Class15 ..> Class16
Class17 ..|> Class18
Class19 <--* Class20
@enduml
2
3
4
5
6
7
8
9
@startuml
!include https://f.umlcn.com/c4.puml
Class21 #-- Class22
Class23 x-- Class24
Class25 }-- Class26
Class27 +-- Class28
Class29 ^-- Class30
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
Class21 #-- Class22
Class23 x-- Class24
Class25 }-- Class26
Class27 +-- Class28
Class29 ^-- Class30
@enduml
2
3
4
5
6
7
8
9
关系上的标签
在关系之间使用标签来说明时, 使用 :
后接 标签文字。
对元素的说明,你可以在每一边使用 ""
来说明.
@startuml
!include https://f.umlcn.com/c4.puml
类01 "1" *-- "many" 类02 : 包含
类03 o-- 类04 : 聚合
类05 --> "1" 类06
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
类01 "1" *-- "many" 类02 : 包含
类03 o-- 类04 : 聚合
类05 --> "1" 类06
@enduml
2
3
4
5
6
7
8
9
10
11
在标签的开始或结束位置添加<
或 >
以表明是哪个对象作用到哪个对象上。
@startuml
!include https://f.umlcn.com/c4.puml
class 汽车
发动机 - 汽车 : 驱动 >
汽车 *- 轮子 : 拥有 4 >
汽车 -- 人 : < 所属
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
class 汽车
发动机 - 汽车 : 驱动 >
汽车 *- 轮子 : 拥有 4 >
汽车 -- 人 : < 所属
@enduml
2
3
4
5
6
7
8
9
10
在元素名称和关系标签中使用非字母
如果你想在类(或枚举...)的显示名称中使用非字母,你可以:
- 在类定义中使用
as
关键字来指定一个别名 - 在类名称周围加上引号
""
@startuml
!include https://f.umlcn.com/c4.puml
class "This is my class" as class1
class class2 as "It works this way too"
class2 *-- "foo/dummy" : use
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
class "This is my class" as class1
class class2 as "It works this way too"
class2 *-- "foo/dummy" : use
@enduml
2
3
4
5
6
7
8
如果一个别名被分配给一个元素,文件的其余部分必须用别名而不是名称来指代该元素。
$
开始
开始的名称注意,以$
开始的名称以后不能被隐藏或删除,因为hide
和remove
命令会认为该名称是$tag
而不是一个组件名称。要想以后删除这些元素,它们必须有一个别名或必须被标记。
@startuml
!include https://f.umlcn.com/c4.puml
class $C1
class $C2 $C2
class "$C2" as dollarC2
remove $C1
remove $C2
remove dollarC2
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
class $C1
class $C2 $C2
class "$C2" as dollarC2
remove $C1
remove $C2
remove dollarC2
@enduml
2
3
4
5
6
7
8
9
10
还要注意的是,以$
开始的名字是有效的,但是要给这样的元素分配一个别名,必须把名字放在引号""
之间。
添加方法
要声明属性和方法,你可以使用符号:
,后面跟字段或方法的名称。
编译器会通过检查括号来选择方法和字段。
@startuml
!include https://f.umlcn.com/c4.puml
Object <|-- ArrayList
Object : equals()
ArrayList : Object[] elementData
ArrayList : size()
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
Object <|-- ArrayList
Object : equals()
ArrayList : Object[] elementData
ArrayList : size()
@enduml
2
3
4
5
6
7
8
9
10
花括号{}
为所有属性和方法分组
注意,语法对类型/名称的顺序有很大的灵活性。
@startuml
!include https://f.umlcn.com/c4.puml
class Dummy {
String data
void methods()
}
class Flight {
flightNumber : Integer
departureTime : Date
}
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
class Dummy {
String data
void methods()
}
class Flight {
flightNumber : Integer
departureTime : Date
}
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
你可以使用{field}
和{method}
修饰符来覆盖编译器对属性和方法的默认识别。
@startuml
!include https://f.umlcn.com/c4.puml
class Dummy {
{field} A field (despite parentheses)
{method} Some method
}
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
class Dummy {
{field} A field (despite parentheses)
{method} Some method
}
@enduml
2
3
4
5
6
7
8
9
定义能见度(可访问性)
当你定义属性或者方法时,你可以使用特殊符号定义相应条目的可访问性质。
字符 | 图标(属性) | 图标(方法) | 可访问性 |
---|---|---|---|
- | private 私有 | ||
# | protected 受保护 | ||
~ | package private 包内可见 | ||
+ | public 公有 |
@startuml
!include https://f.umlcn.com/c4.puml
class Dummy {
-field1
#field2
~method1()
+method2()
}
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
class Dummy {
-field1
#field2
~method1()
+method2()
}
@enduml
2
3
4
5
6
7
8
9
10
11
12
你可以采用命令 skinparam classAttributeIconSize 0
来展示特殊符号本身:
@startuml
!include https://f.umlcn.com/c4.puml
skinparam classAttributeIconSize 0
class Dummy {
-field1
#field2
~method1()
+method2()
}
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
skinparam classAttributeIconSize 0
class Dummy {
-field1
#field2
~method1()
+method2()
}
@enduml
2
3
4
5
6
7
8
9
10
11
12
抽象与静态
通过修饰符{static}
或者{abstract}
,可以定义静态或者抽象的方法或者属性。
这些修饰符可以写在行的开始或者结束。也可以使用{classifier}
这个修饰符来代替{static}
.
@startuml
!include https://f.umlcn.com/c4.puml
class Dummy {
{static} String id
{abstract} void methods()
}
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
class Dummy {
{static} String id
{abstract} void methods()
}
@enduml
2
3
4
5
6
7
8
高级类体
PlantUML默认自动将方法和属性重新分组,你可以自己定义分隔符来重排方法和属性,下面的分隔符都是可用的:--
..
==
__
.
还可以在分隔符中添加标题:
@startuml
!include https://f.umlcn.com/c4.puml
class Foo1 {
You can use
several lines
..
as you want
and group
==
things together.
__
You can have as many groups
as you want
--
End of class
}
class User {
.. Simple Getter ..
+ getName()
+ getAddress()
.. Some setter ..
+ setName()
__ private data __
int age
-- encrypted --
String password
}
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
class Foo1 {
You can use
several lines
..
as you want
and group
==
things together.
__
You can have as many groups
as you want
--
End of class
}
class User {
.. Simple Getter ..
+ getName()
+ getAddress()
.. Some setter ..
+ setName()
__ private data __
int age
-- encrypted --
String password
}
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
备注和版型
版型通过类关键字("<<
"和">>
")来定义
你可以使用note left of
, note right of
, note top of
, note bottom of
这些关键字来添加备注。
你还可以在类的声明末尾使用note left
, note right
,note top
, note bottom
来添加。
此外,单独用note
这个关键字也是可以的,使用 ..
符号可以作出一条连接它与其它对象的虚线。
@startuml
!include https://f.umlcn.com/c4.puml
class Object << general >>
Object <|--- ArrayList
note top of Object : In java, every class\nextends this one.
note "This is a floating note" as N1
note "This note is connected\nto several objects." as N2
Object .. N2
N2 .. ArrayList
class Foo
note left: On last defined class
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
class Object << general >>
Object <|--- ArrayList
note top of Object : In java, every class\nextends this one.
note "This is a floating note" as N1
note "This note is connected\nto several objects." as N2
Object .. N2
N2 .. ArrayList
class Foo
note left: On last defined class
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
备注中的更多功能
可以在注释中使用部分html标签 (See Creole expression):
<b>
<u>
<i>
<s>
,<del>
,<strike>
<font color="#AAAAAA">
or<font color="colorName">
<color:#AAAAAA>
or<color:colorName>
<size:nn>
to change font size<img src="file">
or<img:file>
: the file must be accessible by the filesystem
你也可以在注释中展示多行。
你也可以在定义的class之后直接使用 note left
, note right
, note top
, note bottom
来定义注释。
@startuml
!include https://f.umlcn.com/c4.puml
class Foo
note left: On last defined class
note top of Foo
In java, <size:18>every</size> <u>class</u>
<b>extends</b>
<i>this</i> one.
end note
note as N1
This note is <u>also</u>
<b><color:royalBlue>on several</color>
<s>words</s> lines
And this is hosted by <img:sourceforge.jpg>
end note
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
class Foo
note left: On last defined class
note top of Foo
In java, <size:18>every</size> <u>class</u>
<b>extends</b>
<i>this</i> one.
end note
note as N1
This note is <u>also</u>
<b><color:royalBlue>on several</color>
<s>words</s> lines
And this is hosted by <img:sourceforge.jpg>
end note
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
注释属性 (field, attribute, member) 或方法
可以在属性(field、attribute、member)或方法上添加注释。
⚠ 注意
- 不能与
top
和bottom
同时使用 (只支持left
和right
) - 不能与表示命名空间的分隔符
::
同时使用
注释属性或方法
@startuml
!include https://f.umlcn.com/c4.puml
class A {
{static} int counter
+void {abstract} start(int timeout)
}
note left of A::counter
该成员已注释
end note
note right of A::start
在 UML 注释了此方法
end note
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
class A {
{static} int counter
+void {abstract} start(int timeout)
}
note left of A::counter
该成员已注释
end note
note right of A::start
在 UML 注释了此方法
end note
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
给同名方法注释
@startuml
!include https://f.umlcn.com/c4.puml
class A {
{static} int counter
+void {abstract} start(int timeoutms)
+void {abstract} start(Duration timeout)
}
note left of A::counter
该成员已注释
end note
note right of A::"start(int timeoutms)"
这个start方法的参数是int类型
end note
note right of A::"start(Duration timeout)"
这个start方法的参数是Duration类型
end note
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
class A {
{static} int counter
+void {abstract} start(int timeoutms)
+void {abstract} start(Duration timeout)
}
note left of A::counter
该成员已注释
end note
note right of A::"start(int timeoutms)"
这个start方法的参数是int类型
end note
note right of A::"start(Duration timeout)"
这个start方法的参数是Duration类型
end note
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
链接的注释
在定义链接之后,你可以用 note on link
给链接添加注释
如果想要改变注释相对于标签的位置,你也可以用 note left on link
, note right on link
, note bottom on link
。(对应位置分别在label的左边,右边,下边)
@startuml
!include https://f.umlcn.com/c4.puml
class Dummy
Dummy --> Foo : A link
note on link #red: note that is red
Dummy --> Foo2 : Another link
note right on link #blue
this is my note on right link
and in blue
end note
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
class Dummy
Dummy --> Foo : A link
note on link #red: note that is red
Dummy --> Foo2 : Another link
note right on link #blue
this is my note on right link
and in blue
end note
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
抽象类和接口
用关键字abstract
或abstract class
来定义抽象类。
抽象类用斜体显示。
也可以使用interface
, annotation
和 enum
等关键字。
@startuml
!include https://f.umlcn.com/c4.puml
abstract class AbstractList
abstract AbstractCollection
interface List
interface Collection
List <|-- AbstractList
Collection <|-- AbstractCollection
Collection <|- List
AbstractCollection <|- AbstractList
AbstractList <|-- ArrayList
class ArrayList {
Object[] elementData
size()
}
enum TimeUnit {
DAYS
HOURS
MINUTES
}
annotation SuppressWarnings
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
abstract class AbstractList
abstract AbstractCollection
interface List
interface Collection
List <|-- AbstractList
Collection <|-- AbstractCollection
Collection <|- List
AbstractCollection <|- AbstractList
AbstractList <|-- ArrayList
class ArrayList {
Object[] elementData
size()
}
enum TimeUnit {
DAYS
HOURS
MINUTES
}
annotation SuppressWarnings
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
隐藏属性、函数等
通过使用命令“hide/show
”,你可以用参数表示类的显示方式。
基础命令是: hide empty members
. 这个命令会隐藏空白的方法和属性。
除 empty members
外,你可以用:
empty fields
或者empty attributes
空属性,empty methods
空函数,fields
或attributes
隐藏字段或属性,即使是被定义了methods
隐藏方法,即使是被定义了members
隐藏字段 和 方法,即使是被定义了circle
类名前带圈的,stereotype
原型。
同样可以使用 hide
或 show
关键词,对以下内容进行设置:
class
所有类,interface
所有接口,enum
所有枚举,<<foo1>>
实现 foo1 的类,- 一个既定的类名。
你可以使用 show/hide
命令来定义相关规则和例外。
@startuml
!include https://f.umlcn.com/c4.puml
class Dummy1 {
+myMethods()
}
class Dummy2 {
+hiddenMethod()
}
class Dummy3 <<Serializable>> {
String name
}
hide members
hide <<Serializable>> circle
show Dummy1 methods
show <<Serializable>> fields
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
class Dummy1 {
+myMethods()
}
class Dummy2 {
+hiddenMethod()
}
class Dummy3 <<Serializable>> {
String name
}
hide members
hide <<Serializable>> circle
show Dummy1 methods
show <<Serializable>> fields
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
隐藏类
你也可以使用 show/hide
命令来隐藏类
如果你定义了一个大的!included 文件,且想在文件包含之后隐藏部分类,该功能会很有帮助。
@startuml
!include https://f.umlcn.com/c4.puml
class Foo1
class Foo2
Foo2 *-- Foo1
hide Foo2
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
class Foo1
class Foo2
Foo2 *-- Foo1
hide Foo2
@enduml
2
3
4
5
6
7
8
9
10
11
12
删除类
您还可以使用remove
命令来删除类。
如果您定义了一个大的 [!included file](预处理), 并且如果您想在 [file contains](预处理)之后删除一些类,这可能很有用。
@startuml
!include https://f.umlcn.com/c4.puml
class Foo1
class Foo2
Foo2 *-- Foo1
remove Foo2
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
class Foo1
class Foo2
Foo2 *-- Foo1
remove Foo2
@enduml
2
3
4
5
6
7
8
9
10
11
12
Hide, Remove or Restore tagged element or wildcard
You can put $tags
(using $
) on elements, then remove, hide or restore components either individually or by tags.
By default, all components are displayed:
原图:
@startuml
!include https://f.umlcn.com/c4.puml
class C1 $tag13
enum E1
interface I1 $tag13
C1 -- I1
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
class C1 $tag13
enum E1
interface I1 $tag13
C1 -- I1
@enduml
2
3
4
5
6
7
8
But you can:
hide $tag13
components:
@startuml
!include https://f.umlcn.com/c4.puml
class C1 $tag13
enum E1
interface I1 $tag13
C1 -- I1
hide $tag13
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
class C1 $tag13
enum E1
interface I1 $tag13
C1 -- I1
hide $tag13
@enduml
2
3
4
5
6
7
8
9
10
- or
remove $tag13
components:
@startuml
!include https://f.umlcn.com/c4.puml
class C1 $tag13
enum E1
interface I1 $tag13
C1 -- I1
remove $tag13
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
class C1 $tag13
enum E1
interface I1 $tag13
C1 -- I1
remove $tag13
@enduml
2
3
4
5
6
7
8
9
10
remove $tag13 and restore $tag1
components:
@startuml
!include https://f.umlcn.com/c4.puml
class C1 $tag13 $tag1
enum E1
interface I1 $tag13
C1 -- I1
remove $tag13
restore $tag1
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
class C1 $tag13 $tag1
enum E1
interface I1 $tag13
C1 -- I1
remove $tag13
restore $tag1
@enduml
2
3
4
5
6
7
8
9
10
11
remove * and restore $tag1
components:
@startuml
!include https://f.umlcn.com/c4.puml
class C1 $tag13 $tag1
enum E1
interface I1 $tag13
C1 -- I1
remove *
restore $tag1
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
class C1 $tag13 $tag1
enum E1
interface I1 $tag13
C1 -- I1
remove *
restore $tag1
@enduml
2
3
4
5
6
7
8
9
10
11
隐藏或删除未关联的类
默认情况下, 所有的类都将会展示:
@startuml
!include https://f.umlcn.com/c4.puml
class C1
class C2
class C3
C1 -- C2
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
class C1
class C2
class C3
C1 -- C2
@enduml
2
3
4
5
6
7
8
不过你可以使用:
hide @unlinked
来隐藏未关联的类:
@startuml
!include https://f.umlcn.com/c4.puml
class C1
class C2
class C3
C1 -- C2
hide @unlinked
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
class C1
class C2
class C3
C1 -- C2
hide @unlinked
@enduml
2
3
4
5
6
7
8
9
10
或者使用 remove @unlinked
来删除未关联的类:
@startuml
!include https://f.umlcn.com/c4.puml
class C1
class C2
class C3
C1 -- C2
remove @unlinked
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
class C1
class C2
class C3
C1 -- C2
remove @unlinked
@enduml
2
3
4
5
6
7
8
9
10
泛型(generics)
你可以用 <
和 >
来定义类的泛型。
@startuml
!include https://f.umlcn.com/c4.puml
class Foo<? extends Element> {
int size()
}
Foo *- Element
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
class Foo<? extends Element> {
int size()
}
Foo *- Element
@enduml
2
3
4
5
6
7
8
9
10
It is possible to disable this drawing using skinparam genericDisplay old
command.
指定标记(Spot)
通常标记字符 (C, I, E or A) 用于标记 类(classes), 接口(interface), 枚举(enum)和 抽象类(abstract classes).
但是当你想定义原型时,可以增加对应的单个字符及颜色,来定义自己的标记(spot),就像下面一样:
@startuml
!include https://f.umlcn.com/c4.puml
class System << (S,#FF7700) Singleton >>
class Date << (D,orchid) >>
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
class System << (S,#FF7700) Singleton >>
class Date << (D,orchid) >>
@enduml
2
3
4
5
6
7
包
你可以通过关键词 package
声明包,同时可选的来声明对应的背景色(通过使用html色彩代码或名称)。
注意:包可以被定义为嵌套。
@startuml
!include https://f.umlcn.com/c4.puml
package "Classic Collections" #DDDDDD {
Object <|-- ArrayList
}
package net.sourceforge.plantuml {
Object <|-- Demo1
Demo1 *- Demo2
}
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
package "Classic Collections" #DDDDDD {
Object <|-- ArrayList
}
package net.sourceforge.plantuml {
Object <|-- Demo1
Demo1 *- Demo2
}
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
包样式
包可以定义不同的样式。
你可以通过以下的命令来设置默认样式 : skinparam packageStyle
,或者对包使用对应的模板:
@startuml
!include https://f.umlcn.com/c4.puml
scale 750 width
package foo1 <<Node>> {
class Class1
}
package foo2 <<Rectangle>> {
class Class2
}
package foo3 <<Folder>> {
class Class3
}
package foo4 <<Frame>> {
class Class4
}
package foo5 <<Cloud>> {
class Class5
}
package foo6 <<Database>> {
class Class6
}
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
scale 750 width
package foo1 <<Node>> {
class Class1
}
package foo2 <<Rectangle>> {
class Class2
}
package foo3 <<Folder>> {
class Class3
}
package foo4 <<Frame>> {
class Class4
}
package foo5 <<Cloud>> {
class Class5
}
package foo6 <<Database>> {
class Class6
}
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
参考下面的示例来定义包之间的连线
@startuml
!include https://f.umlcn.com/c4.puml
skinparam packageStyle rectangle
package foo1.foo2 {
}
package foo1.foo2.foo3 {
class Object
}
foo1.foo2 +-- foo1.foo2.foo3
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
skinparam packageStyle rectangle
package foo1.foo2 {
}
package foo1.foo2.foo3 {
class Object
}
foo1.foo2 +-- foo1.foo2.foo3
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
命名空间(Namespaces)
在使用包(package)时(区别于命名空间),类名是类的唯一标识。 也就意味着,在不同的包(package)中的类,不能使用相同的类名。
在那种情况下(译注:同名、不同全限定名类),你应该使用命名空间来取而代之。
你可以从其他命名空间,使用全限定名来引用类, 默认命名空间(译注:无名的命名空间)下的类,以一个“."开头(的类名)来引用(译注:示例中的BaseClass
).
注意:你不用显示地创建命名空间:一个使用全限定名的类会自动被放置到对应的命名空间。
@startuml
!include https://f.umlcn.com/c4.puml
class BaseClass
namespace net.dummy #DDDDDD {
.BaseClass <|-- Person
Meeting o-- Person
.BaseClass <|- Meeting
}
namespace net.foo {
net.dummy.Person <|- Person
.BaseClass <|-- Person
net.dummy.Meeting o-- Person
}
BaseClass <|-- net.unused.Person
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
class BaseClass
namespace net.dummy #DDDDDD {
.BaseClass <|-- Person
Meeting o-- Person
.BaseClass <|- Meeting
}
namespace net.foo {
net.dummy.Person <|- Person
.BaseClass <|-- Person
net.dummy.Meeting o-- Person
}
BaseClass <|-- net.unused.Person
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
自动创建命名空间
使用命令 set namespaceSeparator ???
你可以自定义命名空间分隔符(为 “.” 以外的字符).
@startuml
!include https://f.umlcn.com/c4.puml
set namespaceSeparator ::
class X1::X2::foo {
some info
}
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
set namespaceSeparator ::
class X1::X2::foo {
some info
}
@enduml
2
3
4
5
6
7
8
9
10
禁止自动创建包则可以使用 set namespaceSeparator none
.
@startuml
!include https://f.umlcn.com/c4.puml
set namespaceSeparator none
class X1.X2.foo {
some info
}
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
set namespaceSeparator none
class X1.X2.foo {
some info
}
@enduml
2
3
4
5
6
7
8
9
10
棒棒糖接口
需要定义棒棒糖样式的接口时可以遵循以下语法:
@startuml
!include https://f.umlcn.com/c4.puml
class foo类
bar ()- foo类
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
class foo类
bar ()- foo类
@enduml
2
3
4
5
6
改变箭头方向
类之间默认采用两个破折号 --
显示出垂直 方向的线. 要得到水平方向的可以像这样使用单破折号 (或者点):
@startuml
!include https://f.umlcn.com/c4.puml
教室 o- 学生
教室 *-- 椅子
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
教室 o- 学生
教室 *-- 椅子
@enduml
2
3
4
5
6
你也可以通过改变倒置链接来改变方向
@startuml
!include https://f.umlcn.com/c4.puml
学生 -o 教室
椅子 --* 教室
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
学生 -o 教室
椅子 --* 教室
@enduml
2
3
4
5
6
使用关键字例如left
, right
, up
或者 down
,来改变方向
@startuml
!include https://f.umlcn.com/c4.puml
foo -left-> dummyLeft
foo -right-> dummyRight
foo -up-> dummyUp
foo -down-> dummyDown
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
foo -left-> dummyLeft
foo -right-> dummyRight
foo -up-> dummyUp
foo -down-> dummyDown
@enduml
2
3
4
5
6
7
8
您可以使用缩写形式来表示方向,第一个字符(例如,-d-
而不是 -down-
) 或前两个字符 (-do-
)。
请注意,您不应滥用此功能:Graphviz 通常无需调整即可提供良好的结果。
同时也支持 left to right direction
参数:
@startuml
!include https://f.umlcn.com/c4.puml
left to right direction
foo -left-> dummyLeft
foo -right-> dummyRight
foo -up-> dummyUp
foo -down-> dummyDown
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
left to right direction
foo -left-> dummyLeft
foo -right-> dummyRight
foo -up-> dummyUp
foo -down-> dummyDown
@enduml
2
3
4
5
6
7
8
9
“关系”类
你可以在定义了两个类之间的关系后定义一个 关系类 association class 例如:
@startuml
!include https://f.umlcn.com/c4.puml
class Student {
Name
}
Student "0..*" - "1..*" Course
(Student, Course) .. Enrollment
class Enrollment {
drop()
cancel()
}
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
class Student {
Name
}
Student "0..*" - "1..*" Course
(Student, Course) .. Enrollment
class Enrollment {
drop()
cancel()
}
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
也可以用另一种方式
@startuml
!include https://f.umlcn.com/c4.puml
class Student {
Name
}
Student "0..*" -- "1..*" Course
(Student, Course) . Enrollment
class Enrollment {
drop()
cancel()
}
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
class Student {
Name
}
Student "0..*" -- "1..*" Course
(Student, Course) . Enrollment
class Enrollment {
drop()
cancel()
}
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
同级关联(Association on same classe)
@startuml
!include https://f.umlcn.com/c4.puml
class 站台 {
+名称: 字符串
}
class 通道 {
+花费: 剩余时间
}
<> 结点
通道 . 结点
结点 - "从 0..*" 站台
结点 - "到 0..* " 站台
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
class 站台 {
+名称: 字符串
}
class 通道 {
+花费: 剩余时间
}
<> 结点
通道 . 结点
结点 - "从 0..*" 站台
结点 - "到 0..* " 站台
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
样式参数
用skinparam改变字体和颜色。
可以在如下场景中使用:
@startuml
!include https://f.umlcn.com/c4.puml
skinparam class {
BackgroundColor PaleGreen
ArrowColor SeaGreen
BorderColor SpringGreen
}
skinparam stereotypeCBackgroundColor YellowGreen
类01 "1" *-- "many" 类02 : 包含
类03 o-- 类04 : 聚合
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
skinparam class {
BackgroundColor PaleGreen
ArrowColor SeaGreen
BorderColor SpringGreen
}
skinparam stereotypeCBackgroundColor YellowGreen
类01 "1" *-- "many" 类02 : 包含
类03 o-- 类04 : 聚合
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
模板样式
你可以给模型自定义颜色和字体(You can define specific color and fonts for stereotyped classes.)
@startuml
!include https://f.umlcn.com/c4.puml
skinparam class {
BackgroundColor PaleGreen
ArrowColor SeaGreen
BorderColor SpringGreen
BackgroundColor<<Foo>> Wheat
BorderColor<<Foo>> Tomato
}
skinparam stereotypeCBackgroundColor YellowGreen
skinparam stereotypeCBackgroundColor<< Foo >> DimGray
类1 <<Foo>>
类3 <<Foo>>
类1 "1" *-- "*" 类2 : 包含(contains)
类3 o-- 类4 : 聚合(aggregation)
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
skinparam class {
BackgroundColor PaleGreen
ArrowColor SeaGreen
BorderColor SpringGreen
BackgroundColor<<Foo>> Wheat
BorderColor<<Foo>> Tomato
}
skinparam stereotypeCBackgroundColor YellowGreen
skinparam stereotypeCBackgroundColor<< Foo >> DimGray
类1 <<Foo>>
类3 <<Foo>>
类1 "1" *-- "*" 类2 : 包含(contains)
类3 o-- 类4 : 聚合(aggregation)
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
渐变颜色
你可以使用 # 号为类、注释等等自定义颜色。
在自定义颜色中你可以使用标准颜色的名称 或 RGB 编码,参见: Colors.
你同样可以使用下面的语法为背景色声明为渐变的颜色:
渐变的两个颜色可以使用下面的符号分割:
|
,/
,\
,-
,
他们的区别在于颜色渐变的方向不同。
例如:
@startuml
!include https://f.umlcn.com/c4.puml
skinparam backgroundcolor AntiqueWhite/Gold
skinparam classBackgroundColor Wheat|CornflowerBlue
class 类1 #red-green
note left of 类1 #blue\9932CC
这是foo类的
渐变颜色
end note
package 包1 #GreenYellow/LightGoldenRodYellow {
class 类2
}
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
skinparam backgroundcolor AntiqueWhite/Gold
skinparam classBackgroundColor Wheat|CornflowerBlue
class 类1 #red-green
note left of 类1 #blue\9932CC
这是foo类的
渐变颜色
end note
package 包1 #GreenYellow/LightGoldenRodYellow {
class 类2
}
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
辅助布局
有时候,默认布局并不完美...
你可以使用 together
关键词将某些类进行分组: 布局引擎会尝试将它们捆绑在一起(如同在一个包(package)内)
你也可以使用建立 hidden
链接的方式来强制布局
@startuml
!include https://f.umlcn.com/c4.puml
class Bar1
class Bar2
together {
class Together1
class Together2
class Together3
}
Together1 - Together2
Together2 - Together3
Together2 -[hidden]--> Bar1
Bar1 -[hidden]> Bar2
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
class Bar1
class Bar2
together {
class Together1
class Together2
class Together3
}
Together1 - Together2
Together2 - Together3
Together2 -[hidden]--> Bar1
Bar1 -[hidden]> Bar2
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
拆分大文件
有些情况下,会有一些很大的图片文件。
可以用 page (hpages)x(vpages)
这个命令把生成的图片文件拆分成若干个文件。
hpages
用来表示水平方向页面数, and vpages
用来表示垂直方面页面数。
你也可以使用特定的皮肤设定来给分页添加边框(见例子)
@startuml
!include https://f.umlcn.com/c4.puml
' Split into 4 pages
page 2x2
skinparam pageMargin 10
skinparam pageExternalColor gray
skinparam pageBorderColor black
class BaseClass
namespace net.dummy #DDDDDD {
.BaseClass <|-- Person
Meeting o-- Person
.BaseClass <|- Meeting
}
namespace net.foo {
net.dummy.Person <|- Person
.BaseClass <|-- Person
net.dummy.Meeting o-- Person
}
BaseClass <|-- net.unused.Person
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
' Split into 4 pages
page 2x2
skinparam pageMargin 10
skinparam pageExternalColor gray
skinparam pageBorderColor black
class BaseClass
namespace net.dummy #DDDDDD {
.BaseClass <|-- Person
Meeting o-- Person
.BaseClass <|- Meeting
}
namespace net.foo {
net.dummy.Person <|- Person
.BaseClass <|-- Person
net.dummy.Meeting o-- Person
}
BaseClass <|-- net.unused.Person
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
继承(Extends) 和 实现(implements)
同样可使用 extends
和 implements
关键词.
@startuml
!include https://f.umlcn.com/c4.puml
class ArrayList implements List
class ArrayList extends AbstractList
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
class ArrayList implements List
class ArrayList extends AbstractList
@enduml
2
3
4
5
6
方括号表示关系(连接或箭头)的样式
线样式
可以明确的使用 bold
, dashed
, dotted
, hidden
或 plain
来表示关系, 连接或箭头:
- 没有标签
@startuml
!include https://f.umlcn.com/c4.puml
title 使用方括号([])表示的线样式(无标签版)
class foo类
class bar类
bar类1 : [bold]
bar类2 : [dashed]
bar类3 : [dotted]
bar类4 : [hidden]
bar类5 : [plain]
foo类 --> bar类
foo类 -[bold]-> bar类1
foo类 -[dashed]-> bar类2
foo类 -[dotted]-> bar类3
foo类 -[hidden]-> bar类4
foo类 -[plain]-> bar类5
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
title 使用方括号([])表示的线样式(无标签版)
class foo类
class bar类
bar类1 : [bold]
bar类2 : [dashed]
bar类3 : [dotted]
bar类4 : [hidden]
bar类5 : [plain]
foo类 --> bar类
foo类 -[bold]-> bar类1
foo类 -[dashed]-> bar类2
foo类 -[dotted]-> bar类3
foo类 -[hidden]-> bar类4
foo类 -[plain]-> bar类5
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
- 有标签
@startuml
!include https://f.umlcn.com/c4.puml
title 使用方括号([])表示的线样式(有标签版)
class foo类
class bar类
bar类1 : [bold]
bar类2 : [dashed]
bar类3 : [dotted]
bar类4 : [hidden]
bar类5 : [plain]
foo类 --> bar类 : ∅
foo类 -[bold]-> bar类1 : [bold]
foo类 -[dashed]-> bar类2 : [dashed]
foo类 -[dotted]-> bar类3 : [dotted]
foo类 -[hidden]-> bar类4 : [hidden]
foo类 -[plain]-> bar类5 : [plain]
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
title 使用方括号([])表示的线样式(有标签版)
class foo类
class bar类
bar类1 : [bold]
bar类2 : [dashed]
bar类3 : [dotted]
bar类4 : [hidden]
bar类5 : [plain]
foo类 --> bar类 : ∅
foo类 -[bold]-> bar类1 : [bold]
foo类 -[dashed]-> bar类2 : [dashed]
foo类 -[dotted]-> bar类3 : [dotted]
foo类 -[hidden]-> bar类4 : [hidden]
foo类 -[plain]-> bar类5 : [plain]
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
线颜色
@startuml
!include https://f.umlcn.com/c4.puml
title 使用方括号([])表示的线颜色
class foo类
class bar类
bar类1 : [#red]
bar类2 : [#green]
bar类3 : [#blue]
foo类 --> bar类
foo类 -[#red]-> bar类1 : [#red]
foo类 -[#green]-> bar类2 : [#green]
foo类 -[#blue]-> bar类3 : [#blue]
'foo类 -[#blue;#yellow;#green]-> bar类4
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
title 使用方括号([])表示的线颜色
class foo类
class bar类
bar类1 : [#red]
bar类2 : [#green]
bar类3 : [#blue]
foo类 --> bar类
foo类 -[#red]-> bar类1 : [#red]
foo类 -[#green]-> bar类2 : [#green]
foo类 -[#blue]-> bar类3 : [#blue]
'foo类 -[#blue;#yellow;#green]-> bar类4
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
线宽度
@startuml
!include https://f.umlcn.com/c4.puml
title 使用方括号([])表示的线宽度
class foo类
class bar类
bar类1 : [thickness=1]
bar类2 : [thickness=2]
bar类3 : [thickness=4]
bar类4 : [thickness=8]
bar类5 : [thickness=16]
foo类 --> bar类 : ∅
foo类 -[thickness=1]-> bar类1 : [1]
foo类 -[thickness=2]-> bar类2 : [2]
foo类 -[thickness=4]-> bar类3 : [4]
foo类 -[thickness=8]-> bar类4 : [8]
foo类 -[thickness=16]-> bar类5 : [16]
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
title 使用方括号([])表示的线宽度
class foo类
class bar类
bar类1 : [thickness=1]
bar类2 : [thickness=2]
bar类3 : [thickness=4]
bar类4 : [thickness=8]
bar类5 : [thickness=16]
foo类 --> bar类 : ∅
foo类 -[thickness=1]-> bar类1 : [1]
foo类 -[thickness=2]-> bar类2 : [2]
foo类 -[thickness=4]-> bar类3 : [4]
foo类 -[thickness=8]-> bar类4 : [8]
foo类 -[thickness=16]-> bar类5 : [16]
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
混合样式
@startuml
!include https://f.umlcn.com/c4.puml
title 使用方括号([])表示的线混合样式
class foo类
class bar类
bar类1 : [#red,thickness=1]
bar类2 : [#red,dashed,thickness=2]
bar类3 : [#green,dashed,thickness=4]
bar类4 : [#blue,dotted,thickness=8]
bar类5 : [#blue,plain,thickness=16]
foo类 --> bar类 : ∅
foo类 -[#red,thickness=1]-> bar类1 : [#red,1]
foo类 -[#red,dashed,thickness=2]-> bar类2 : [#red,dashed,2]
foo类 -[#green,dashed,thickness=4]-> bar类3 : [#green,dashed,4]
foo类 -[#blue,dotted,thickness=8]-> bar类4 : [blue,dotted,8]
foo类 -[#blue,plain,thickness=16]-> bar类5 : [blue,plain,16]
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
title 使用方括号([])表示的线混合样式
class foo类
class bar类
bar类1 : [#red,thickness=1]
bar类2 : [#red,dashed,thickness=2]
bar类3 : [#green,dashed,thickness=4]
bar类4 : [#blue,dotted,thickness=8]
bar类5 : [#blue,plain,thickness=16]
foo类 --> bar类 : ∅
foo类 -[#red,thickness=1]-> bar类1 : [#red,1]
foo类 -[#red,dashed,thickness=2]-> bar类2 : [#red,dashed,2]
foo类 -[#green,dashed,thickness=4]-> bar类3 : [#green,dashed,4]
foo类 -[#blue,dotted,thickness=8]-> bar类4 : [blue,dotted,8]
foo类 -[#blue,plain,thickness=16]-> bar类5 : [blue,plain,16]
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
改变关系(线和箭头)的颜色和样式(单行样式)
你可以改变表示关系的线和箭头的颜色或样式,使用下面的单行样式格式:
#color;line.[bold|dashed|dotted];text:color
@startuml
!include https://f.umlcn.com/c4.puml
class foo类
foo类 --> bar类 : 默认
foo类 --> bar类1 #line:red;line.bold;text:red : 红色粗线
foo类 --> bar类2 #green;line.dashed;text:green : 绿色断线
foo类 --> bar类3 #blue;line.dotted;text:blue : 蓝色点线
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
class foo类
foo类 --> bar类 : 默认
foo类 --> bar类1 #line:red;line.bold;text:red : 红色粗线
foo类 --> bar类2 #green;line.dashed;text:green : 绿色断线
foo类 --> bar类3 #blue;line.dotted;text:blue : 蓝色点线
@enduml
2
3
4
5
6
7
8
9
改变类颜色和样式 (单行样式)
你可以改变定义类的 颜色 或样式, 通过下面两种指定格式:
#color ##[style]color
第一个颜色(#color
)表示背景色,然后第二个表示线的样式和颜色(##[style]color
)
@startuml
!include https://f.umlcn.com/c4.puml
abstract 抽象类
annotation 标注 #pink ##[bold]red
class 类 #palegreen ##[dashed]green
interface 接口 #aliceblue ##[dotted]blue
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
abstract 抽象类
annotation 标注 #pink ##[bold]red
class 类 #palegreen ##[dashed]green
interface 接口 #aliceblue ##[dotted]blue
@enduml
2
3
4
5
6
7
8
#[color|back:color];header:color;line:color;line.[bold|dashed|dotted];text:color
@startuml
!include https://f.umlcn.com/c4.puml
abstract 抽象类
annotation 标注 #pink;line:red;line.bold;text:red
class 类 #palegreen;line:green;line.dashed;text:green
interface 接口 #aliceblue;line:blue;line.dotted;text:blue
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
abstract 抽象类
annotation 标注 #pink;line:red;line.bold;text:red
class 类 #palegreen;line:green;line.dashed;text:green
interface 接口 #aliceblue;line:blue;line.dotted;text:blue
@enduml
2
3
4
5
6
7
8
第一个原始示例:
@startuml
!include https://f.umlcn.com/c4.puml
class bar类 #line:green;back:lightblue
class bar类2 #lightblue;line:green
class Foo类1 #back:red;line:00FFFF
class Foo类Dashed #line.dashed:blue
class Foo类Dotted #line.dotted:blue
class Foo类Bold #line.bold
class Demo类1 #back:lightgreen|yellow;header:blue/red
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
class bar类 #line:green;back:lightblue
class bar类2 #lightblue;line:green
class Foo类1 #back:red;line:00FFFF
class Foo类Dashed #line.dashed:blue
class Foo类Dotted #line.dotted:blue
class Foo类Bold #line.bold
class Demo类1 #back:lightgreen|yellow;header:blue/red
@enduml
2
3
4
5
6
7
8
9
10
11
12
类成员的箭头方向(Arrows from/to class members)
@startuml
!include https://f.umlcn.com/c4.puml
class Foo类 {
+ 字段1
+ 字段2
}
class Bar类 {
+ 字段3
+ 字段4
}
Foo类::字段1 --> Bar类::字段3 : foo
Foo类::字段2 --> Bar类::字段4 : bar
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
class Foo类 {
+ 字段1
+ 字段2
}
class Bar类 {
+ 字段3
+ 字段4
}
Foo类::字段1 --> Bar类::字段3 : foo
Foo类::字段2 --> Bar类::字段4 : bar
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@startuml
!include https://f.umlcn.com/c4.puml
left to right direction
class User类 {
id : INTEGER
..
其他id : INTEGER
}
class Email类 {
id : INTEGER
..
用户_id : INTEGER
地址 : STRING
}
User类::id *-- Email类::用户_id
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
left to right direction
class User类 {
id : INTEGER
..
其他id : INTEGER
}
class Email类 {
id : INTEGER
..
用户_id : INTEGER
地址 : STRING
}
User类::id *-- Email类::用户_id
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
分组继承关系(Grouping inheritance arrow heads)
你可以用skinparam groupInheritance
关键字合并泛化箭头, 后接参数合并阈值(从几个继承类时开始合并)。
GroupInheritance 1 (不合并)
@startuml
!include https://f.umlcn.com/c4.puml
skinparam groupInheritance 1
A1 <|-- B1
A2 <|-- B2
A2 <|-- C2
A3 <|-- B3
A3 <|-- C3
A3 <|-- D3
A4 <|-- B4
A4 <|-- C4
A4 <|-- D4
A4 <|-- E4
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
skinparam groupInheritance 1
A1 <|-- B1
A2 <|-- B2
A2 <|-- C2
A3 <|-- B3
A3 <|-- C3
A3 <|-- D3
A4 <|-- B4
A4 <|-- C4
A4 <|-- D4
A4 <|-- E4
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
GroupInheritance 2 (从2个组开始合并)
@startuml
!include https://f.umlcn.com/c4.puml
skinparam groupInheritance 2
A1 <|-- B1
A2 <|-- B2
A2 <|-- C2
A3 <|-- B3
A3 <|-- C3
A3 <|-- D3
A4 <|-- B4
A4 <|-- C4
A4 <|-- D4
A4 <|-- E4
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
skinparam groupInheritance 2
A1 <|-- B1
A2 <|-- B2
A2 <|-- C2
A3 <|-- B3
A3 <|-- C3
A3 <|-- D3
A4 <|-- B4
A4 <|-- C4
A4 <|-- D4
A4 <|-- E4
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
GroupInheritance 3 (从3个组开始合并)
@startuml
!include https://f.umlcn.com/c4.puml
skinparam groupInheritance 3
A1 <|-- B1
A2 <|-- B2
A2 <|-- C2
A3 <|-- B3
A3 <|-- C3
A3 <|-- D3
A4 <|-- B4
A4 <|-- C4
A4 <|-- D4
A4 <|-- E4
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
skinparam groupInheritance 3
A1 <|-- B1
A2 <|-- B2
A2 <|-- C2
A3 <|-- B3
A3 <|-- C3
A3 <|-- D3
A4 <|-- B4
A4 <|-- C4
A4 <|-- D4
A4 <|-- E4
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
GroupInheritance 4 (从4个组开始合并)
@startuml
!include https://f.umlcn.com/c4.puml
skinparam groupInheritance 4
A1 <|-- B1
A2 <|-- B2
A2 <|-- C2
A3 <|-- B3
A3 <|-- C3
A3 <|-- D3
A4 <|-- B4
A4 <|-- C4
A4 <|-- D4
A4 <|-- E4
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
skinparam groupInheritance 4
A1 <|-- B1
A2 <|-- B2
A2 <|-- C2
A3 <|-- B3
A3 <|-- C3
A3 <|-- D3
A4 <|-- B4
A4 <|-- C4
A4 <|-- D4
A4 <|-- E4
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Display JSON Data on Class or Object diagram
Simple example
@startuml
!include https://f.umlcn.com/c4.puml
class Class
object Object
json JSON {
"fruit":"Apple",
"size":"Large",
"color": ["Red", "Green"]
}
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
class Class
object Object
json JSON {
"fruit":"Apple",
"size":"Large",
"color": ["Red", "Green"]
}
@enduml
2
3
4
5
6
7
8
9
10
11
Packages and Namespaces Enhancement
@startuml
!include https://f.umlcn.com/c4.puml
class A.B.C.D.Z {
}
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
class A.B.C.D.Z {
}
@enduml
2
3
4
5
6
@startuml
!include https://f.umlcn.com/c4.puml
set separator none
class A.B.C.D.Z {
}
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
set separator none
class A.B.C.D.Z {
}
@enduml
2
3
4
5
6
7
@startuml
!include https://f.umlcn.com/c4.puml
!pragma useIntermediatePackages false
class A.B.C.D.Z {
}
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
!pragma useIntermediatePackages false
class A.B.C.D.Z {
}
@enduml
2
3
4
5
6
7
@startuml
!include https://f.umlcn.com/c4.puml
set separator none
package A.B.C.D {
class Z {
}
}
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
set separator none
package A.B.C.D {
class Z {
}
}
@enduml
2
3
4
5
6
7
8
9
Qualified associations
Minimal example
@startuml
!include https://f.umlcn.com/c4.puml
class class1
class class2
class1 [Qualifier] - class2
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
class class1
class class2
class1 [Qualifier] - class2
@enduml
2
3
4
5
6
7
8
Another example
@startuml
!include https://f.umlcn.com/c4.puml
interface Map<K,V>
class HashMap<Long,Customer>
Map <|.. HashMap
Shop [customerId: long] ---> "customer\n1" Customer
HashMap [id: Long] -r-> "value" Customer
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
interface Map<K,V>
class HashMap<Long,Customer>
Map <|.. HashMap
Shop [customerId: long] ---> "customer\n1" Customer
HashMap [id: Long] -r-> "value" Customer
@enduml
2
3
4
5
6
7
8
9
10
Change diagram orientation
You can change (whole) diagram orientation with:
top to bottom direction
(by default)left to right direction
Top to bottom (by default)
With Graphviz (layout engine by default)
The main rule is: Nested element first, then simple element.
@startuml
!include https://f.umlcn.com/c4.puml
class a
class b
package A {
class a1
class a2
class a3
class a4
class a5
package sub_a {
class sa1
class sa2
class sa3
}
}
package B {
class b1
class b2
class b3
class b4
class b5
package sub_b {
class sb1
class sb2
class sb3
}
}
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
class a
class b
package A {
class a1
class a2
class a3
class a4
class a5
package sub_a {
class sa1
class sa2
class sa3
}
}
package B {
class b1
class b2
class b3
class b4
class b5
package sub_b {
class sb1
class sb2
class sb3
}
}
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
he main rule is the opposite: Simple element first, then nested element.
@startuml
!include https://f.umlcn.com/c4.puml
!pragma layout smetana
class a
class b
package A {
class a1
class a2
class a3
class a4
class a5
package sub_a {
class sa1
class sa2
class sa3
}
}
package B {
class b1
class b2
class b3
class b4
class b5
package sub_b {
class sb1
class sb2
class sb3
}
}
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
!pragma layout smetana
class a
class b
package A {
class a1
class a2
class a3
class a4
class a5
package sub_a {
class sa1
class sa2
class sa3
}
}
package B {
class b1
class b2
class b3
class b4
class b5
package sub_b {
class sb1
class sb2
class sb3
}
}
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Left to right
With Graphviz(layout engine by default)
@startuml
!include https://f.umlcn.com/c4.puml
left to right direction
class a
class b
package A {
class a1
class a2
class a3
class a4
class a5
package sub_a {
class sa1
class sa2
class sa3
}
}
package B {
class b1
class b2
class b3
class b4
class b5
package sub_b {
class sb1
class sb2
class sb3
}
}
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
left to right direction
class a
class b
package A {
class a1
class a2
class a3
class a4
class a5
package sub_a {
class sa1
class sa2
class sa3
}
}
package B {
class b1
class b2
class b3
class b4
class b5
package sub_b {
class sb1
class sb2
class sb3
}
}
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
With Smetana(internal layout engine)
@startuml
!include https://f.umlcn.com/c4.puml
!pragma layout smetana
left to right direction
class a
class b
package A {
class a1
class a2
class a3
class a4
class a5
package sub_a {
class sa1
class sa2
class sa3
}
}
package B {
class b1
class b2
class b3
class b4
class b5
package sub_b {
class sb1
class sb2
class sb3
}
}
@enduml
@startuml
!include https://f.umlcn.com/c4.puml
!pragma layout smetana
left to right direction
class a
class b
package A {
class a1
class a2
class a3
class a4
class a5
package sub_a {
class sa1
class sa2
class sa3
}
}
package B {
class b1
class b2
class b3
class b4
class b5
package sub_b {
class sb1
class sb2
class sb3
}
}
@enduml
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33