为了账号安全,请及时绑定邮箱和手机立即绑定

UML时序图详解及Markdown语法

标签:
资讯

序列图根据时间序列展示对象如何进行协作。它展示了在用例的特定场景中,对象如何与其他对象交互。

元素

UML时序图在Markdown语法中固定以sequenceDiagram开头。

https://img1.sycdn.imooc.com//61128ad70001f2c302290199.jpg

参与者/角色

  • 参与者/角色(Participants/Actors)可以隐式声明,也可以显式声明,它们的顺序与在代码中声明的顺序一致。

  • 隐式声明

老板->>小明: 你好
小明-->>老板: 你好 
老板-)小明: 请你出去

https://img1.sycdn.imooc.com//61128afd0001a80a05790370.jpg

  • 显式声明

participant 老板

participant 小明

老板->>小明: 你好

小明-->>老板: 你好

老板-)小明: 请你出去

https://img1.sycdn.imooc.com//61128b2300018d0605170398.jpg

别名

  • 我们可以给参与者起一个方便的别名as,在之后只需要使用别名即可。

participant B as 老板

participant M as 小明

B->>M: 你好

M-->>B: 你好

B-)M: 请你出去

https://img1.sycdn.imooc.com//61128b430001e33a05880412.jpg

消息

  • 消息线可以由实线和虚线来表示。

https://img1.sycdn.imooc.com//61128b5b0001b9b203540556.jpg

participant B as 老板

participant M as 小明

B -> M: 你好

B --> M: 你好

B ->> M: 你好

B -->> M: 你好

B ->> M: 你好

B -x M:你好

B --x M:你好

B -) M:你好

M --) B:gun

https://img1.sycdn.imooc.com//61128b760001e55c05250751.jpg

激活

  • 激活角色或者使角色失活。

  • 使用activate/deactivate实现

participant B as 老板

participant M as 小明

B -> M: 你好

B --> M: 你好

B -) M:你好

activate M

M --) B:gun

deactivate M

https://img1.sycdn.imooc.com//61128ba500014e6605120445.jpg

  • 在消息箭头后使用+/-来实现

participant B as 老板

participant M as 小明

B -> M: 你好

B --> M: 你好

B -) M:你好

activate M

M --) B:gun

deactivate M

https://img1.sycdn.imooc.com//61128bc10001958705250459.jpg

  • 同一个角色重复激活

participant B as 老板

participant M as 小明

B ->+ M: 你好

B -)+ M:你好

M -->- B: 你好

M --)- B:gun

https://img1.sycdn.imooc.com//61128be100010df905000440.jpg

说明

  • 为时序图添加一个说明Note,可以通过指定方向来定制说明的位置

  • 左(left of)、右(right of)、跨越(over)

participant B as 老板

Note left of B: 暴发户

participant M as 小明

Note right of M:董事长的亲孙子

Note over B,M : 相亲相爱一家人

B ->+ M: 你好

B -)+ M:你好

M -->- B: 你好

M --)- B:gun

https://img1.sycdn.imooc.com//61128c020001392b07580622.jpg

循环

  • 表示一个循环loop...end

participant B as 老板

Note left of B: 暴发户

participant M as 小明

Note right of M:董事长的亲孙子

B ->> M: 你好

M -) B:我爷爷是董事长

loop 每小时一次

B -) M : baba

end

M --) B:gun

B -) M : 好勒

https://img1.sycdn.imooc.com//61128c23000164e107410711.jpg

选择

  • 在时序图中表示选择,使用alt...else..end来表示。

  • 若是没有else的选择,可以使用opt来表示。

participant B as 老板

participant M as 小明

B ->> M: 你爷爷是董事长吗?

alt 是的

M -) B:我爷爷是董事长

else 不是

M -) B:我爷爷不是董事长

end

opt 额外回复

M --) B:想干嘛?

end

B -) M : 没事,就问问

https://img1.sycdn.imooc.com//61128c420001645405290716.jpg

并行

  • 表示并行发生,使用par...and...end

  • 单层并行

par A to B

A->>B: hello,B

and A to C

A->>C: hello,C

end

C-->>A: hello A

B-->>A: hello A

https://img1.sycdn.imooc.com//61128c6700019b3f07730614.jpg

  • 嵌套并行

par A to B

A->>B: hello,B

and A to C

A->>C: hello,C


par C to D

C-)D:hello D

and C to E

C-)E:hello E

end


end

C-->>A: hello A

B-->>A: hello A

D--)C: hello C

E--)C: hello C

https://img1.sycdn.imooc.com//61128c870001d23408180634.jpg

背景高亮

  • 使用rect...end实现,颜色可以使用CSS的颜色名称,RGB,HEX,HSL,RGBA,HSLA值来指定。

participant B as 老板

participant M as 小明

rect yellow

B ->> M: 你好

rect rgb(255,0,0)

M -) B:我爷爷是董事长

M --) B:gun

end

B -) M : 好勒

end

https://img1.sycdn.imooc.com//61128ca40001aebb05420538.jpg

序列号

  • 使用autonumber来自动生成序列号

autonumber

participant B as 老板

participant M as 小明

B ->> M: 你好

M -) B:我爷爷是董事长

M --) B:gun

B -) M : 好勒

https://img1.sycdn.imooc.com//61128cc0000172bd05470430.jpg


作者:MySQ
链接:https://juejin.cn/post/6993339707555840007
来源:掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消