= 類圖 =
類之間的關係
類之間的關係通過下面的符號定義 :
Type | Symbol | Drawing |
Extension(擴展) | <|-- |
|
Composition(組合) | *-- |
|
Aggregation(聚合) | o-- |
使用..
來代替 --
可以得到點 線.
在這些規則下,也可以繪製下列圖形
@startuml
Class01 <|-- Class02
Class03 *-- Class04
Class05 o-- Class06
Class07 .. Class08
Class09 -- Class10
@enduml
@startuml
Class11 <|.. Class12
Class13 --> Class14
Class15 ..> Class16
Class17 ..|> Class18
Class19 <--* Class20
@enduml
@startuml
Class21 #-- Class22
Class23 x-- Class24
Class25 }-- Class26
Class27 +-- Class28
Class29 ^-- Class30
@enduml
關係上的標識
在關係之間使用標籤來說明時, 使用 :
後接 標籤文字。
對元素的說明,你可以在每一邊使用 ""
來說明.
@startuml
Class01 "1" *-- "many" Class02 : contains
Class03 o-- Class04 : aggregation
Class05 --> "1" Class06
@enduml
在標籤的開始或結束位置添加<
或 >
以表明是哪個對象作用到哪個對象上。
@startuml
class Car
Driver - Car : drives >
Car *- Wheel : have 4 >
Car -- Person : < owns
@enduml
添加方法
爲了聲明字段(對象屬性)或者方法,你可以使用 後接字段名或方法名。
系統檢查是否有括號來判斷是方法還是字段。
@startuml
Object <|-- ArrayList
Object : equals()
ArrayList : Object[] elementData
ArrayList : size()
@enduml
也可以使用{}
把字段或者方法括起來
注意,這種語法對於類型/名字的順序是非常靈活的。
@startuml
class Dummy {
String data
void methods()
}
class Flight {
flightNumber : Integer
departureTime : Date
}
@enduml
你可以(顯式地)使用 {field}
和 {method}
修飾符來覆蓋解析器的對於字段和方法的默認行爲
@startuml
class Dummy {
{field} A field (despite parentheses)
{method} Some method
}
@enduml
定義可訪問性
一旦你定義了域或者方法,你可以定義 相應條目的可訪問性質。
Character | Icon for field | Icon for method | Visibility |
- |
private | ||
# |
protected | ||
~ |
package private | ||
+ |
public |
@startuml
class Dummy {
-field1
#field2
~method1()
+method2()
}
@enduml
你可以採用以下命令停用這些特性 skinparam classAttributeIconSize 0
:
@startuml
skinparam classAttributeIconSize 0
class Dummy {
-field1
#field2
~method1()
+method2()
}
@enduml
抽象與靜態
通過修飾符{static}
或者{abstract}
,可以定義靜態或者抽象的方法或者屬性。
這些修飾符可以寫在行的開始或者結束。也可以使用{classifier}
這個修飾符來代替{static}
.
@startuml
class Dummy {
{static} String id
{abstract} void methods()
}
@enduml
高級類體
PlantUML默認自動將方法和屬性重新分組,你可以自己定義分隔符來重排方法和屬性,下面的分隔符都是可用的:--
..
==
__
.
還可以在分隔符中添加標題:
@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
更多註釋
可以在註釋中使用部分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
你也可以在註釋中展示多行。
你也可以在定義的class之後直接使用 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 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
抽象類和接口
用關鍵字abstract
或abstract class
來定義抽象類。抽象類用斜體顯示。 也可以使用interface
, annotation
和 enum
關鍵字。
@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
使用非字母字符
如果你想在類(或者枚舉)的顯示中使用非字母符號,你可以:
- 在類的定義中使用
as
關鍵字 - 在類名旁邊加上
""
@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
隱藏類
你也可以使用 show/hide
命令來隱藏類
如果你定義了一個大的!included 文件,且想在文件包含之後隱藏部分類,該功能會很有幫助。
@startuml
class Foo1
class Foo2
Foo2 *-- Foo1
hide Foo2
@enduml
泛型(generics)
你可以用 <
和 >
來定義類的泛型。
@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
= 包 =
你可以通過關鍵詞 package
聲明包,同時可選的來聲明對應的背景色(通過使用html色彩代碼或名稱)。
注意:包可以被定義爲嵌套。
@startuml
package "Classic Collections" #DDDDDD {
Object <|-- ArrayList
}
package net.sourceforge.plantuml {
Object <|-- Demo1
Demo1 *- Demo2
}
@enduml
包樣式
包可以定義不同的樣式。
你可以通過以下的命令來設置默認樣式 : skinparam packageStyle
,或者對包使用對應的模板:
@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
你也可以參考下面的示例來定義包之間的連線:
@startuml
skinparam packageStyle rectangle
package foo1.foo2 {
}
package foo1.foo2.foo3 {
class Object
}
foo1.foo2 +-- foo1.foo2.foo3
@enduml
命名空間(Namespaces)
在使用包(package)時(區別於命名空間),類名是類的唯一標識。 也就意味着,在不同的包(package)中的類,不能使用相同的類名。
在那種情況下(譯註:同名、不同全限定名類),你應該使用命名空間來取而代之。
你可以從其他命名空間,使用全限定名來引用類, 默認命名空間(譯註:無名的命名空間)下的類,以一個“."開頭(的類名)來引用(譯註:示例中的BaseClass
).
注意:你不用顯示地創建命名空間:一個使用全限定名的類會自動被放置到對應的命名空間。
@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
棒棒糖 接口
需要定義棒棒糖樣式的接口時可以遵循以下語法:
bar ()- foo
bar ()-- foo
foo -() bar
@startuml
class foo
bar ()- foo
@enduml
改變箭頭方向
類之間默認採用兩個破折號 --
顯示出垂直 方向的線. 要得到水平方向的可以像這樣使用單破折號 (或者點):
@startuml
Room o- Student
Room *-- Chair
@enduml
你也可以通過改變倒置鏈接來改變方向
@startuml
Student -o Room
Chair --* Room
@enduml
也可通過在箭頭內部使用關鍵字, 例如left
, right
, up
或者 down
,來改變方向
@startuml
foo -left-> dummyLeft
foo -right-> dummyRight
foo -up-> dummyUp
foo -down-> dummyDown
@enduml
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.
“關係”類
你可以在定義了兩個類之間的關係後定義一個 關係類 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
皮膚參數
用skinparam
改變字體和顏色。
可以在如下場景中使用:
@startuml
skinparam class {
BackgroundColor PaleGreen
ArrowColor SeaGreen
BorderColor SpringGreen
}
skinparam stereotypeCBackgroundColor YellowGreen
Class01 "1" *-- "many" Class02 : contains
Class03 o-- Class04 : aggregation
@enduml
Skinned Stereotypes
You can define specific color and fonts for stereotyped classes.
@startuml
skinparam class {
BackgroundColor PaleGreen
ArrowColor SeaGreen
BorderColor SpringGreen
BackgroundColor<<Foo>> Wheat
BorderColor<<Foo>> Tomato
}
skinparam stereotypeCBackgroundColor YellowGreen
skinparam stereotypeCBackgroundColor<< Foo >> DimGray
Class01 <<Foo>>
Class03 <<Foo>>
Class01 "1" *-- "many" Class02 : contains
Class03 o-- Class04 : aggregation
@enduml
Color gradient
It's possible to declare individual color for classes or note using the # notation.
You can use either standard color name or RGB code.
You can also use color gradient in background, with the following syntax: two colors names separated either by:
|
,/
,\
,- or
-
depending the direction of the gradient.
For example, you could have:
@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
拆分大文件
有些情況下,會有一些很大的圖片文件。
可以用 page (hpages)x(vpages)
這個命令把生成的圖片文件拆分成若干個文件。
hpages
用來表示水平方向頁面數, and vpages
用來表示垂直方面頁面數。
你也可以使用特定的皮膚設定來給分頁添加邊框(見例子)
@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