@startuml
class Car
Driver - Car : drives >
Car *- Wheel : have 4 >
Car -- Person : < owns
@enduml
Label on relations
It is possible to add a label on the relation, using :, followed
by the text of the label.
For cardinality, you can use double-quotes "" on each side
of the relation.
@startuml
class Dummy {
{field} A field (despite parentheses)
{method} Some method
}
@enduml
Adding methods
To declare fields and methods, you can use the symbol
: followed by the field's or method's name.
The system checks for parenthesis to choose between methods and fields.
@startuml
class Dummy {
{static} String id
{abstract} void methods()
}
@enduml
Abstract and Static
You can define static or abstract methods or fields using the {static}
or {abstract} modifier.
These modifiers can be used at the start or at the end of the line.
You can also use {classifier} instead of {static}.
@startuml
class Dummy {
{static} String id
{abstract} void methods()
}
@enduml
@startuml
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
Advanced class body
By default, methods and fields are automatically regrouped by PlantUML.
You can use separators to define your own way of ordering fields and methods.
The following separators are possible : --..==__.
You can also use titles within the separators:
@startuml
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
备注和模板
模板通过类关键字("<<"和">>")来定义
你可以使用note left of , note right of , note top of , note bottom of这些关键字来添加备注。
你还可以在类的声明末尾使用note left, note right,note top, note bottom来添加备注。
此外,单独用note这个关键字也是可以的,使用 .. 符号可以作出一条连接它与其它对象的虚线。
@startuml
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
Notes and stereotypes
Stereotypes are defined with the class keyword, << and >>.
You can also define notes using note left of , note right of , note top of , note bottom of
keywords.
You can also define a note on the last defined class using note left, note right,
note top, note bottom.
A note can be also define alone with the note
keywords, then linked to other objects using the .. symbol.
@startuml
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
更多注释
可以在注释中使用部分html标签:
<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
@startuml
class Foo
note left: On last defined class
note top of Object
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
WARNING
This translation need to be updated.
WARNING
More on notes
It is also possible to use few HTML tags (See Creole expression) like :
<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
You can also have a note on several lines.
You can also define a note on the last defined class using note left, note right,
note top, note bottom.
@startuml
class Foo
note left: On last defined class
note top of Object
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
Note on field (field, attribute, member) or method
It is possible to add a note on field (field, attribut, member) or on method.
Note on field or method
@startuml
class A {
{static} int counter
+void {abstract} start(int timeout)
}
note right of A::counter
This member is annotated
end note
note right of A::start
This method is now explained in a UML note
end note
@enduml
Note on method with the same name
@startuml
class A {
{static} int counter
+void {abstract} start(int timeoutms)
+void {abstract} start(Duration timeout)
}
note left of A::counter
This member is annotated
end note
note right of A::"start(int timeoutms)"
This method with int
end note
note right of A::"start(Duration timeout)"
This method with Duration
end note
@enduml
在定义链接之后,你可以用 note on link 给链接添加注释
如果想要改变注释相对于标签的位置,你也可以用 note left on link, note right on link, note bottom on link。(对应位置分别在label的左边,右边,下边)
@startuml
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
Note on links
It is possible to add a note on a link, just after the link definition, using note on link.
You can also use note left on link, note right on link, note top on link,
note bottom on link if you want to change the relative position of the note with the label.
@startuml
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
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
Abstract class and interface
You can declare a class as abstract using abstract or abstract class keywords.
The class will be printed in italic.
You can use the interface, annotation and enum keywords too.
@startuml
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
class "This is my class" as class1
class class2 as "It works this way too"
class2 *-- "foo/dummy" : use
@enduml
Using non-letters
If you want to use non-letters in the class (or enum...) display, you can either :
Use the as keyword in the class definition
Put quotes "" around the class name
@startuml
class "This is my class" as class1
class class2 as "It works this way too"
class2 *-- "foo/dummy" : use
@enduml
隐藏属性、函数等
通过使用命令“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
class Dummy1 {
+myMethods()
}
class Dummy2 {
+hiddenMethod()
}
class Dummy3 <<Serializable>> {
String name
}
hide members
hide <<Serializable>> circle
show Dummy1 methods
show <<Serializable>> fields
@enduml
Hide attributes, methods...
You can parameterize the display of classes using the hide/show
command.
The basic command is: hide empty members. This
command will hide attributes or methods if they are empty.
Instead of empty members, you can use:
empty fields or empty attributes for empty fields,
empty methods for empty methods,
fields or attributes which will hide fields, even if they are described,
methods which will hide methods, even if they are described,
members which will hide fields and methods, even if they are described,
circle for the circled character in front of class name,
stereotype for the stereotype.
You can also provide, just after the hide or show
keyword:
class for all classes,
interface for all interfaces,
enum for all enums,
<<foo1>> for classes which are stereotyped with foo1,
an existing class name.
You can use several show/hide commands to define rules and
exceptions.
@startuml
class Dummy1 {
+myMethods()
}
class Dummy2 {
+hiddenMethod()
}
class Dummy3 <<Serializable>> {
String name
}
hide members
hide <<Serializable>> circle
show Dummy1 methods
show <<Serializable>> fields
@enduml
@startuml
class Foo1
class Foo2
Foo2 *-- Foo1
hide Foo2
@enduml
WARNING
This translation need to be updated.
WARNING
Hide classes
You can also use the show/hide commands to hide classes.
This may be useful if you define a large !included file,
and if you want to hide some classes after file inclusion.
@startuml
class Foo1
class Foo2
Foo2 *-- Foo1
hide Foo2
@enduml
Remove classes
You can also use the remove commands to remove classes.
This may be useful if you define a large !included file,
and if you want to remove some classes after file inclusion.
@startuml
class Foo1
class Foo2
Foo2 *-- Foo1
remove Foo2
@enduml
Hide or Remove unlinked class
By default, all classes are displayed:
@startuml
class C1
class C2
class C3
C1 -- C2
@enduml
But you can:
hide @unlinked classes:
@startuml
class C1
class C2
class C3
C1 -- C2
hide @unlinked
@enduml
or remove @unlinked classes:
@startuml
class C1
class C2
class C3
C1 -- C2
remove @unlinked
@enduml
@startuml
class Foo<? extends Element> {
int size()
}
Foo *- Element
@enduml
It is possible to disable this drawing using skinparam genericDisplay old command.
Use generics
You can also use bracket < and > to define generics usage in a class.
@startuml
class Foo<? extends Element> {
int size()
}
Foo *- Element
@enduml
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
class System << (S,#FF7700) Singleton >>
class Date << (D,orchid) >>
@enduml
Specific Spot
Usually, a spotted character (C, I, E or A) is used for classes,
interface, enum and abstract classes.
But you can define your own spot for a class when you define the stereotype,
adding a single character and a color, like in this example:
@startuml
class System << (S,#FF7700) Singleton >>
class Date << (D,orchid) >>
@enduml
You can define a package using the
package keyword, and optionally declare a background color
for your package (Using a html color code or name).
Note that package definitions can be nested.
There are different styles available for packages.
You can specify them either by setting a default style with the command : skinparam packageStyle,
or by using a stereotype on the package:
@startuml
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
You can also define links between packages, like in the following
example:
@startuml
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
Namespaces
In packages, the name of a class is the unique identifier of this class.
It means that you cannot have two classes with the very same name in
different packages.
In that case, you should use namespaces
instead of packages.
You can refer to classes from other namespaces by fully qualify
them. Classes from the default namespace are qualified with a starting
dot.
Note that you don't have to explicitly create namespace : a fully
qualified class is automatically put in the right namespace.
@startuml
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
自动创建命名空间
使用命令 set namespaceSeparator ??? 你可以自定义命名空间分隔符(为 “.” 以外的字符).
@startuml
set namespaceSeparator ::
class X1::X2::foo {
some info
}
@enduml
禁止自动创建包则可以使用
set namespaceSeparator none.
@startuml
set namespaceSeparator none
class X1.X2.foo {
some info
}
@enduml
Automatic namespace creation
You can define another separator (other than the dot) using
the command : set namespaceSeparator ???.
@startuml
set namespaceSeparator ::
class X1::X2::foo {
some info
}
@enduml
You can disable automatic package creation using the command
set namespaceSeparator none.
@startuml
set namespaceSeparator none
class X1.X2.foo {
some info
}
@enduml
棒棒糖 接口
需要定义棒棒糖样式的接口时可以遵循以下语法:
bar ()- foo
bar ()-- foo
foo -() bar
@startuml
class foo
bar ()- foo
@enduml
Lollipop interface
You can also define lollipops interface on classes, using the following
syntax:
You can shorten the arrow by using only the first character of the direction (for example, -d- instead of
-down-) or the two first characters (-do-).
Please note that you should not abuse this functionality : Graphviz gives usually good results without tweaking.
Changing arrows direction
By default, links between classes have two dashes -- and are vertically oriented.
It is possible to use horizontal link by putting a single dash (or dot) like this:
@startuml
Room o- Student
Room *-- Chair
@enduml
You can also change directions by reversing the link:
@startuml
Student -o Room
Chair --* Room
@enduml
It is also possible to change arrow direction by adding left, right, up
or down keywords inside the arrow:
You can shorten the arrow by using only the first character of the direction (for example, -d- instead of
-down-)
or the two first characters (-do-).
Please note that you should not abuse this functionality : Graphviz gives usually good results without tweaking.
And with the left to right direction parameter:
@startuml
left to right direction
foo -left-> dummyLeft
foo -right-> dummyRight
foo -up-> dummyUp
foo -down-> dummyDown
@enduml
“关系”类
你可以在定义了两个类之间的关系后定义一个 关系类 association class 例如:
@startuml
class Student {
Name
}
Student "0..*" - "1..*" Course
(Student, Course) .. Enrollment
class Enrollment {
drop()
cancel()
}
@enduml
也可以用另一种方式:
@startuml
class Student {
Name
}
Student "0..*" -- "1..*" Course
(Student, Course) . Enrollment
class Enrollment {
drop()
cancel()
}
@enduml
Association classes
You can define association class after that a relation has been
defined between two classes, like in this example:
@startuml
class Student {
Name
}
Student "0..*" - "1..*" Course
(Student, Course) .. Enrollment
class Enrollment {
drop()
cancel()
}
@enduml
You can define it in another direction:
@startuml
class Student {
Name
}
Student "0..*" -- "1..*" Course
(Student, Course) . Enrollment
class Enrollment {
drop()
cancel()
}
@enduml
Association on same classe
@startuml
class Station {
+name: string
}
class StationCrossing {
+cost: TimeInterval
}
<> diamond
StationCrossing . diamond
diamond - "from 0..*" Station
diamond - "to 0..* " Station
@enduml
You can declare individual colors for classes, notes etc using the # notation.
You can use standard color names or RGB codes in various notations, see Colors.
You can also use color gradient for background colors, with the following syntax:
two colors names separated either by:
|,
/,
\, or
-
depending on the direction of the gradient.
For example:
@startuml
skinparam backgroundcolor AntiqueWhite/Gold
skinparam classBackgroundColor Wheat|CornflowerBlue
class Foo #red-green
note left of Foo #blue\9932CC
this is my
note on this class
end note
package example #GreenYellow/LightGoldenRodYellow {
class Dummy
}
@enduml
辅助布局
有时候,默认布局并不完美...
你可以使用 together 关键词将某些类进行分组:
布局引擎会尝试将它们捆绑在一起(如同在一个包(package)内)
你也可以使用建立 隐藏 链接的方式来强制布局
@startuml
class Bar1
class Bar2
together {
class Together1
class Together2
class Together3
}
Together1 - Together2
Together2 - Together3
Together2 -[hidden]--> Bar1
Bar1 -[hidden]> Bar2
@enduml
Help on layout
Sometimes, the default layout is not perfect...
You can use together keyword to group some classes together :
the layout engine will try to group them (as if they were in the same package).
You can also use hidden links to force the layout.
@startuml
class Bar1
class Bar2
together {
class Together1
class Together2
class Together3
}
Together1 - Together2
Together2 - Together3
Together2 -[hidden]--> Bar1
Bar1 -[hidden]> Bar2
@enduml
@startuml
' 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
Splitting large files
Sometimes, you will get some very large image files.
You can use the page (hpages)x(vpages) command to split the generated image into several files :
hpages is a number that indicated the number of horizontal pages,
and vpages is a number that indicated the number of vertical pages.
You can also use some specific skinparam settings to put borders on splitted pages (see example).
@startuml
' 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
Extends and implements
It is also possible to use extends and implements keywords.
@startuml
class ArrayList implements List
class ArrayList extends AbstractList
@enduml
Bracketed relations (linking or arrow) style
Line style
It's also possible to have explicitly bold, dashed, dotted, hidden or plain relation, links or arrows:
without label
@startuml
title Bracketed line style without label
class foo
class bar
bar1 : [bold]
bar2 : [dashed]
bar3 : [dotted]
bar4 : [hidden]
bar5 : [plain]
foo --> bar
foo -[bold]-> bar1
foo -[dashed]-> bar2
foo -[dotted]-> bar3
foo -[hidden]-> bar4
foo -[plain]-> bar5
@enduml
with label
@startuml
title Bracketed line style with label
class foo
class bar
bar1 : [bold]
bar2 : [dashed]
bar3 : [dotted]
bar4 : [hidden]
bar5 : [plain]
foo --> bar : ∅
foo -[bold]-> bar1 : [bold]
foo -[dashed]-> bar2 : [dashed]
foo -[dotted]-> bar3 : [dotted]
foo -[hidden]-> bar4 : [hidden]
foo -[plain]-> bar5 : [plain]
@enduml
@startuml
title Bracketed line style mix
class foo
class bar
bar1 : [#red,thickness=1]
bar2 : [#red,dashed,thickness=2]
bar3 : [#green,dashed,thickness=4]
bar4 : [#blue,dotted,thickness=8]
bar5 : [#blue,plain,thickness=16]
foo --> bar : ∅
foo -[#red,thickness=1]-> bar1 : [#red,1]
foo -[#red,dashed,thickness=2]-> bar2 : [#red,dashed,2]
foo -[#green,dashed,thickness=4]-> bar3 : [#green,dashed,4]
foo -[#blue,dotted,thickness=8]-> bar4 : [blue,dotted,8]
foo -[#blue,plain,thickness=16]-> bar5 : [blue,plain,16]
@enduml
Change relation (linking or arrow) color and style (inline style)
You can change the color or style of individual relation or arrows using the inline following notation:
#color;line.[bold|dashed|dotted];text:color
@startuml
class foo
foo --> bar : normal
foo --> bar1 #line:red;line.bold;text:red : red bold
foo --> bar2 #green;line.dashed;text:green : green dashed
foo --> bar3 #blue;line.dotted;text:blue : blue dotted
@enduml
@startuml
abstract abstract
annotation annotation #pink;line:red;line.bold;text:red
class class #palegreen;line:green;line.dashed;text:green
interface interface #aliceblue;line:blue;line.dotted;text:blue
@enduml
First original example:
@startuml
class bar #line:green;back:lightblue
class bar2 #lightblue;line:green
class Foo1 #back:red;line:00FFFF
class FooDashed #line.dashed:blue
class FooDotted #line.dotted:blue
class FooBold #line.bold
class Demo1 #back:lightgreen|yellow;header:blue/red
@enduml
@startuml
left to right direction
class User {
id : INTEGER
..
other_id : INTEGER
}
class Email {
id : INTEGER
..
user_id : INTEGER
address : INTEGER
}
User::id *-- Email::user_id
@enduml