組件圖
= 組件 =
組件必須用中括號括起來。
還可以使用關鍵字component
定義一個組件。 並且可以用關鍵字as
給組件定義一個別名。 這個別名可以在稍後定義關係的時候使用。
@startuml
[First component]
[Another component] as Comp2
component Comp3
component [Last\ncomponent] as Comp4
@enduml
= 接口 =
接口可以使用()
來定義(因爲這個看起來像個圓)。
還可以使用關鍵字interface
關鍵字來定義接口。 並且還可以使用關鍵字as
定義一個別名。 這個別名可以在稍後定義關係的時候使用。
我們稍後可以看到,接口的定義是可選的。
@startuml
() "First Interface"
() "Another interface" as Interf2
interface Interf3
interface "Last\ninterface" as Interf4
@enduml
基礎的示例
元素之間可以使用虛線(..
)、直線(--
)、箭頭(-->
)進行連接。
@startuml
DataAccess - [First Component]
[First Component] ..> HTTP : use
@enduml
使用註釋
你可以使用 note left of
, note right of
, note top of
, note bottom of
等關鍵字定義相對於對象位置的註釋。
也可以使用關鍵字note
單獨定義註釋,然後使用虛線(..
)將其連接到其他對象。
@startuml
interface "Data Access" as DA
DA - [First Component]
[First Component] ..> HTTP : use
note left of HTTP : Web Service only
note right of [First Component]
A note can also
be on several lines
end note
@enduml
組合組件
你可以使用多個關鍵字將組件和接口組合在一起。
package
node
folder
frame
cloud
database
@startuml
package "Some Group" {
HTTP - [First Component]
[Another Component]
}
node "Other Groups" {
FTP - [Second Component]
[First Component] --> FTP
}
cloud {
[Example 1]
}
database "MySql" {
folder "This is my folder" {
[Folder 3]
}
frame "Foo" {
[Frame 4]
}
}
[Another Component] --> [Example 1]
[Example 1] --> [Folder 3]
[Folder 3] --> [Frame 4]
@enduml
改變箭頭方向
默認情況下,對象之間用--
連接,並且連接是豎直的。不過可以使用一個橫線或者點設置水平方向的連接,就行這樣:
@startuml
[Component] --> Interface1
[Component] -> Interface2
@enduml
也可以使用反向連接:
@startuml
Interface1 <-- [Component]
Interface2 <- [Component]
@enduml
還可以使用關鍵字left
, right
, up
or down
改變箭頭方向。
@startuml
[Component] -left-> left
[Component] -right-> right
[Component] -up-> up
[Component] -down-> down
@enduml
允許使用方向單詞的首字母或者前兩個字母表示方向(例如-d-
, -do-
, -down-
都是等價的)。
請不要亂用這些功能:Graphviz(PlantUML的後端引擎)不喜歡這個樣子。
使用UML2標記符
命令skinparam componentStyle uml2
可以切換到UML2標記符。
@startuml
skinparam componentStyle uml2
interface "Data Access" as DA
DA - [First Component]
[First Component] ..> HTTP : use
@enduml
長描述
可以用方括號"[ ]"在連線上添加描述。
@startuml
component comp1 [
This component
has a long comment
on several lines
]
@enduml
不同的顏色表示
你可以在聲明一個組件時加上顏色的聲明。
@startuml
component [Web Server] #Yellow
@enduml
在定型組件中使用精靈圖
你可以在定型組件中使用精靈圖(sprite)。
@startuml
sprite $businessProcess [16x16/16] {
FFFFFFFFFFFFFFFF
FFFFFFFFFFFFFFFF
FFFFFFFFFFFFFFFF
FFFFFFFFFFFFFFFF
FFFFFFFFFF0FFFFF
FFFFFFFFFF00FFFF
FF00000000000FFF
FF000000000000FF
FF00000000000FFF
FFFFFFFFFF00FFFF
FFFFFFFFFF0FFFFF
FFFFFFFFFFFFFFFF
FFFFFFFFFFFFFFFF
FFFFFFFFFFFFFFFF
FFFFFFFFFFFFFFFF
FFFFFFFFFFFFFFFF
}
rectangle " End to End\nbusiness process" <<$businessProcess>> {
rectangle "inner process 1" <<$businessProcess>> as src
rectangle "inner process 2" <<$businessProcess>> as tgt
src -> tgt
}
@enduml
顯示參數
用skinparam
改變字體和顏色。
可以在如下場景中使用:
可以爲構造類型和接口定義特殊的顏色和字體。
@startuml
skinparam interface {
backgroundColor RosyBrown
borderColor orange
}
skinparam component {
FontSize 13
BackgroundColor<<Apache>> Red
BorderColor<<Apache>> #FF6655
FontName Courier
BorderColor black
BackgroundColor gold
ArrowFontName Impact
ArrowColor #FF6655
ArrowFontColor #777777
}
() "Data Access" as DA
DA - [First Component]
[First Component] ..> () HTTP : use
HTTP - [Web Server] << Apache >>
@enduml
@startuml
[AA] <<static lib>>
[BB] <<shared lib>>
[CC] <<static lib>>
node node1
node node2 <<shared node>>
database Production
skinparam component {
backgroundColor<<static lib>> DarkKhaki
backgroundColor<<shared lib>> Green
}
skinparam node {
borderColor Green
backgroundColor Yellow
backgroundColor<<shared node>> Magenta
}
skinparam databaseBackgroundColor Aqua
@enduml