PlantUML畫類圖、流程圖、時序圖使用詳解
阿新 • • 發佈:2019-01-14
程式設計師難免要經常畫流程圖,狀態圖,時序圖等。以前經常用 visio
畫,經常為矩形畫多大,擺放在哪等問題費腦筋。有時候修改文字後,為了較好的顯示效果不得不再去修改圖形。今天介紹的工具是如何使用
PlantUML 的外掛畫流程圖,狀態圖,時序圖等。這是一種程式設計師看了就會愛上的畫圖方式:自然,高效。
什麼是 PlantUML
PlantUML 是一個畫圖指令碼語言,用它可以快速地畫出:
- 類圖
- 流程圖
- 時序圖
- 用例圖
- 狀態圖
- 元件圖
1. 類圖
案例1:
@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 } @enduml
案例2:
@startuml class Track class Media class Trip{ String tripID; String tracks; String medias; } Trip --> Track Trip --> Media interface ITripTrackCollection{ void start(); void stop(); void pause(); void destory(); } class TripTrackCollection implements ITripTrackCollection{ Vector<LocationInfo> mLocations; ExtcutorService mVecoterThread; ScheduledExecutorService mDatabaseThread; } class TrackCollectService extends Service implements ITripTrackCollection{ TripTrackCollection TripTrackCollection; } TrackCollectService -->TripTrackCollection @enduml
2. 流程圖
案例1:
@startuml
(*) --> "check input"
If "input is verbose" then
--> [Yes] "turn on verbosity"
--> "run command"
else
--> "run command"
Endif
-->(*)
@enduml
案例2:
start :"步驟1處理"; :"步驟2處理"; if ("條件1判斷") then (true) :條件1成立時執行的動作; if ("分支條件2判斷") then (no) :"條件2不成立時執行的動作"; else if ("條件3判斷") then (yes) :"條件3成立時的動作"; else (no) :"條件3不成立時的動作"; endif endif :"順序步驟3處理"; endif if ("條件4判斷") then (yes) :"條件4成立的動作"; else if ("條件5判斷") then (yes) :"條件5成立時的動作"; else (no) :"條件5不成立時的動作"; endif endif stop @enduml
3.時序圖
案例1:
@startuml
Alice -> Bob: Authentication Request
Bob --> Alice: Authentication Response
Alice -> Bob: Another authentication Request
Alice <-- Bob: another authentication Response
@enduml
案例2:
@startuml
title Android Broadcast procedure
participant Activity #Lime
participant ContextWrapper #Cyan
participant ContextImpl #Cyan
participant ActivityManagerService #Cyan
participant ActivityStackSupervisor #Cyan
participant ActivityStack #Cyan
participant ApplicationThreadProxy #Silver
participant InnerReceiver #Magenta
participant ReceiverDispatcher #Magenta
participant BroadcastReceiver #Magenta
autonumber
Activity -> ContextWrapper : registerReceiver()
ContextWrapper -> ContextImpl : registerReceiver()
ContextImpl -> LoadedApk : getReceiverDispatcher()
LoadedApk -> ActivityManagerProxy : registerReceiver()
ActivityManagerProxy -> ActivityManagerService : registerReceiver()
Activity -> ContextWrapper : sendBroadcast()
ContextWrapper -> ContextImpl : sendBroadcast()
ContextImpl -> ActivityManagerService: broadcastIntent()
ActivityManagerService -> ActivityManagerService : broadcastIntentLocked()
ActivityManagerService -> ActivityManagerService : collectReceiverComponents()
ActivityManagerService -> ActivityManagerService : scheduleBroadcastsLocked()
ActivityManagerService -> ActivityManagerService : processNextBroadcast()
ActivityManagerService -> ActivityManagerService : deliverToRegisteredReceiverLocked()
ActivityManagerService -> ActivityManagerService : performReceiveLocked()
ActivityManagerService -> ApplicationThreadProxy : scheduleRegisteredReceiver()
ApplicationThreadProxy -> InnerReceiver : performReceive()
InnerReceiver -> ReceiverDispatcher : performReceive()
ReceiverDispatcher -> BroadcastReceiver : onReceive()
Activity -> ContextWrapper : sendOrderedBroadcast()
ContextWrapper -> ContextImpl : sendOrderedBroadcast()
ContextImpl -> ActivityManagerService: broadcastIntent()
@enduml
4. 用例圖:
@startuml
:Main Admin: as Admin
(Use the application) as (Use)
User -> (Start)
User --> (Use)
Admin ---> (Use)
note right of Admin : This is an example.
note right of (Use)
A note can also
be on several lines
end note
note "This note is connected\nto several objects." as N2
(Start) .. N2
N2 .. (Use)
@enduml
5. 狀態圖
@startuml
scale 350 width
[*] --> NotShooting
state NotShooting {
[*] --> Idle
Idle --> Configuring : EvConfig
Configuring --> Idle : EvConfig
}
state Configuring {
[*] --> NewValueSelection
NewValueSelection --> NewValuePreview : EvNewValue
NewValuePreview --> NewValueSelection : EvNewValueRejected
NewValuePreview --> NewValueSelection : EvNewValueSaved
state NewValuePreview {
State1 -> State2
}
}
@enduml
6. 元件圖
案例1:
@startuml
package "Some Group" {
HTTP - [First Component]
[Another Component]
}
package "Other Groups" {
FTP - [Second Component]
[First Component] --> FTP
}
@enduml
案例2:
@startuml
package "元件1" {
["元件1.1"] - ["元件1.2"]
["元件1.2"] -> ["元件2.1"]
}
node "元件2" {
["元件2.1"] - ["元件2.2"]
["元件2.2"] --> [負載均衡伺服器]
}
cloud {
[負載均衡伺服器] -> [邏輯伺服器1]
[負載均衡伺服器] -> [邏輯伺服器2]
[負載均衡伺服器] -> [邏輯伺服器3]
}
database "MySql" {
folder "This is my folder" {
[Folder 3]
}
frame "Foo" {
[Frame 4]
}
}
[邏輯伺服器1] --> [Folder 3]
[邏輯伺服器2] --> [Frame 4]
[邏輯伺服器3] --> [Frame 4]
@enduml