前言最近通过代码来画时序图,UML用例图,感觉很不错,所以给大家分享一下 。
日常开发,一般在设计阶段,我们都需要画时序图、用例图等等 。大家平时画图的时候,是用draw.io还是processOn呢?用它们画出的图,其实都挺好看的 。但是呢,今天田螺哥介绍一个款开源的,画图神器!用代码就能画图,配合IDE使用,画图高效简单,信手拈来,还挺美观的 。这个神奇就是PlantUML 。

文章插图
- github地址,给个star,感谢感谢
- 公众号:捡田螺的小男孩
PlantUML 画出来的图,简洁美观,先给大家看看,一个用PlantUML画出来的登录时序图,以及对应画图的代码,如下:
/** * 关注公众号:键捡田螺的小男孩 */@startumltitle Sequence Diagram of User loginactor User as userparticipant "gateway" as gatewayparticipant "user-core" as userCoredatabase "MySQL" as mysqldatabase "redis" as redisautonumberuser-> gateway:login request,param:username,passwordactivate gatewaygateway-> userCore:forward the login requestactivate userCoreuserCore-> userCore :check the login paramuserCore-> mysql:query user info from mysql by usernameactivate mysqlmysql-> userCore:response with username and passworddeactivate mysqluserCore->userCore:compare the requested password with the DB's passworduserCore-> userCore: generate an unique tokenuserCore--> redis: save the token to redisuserCore-> gateway: response with the tokendeactivate userCoregateway-> user: login success with the tokendeactivate gateway@enduml复制代码登录用例时序图如下:
文章插图
2. PlantUML的安装使用PlantUML的安装很方便的.有个插件,名字是:PlantUML Integration,大家可以去IDE的插件市场,搜索安装即可,如下:

文章插图
安装成功后,想快速体验一般的话.可以新建一个项目,然后新建一个plantUML File文件,然后把我上个小节,登录时序图那个代码复制进去,就可以看到登录时序图啦.

文章插图
3.如何用PlantUML 画时序图什么是时序图?
时序图(Sequence Diagram),又名序列图、循序图,是一种UML交互图 。它通过描述对象之间发送消息的时间顺序显示多个对象之间的动态协作 。它可以表示用例的行为顺序,当执行一个用例行为时,其中的每条消息对应一个类操作或状态机中引起转换的触发事件 。如何用PlantUML画时序图呢?
你可以先新建一个PlantUML文件

文章插图
然后选择Sequence,并定义一个文件名称

文章插图
就会有默认的时序图生成啦.

文章插图
我们照着登录时序图的代码,来大概说下每个关键词的意思吧.
/** * 关注公众号:键捡田螺的小男孩 */@startumltitle Sequence Diagram of User loginactor User as userparticipant "gateway" as gatewayparticipant "user-core" as userCoredatabase "MySQL" as mysqldatabase "Redis" as redisautonumberuser-> gateway:login request,param:username,passwordactivate gatewaygateway-> userCore:forward the login requestactivate userCoreuserCore-> userCore :check the login paramuserCore-> mysql:query user info from mysql by usernameactivate mysqlmysql-> userCore:response with username and passworddeactivate mysqluserCore->userCore:compare the requested password with the DB's passworduserCore-> userCore: generate an unique tokenuserCore--> redis: save the token to redisuserCore-> gateway: response with the tokendeactivate userCoregateway-> user: login success with the tokendeactivate gateway@enduml复制代码关键词解释如下:- title:表示该UML用例图的标题
- actor:表示人形的参与者
- as: 使用as 关键字命名参与者 。你可以把它理解成定义变量一样,as后面跟着的就是变量,声明后,我们后面就可以使用这个变量啦
推荐阅读
- 山水画|办公室挂什么画好?想要好寓意,一幅山水画必不可少
- 动画|京都动画纵火案三周年 30余人丧生:嫌疑人初审仍未开始
- 全自动洗衣机哪个牌子好用质量好?
- 长三角|范冰冰向BABY学画微笑唇妆,三美红唇同台李小璐输在人工笑
- 安卓手机如何打开.hwt文件?
- 五味子的主要功效与作用是什么?
- 动画|京都动画纵火案三周年 官方举行追悼式:继续打造新作品
- 文玩|文玩盘玩到底用不用带手套?各种品类其实都离不开手套!
- 藕饼是什么意思?
- 最小公倍数怎么求?
