时序图有什么用
时序图是一个很好用很直观的图,它展示了消息是如何在各个对象之间流转的。
时序图的组成
对象(Object)和生命线(Lifeline)
生命线头上那个方正的框里面存放的就是对象,对象有自己的名字,生命线其实就是从上到下的一个虚线。生命线标示一个对象存在的生命周期,两条生命线中间通过消息连接起来,

消息(Message)
消息用于对象间传递信息,对象之间的信息互通就是通过消息,消息按照分类可分为:同步消息(Synchronous Message),异步消息(Asynchronous Message)和返回消息(Return Message) 自关联消息(Self-Message)
- 同步消息:发送消息的对象在发送消息后会挂住,等消息接受对象接受消息返回后才会解除挂住的状态继续自己的工作。
- 异步消息:发送消息的对象在发送消息后会继续自己的工作,而不等消息接受对象接受消息返回。
- 返回消息:标示发送消息后返回的动作
-
自关联消息:一个对象内自调用的情况。
时序图.png
Combined Fragments
标示有一定条件的消息发送,
- Alternative fragment(denoted “alt”) 标示 if…then…else
- Option fragment (denoted “opt”) 标示Switch
- Parallel fragment (denoted “par”) 标示同时发生
- Loop fragment(denoted “loop”) 标示for
- Break标示退出循环
1.loop:
当没有指定循环边界默认范围为[0,无穷大]:

如果只指定了一个值,那么默认执行该值次数:

指定了循环边界,则最少执行最小值值,最多执行最大值次数:

实现dowhile方式,至少执行一次,如果size<0则继续,直到size>0退出循环:

2. alt:
条件判断,如果n>0则执行agree函数否者执行reject函数

3. opt:
switch,当满足不同条件执行不同方法:

4. break:
n=10时候执行save并退出循环

5. par:
同时进行,比如多个线程同时执行任务

一个例子

(开篇)UML-开篇
(下一篇)UML-活动图
网友评论