程序员难免要经常画流程图,状态图,时序图等。以前经常用 visio画,经常为矩形画多大,摆放在哪等问题费脑筋。有时候修改文字后,为了较好的显示效果不得不再去修改图形。今天介绍的工具是如何使用PlantUML 的插件画流程图,状态图,时序图等。这是一种程序员看了就会爱上的画图方式:自然,高效。

       什么是 PlantUML,PlantUML 是一个画图脚本语言,用它可以快速地画出:

0、前导:vscode 环境搭建 Plantuml和Markdown环境 

(1)需要
①Java环境(PlantUML需要java环境)
②VSCode 
③VSCode插件
   ※PlantUML (支持在VSCode中导出PlantUML)
   ※Markdown All in One(支持Markdown在VSCode中的使用,包括即时预览,快捷键,导出等命令)
④PlantUML Server(提供PlantUML 服务支持)
   下载地址:https://github.com/plantuml/plantuml-server
   可用Idea或者Eclipse导入项目,运行。支持maven,docker。

(2)使用
①在VSCode中新建Markdown文件,Markdown中写PlantUML语法为:

```plantuml
这里写plantuml
```

②快捷键Shift+Ctrl+P,打开VSCode命令栏,输入plantuml,可以看到导出的选项。
③完成

(3)无需启动server
经测试,VSCode中只需要安装PlantUML相关插件,就能渲染plantuml图形。
相关插件:
PlantUML
Markdown All In One
Markdown Preview Enhanced

(4)注意:

        由于plantuml使用Graphviz来生成相关图形(只有序列图可以不依赖它),其它图形都需要,因此得安装它,否则生成图形失败,且报错。

        PlantUML 是一个通过简单的脚本来绘制UML图表的一个工具。Graphviz 是一个更为通用的图表绘制语言,PlantUML中的部分图表需要调用Graphviz来绘制。

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>
@startuml
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
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

 

参考链接:

https://blog.csdn.net/Zhangxichao100/article/details/77774752     Markdown 绘制 UML 图 -- PlantUML + Gravizo

https://blog.csdn.net/geduo_83/article/details/86422485                PlantUML画类图、流程图、时序图使用详解 ☆

https://blog.csdn.net/qq_36320180/article/details/105372049      【VSCode插件】Plantuml和Markdown搭配使用

https://blog.csdn.net/universsky2015/article/details/102805398     PlantUML 简明教程  ☆

https://www.jianshu.com/p/e972b9d790ea                                      plantUML语法(-)时序图

https://www.freesion.com/article/3938240762/                                PLANTUML 之序列图(一)       

https://www.cnblogs.com/young-youth/p/11665573.html                (一)PlantUML 语法之时序图  ☆
 

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐