PlantUML類圖
摘要: 類之間的關系 PlantUML用下面的符號來表示類之間的關系: 泛化,Generalization:<|-- 關聯,Association:<-- 組合,Composition:*-- 聚合,Aggregation:o-- 實現,Realization:<|.. 依賴,Dependency:<.. 以上是常見的六種關系,--可以替換成..就可以得到虛線。
類之間的關系
PlantUML用下面的符號來表示類之間的關系:
- 泛化,
Generalization
:<|--
- 關聯,
Association
:<--
- 組合,
Composition
:*--
- 聚合,
Aggregation
:o--
- 實現,
Realization
:<|..
- 依賴,
Dependency
:<..
以上是常見的六種關系,--
可以替換成..
就可以得到虛線。另外,其中的符號是可以改變方向的,例如:<|--
表示右邊的類泛化左邊的類;--|>
表示左邊的類泛化右邊的類。
例如,下面的是--
:
@startuml
Class01 <|-- Class02:泛化
Class03 <-- Class04:關聯
Class05 *-- Class06:組合
Class07 o-- Class08:聚合
Class09 -- Class10
@enduml
生成的類圖如下:
--
可以替換成..
,對應的虛線:
@startuml
Class11 <|.. Class12:實現
Class13 <.. Class14:依賴
Class15 *.. Class16
Class17 o.. Class18
Class19 .. Class20
@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
生成的類圖如下:
上面的類圖意思是:
- Driver 駕駛 Car
- Car 有4個 Wheel
- Person 擁有 Car
添加方法
在類名后面添加冒號可以添加方法和方法的參數,例如:
@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
生成的類圖如下:
定義可見性
以下符號定義字段或者方法的可見性:
-
:private
#
:protected
~
:package private
+
:public
例如:
@startuml class Dummy { -field1 #field2 ~method1() +method2() } @enduml
你可以使用skinparam classAttributeIconSize 0關掉icon的顯示:
@startuml skinparam classAttributeIconSize 0 class Dummy { -field1 #field2 ~method1() +method2() } @enduml
抽象和靜態
你可以使用{static}
或者{abstract}
來修飾字段或者方法,修飾符需要在行的開頭或者末尾使用。你也可以使用{classifier}
代替{static}
。
@startuml class Dummy { {static} String id {classifier} String name {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 classes } class User { .. Simple Getter .. + getName() + getAddress() .. Some setter .. + setName() __ private data __ int age -- encrypted -- String password } @enduml
注釋和原型
原型使用class
、<<
和>>
進行定義。
注釋使用note left of
、note right of
、note top of
、note bottom of
關鍵字進行定義。
你也可以在最后一個定義的類上使用note left
、note right
、note top
、note bottom
關鍵字。
注釋可以使用..
與其他對象進行連接。
@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">
或者<font color="colorName">
<color:#AAAAAA>
或者<color:colorName>
<size:nn>
該表font大小<img src="file">
或者<img:file>
,文件必須是可以訪問的。
@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 top on link
、note bottom on link
來改變注釋的位置。
@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
或者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
使用非字母
類名可以使用非字母的方式顯示:
@startuml
class "This is my class" as class1
class class2 as "It works this way too"
class2 *-- "foo/dummy" : use
@enduml
隱藏字段和方法
@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
使用泛型
@startuml class Foo<? extends Element> { int size() } Foo *- Element @enduml
包
@startuml package "Classic Collections" #yellow{ Object <|-- ArrayList } package net.sourceforge.plantuml { Object <|-- Demo1 Demo1 *- Demo2 } @enduml
包可以設置樣式,也可以使用skinparam packageStyle
設置為默認樣式。
@startuml scale 750 width package foo1 <<Node>> { class Class1 } package foo2 <<Rect>> { 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 rect package foo1.foo2 { } package foo1.foo2.foo3 { class Object } foo1.foo2 +-- foo1.foo2.foo3 @enduml
命名空間
命名空間內使用默認的類,需要在類名前加上點
。
@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 ???
設置命名空間分隔符,使用set namespaceSeparator none
可以關閉自動設置命名空間。
@startuml set namespaceSeparator :: class X1::X2::foo { some info } @enduml
改變箭頭方向
@startuml Room o- Student Room *-- Chair @enduml
換個方向:
@startuml Student -o Room Chair --* Room @enduml
也可以在箭頭上使用left, right, up or down關鍵字:
@startuml foo -left-> dummyLeft foo -right-> dummyRight foo -up-> dummyUp foo -down-> dummyDown @enduml
這些關鍵字可以使用開頭的幾個字符簡寫,例如,使用-d-
代替-down-
。
標題
使用title
或者title end title
。
@startuml title Simple <b>example</b>\nof title Object <|-- ArrayList @enduml
設置Legend
@startuml Object <|- ArrayList legend right <b>Object</b> and <b>ArrayList</b> are simple class endlegend @enduml
關聯類
一個類和兩個類有關聯時設置關系:
@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
其他
還有一些特性,如設置皮膚參數、顏色、拆分大文件等等,請參考官方文檔。
例子
一個完整的例子:
@startuml
title class-diagram.png
scale 1.5
/'組合關系(composition)'/
class Human {
- Head mHead;
- Heart mHeart;
..
- CreditCard mCard;
--
+ void travel(Vehicle vehicle);
}
Human *-up- Head : contains >
Human *-up- Heart : contains >
/'聚合關系(aggregation)'/
Human o-left- CreditCard : owns >
/'依賴關系(dependency)'/
Human .down.> Vehicle : dependent
/'關聯關系(association'/
Human -down-> Company : associate
/'繼承關系(extention)'/
interface IProgram {
+ void program();
}
class Programmer {
+ void program();
}
Programmer -left-|> Human : extend
Programmer .up.|> IProgram : implement
@enduml
下面例子,參考自Class Diagram。
Java集合類圖
@startuml abstract class AbstractList abstract AbstractCollection interface List interface Collection List <|-- AbstractList Collection <|-- AbstractCollection Collection <|- List AbstractCollection <|- AbstractList AbstractList <|-- ArrayList ArrayList : Object[] elementData ArrayList : size() enum TimeUnit TimeUnit : DAYS TimeUnit : HOURS TimeUnit : MINUTES @enduml
類和接口
@startuml Object -- AbstractList class ArrayList extends Object { int size } interface List extends Collection { add() } interface Set extends Collection class TreeSet implements SortedSet @enduml
Repository接口
@startuml package framework <<Folder>>{ interface BasicRepository { E find(Object pk); List<E> findAll(); void save(E entity); void update(E entity); void remove(E entity); } class AbstractHibernateRepository << @Repository >> { -EntityManager entityManager; } } interface PartnerRepository { List<PartnerEntity> findByFoo(...); List<PartnerEntity> search(String pattern, int maxResult); } class HibernatePartnerRepository << @Repository >> { } class InMemoryPartnerRepository { } BasicRepository <|.. PartnerRepository BasicRepository <|.. AbstractHibernateRepository AbstractHibernateRepository <|-- HibernatePartnerRepository PartnerRepository <|.. HibernatePartnerRepository PartnerRepository <|.. InMemoryPartnerRepository @enduml
Java異常層次
@startuml namespace java.lang #DDDDDD { class Error << unchecked >> Throwable <|-- Error Throwable <|-- Exception Exception <|-- CloneNotSupportedException Exception <|-- RuntimeException RuntimeException <|-- ArithmeticException RuntimeException <|-- ClassCastException RuntimeException <|-- IllegalArgumentException RuntimeException <|-- IllegalStateException Exception <|-- ReflectiveOperationException ReflectiveOperationException <|-- ClassNotFoundException } namespace java.io #DDDDDD { java.lang.Exception <|-- IOException IOException <|-- EOFException IOException <|-- FileNotFoundException } namespace java.net #DDDDDD { java.io.IOException <|-- MalformedURLException java.io.IOException <|-- UnknownHostException } @enduml