UML顺序图介绍
This UML sequence diagram tutorial introduces the commonly used elements of UML sequence diagrams and explains how to use them.
All diagrams in this guide were created with Trace Modeler, an easy-to-use and smart editor for UML sequence diagrams developed by the author. Check out the 30 sec demo to see how easy it really is..
UML Sequence Diagrams UML顺序图)
UML sequence diagrams are used to show how objects interact in a given situation. An important characteristic of a sequence diagram is that time passes from top to bottom : the interaction starts near the top of the diagram and ends at the bottom (i.e. Lower equals Later).
UML顺序图用于显示对象是如何在一个给定的情境下进行交互的(interaction)。顺序图的一个重要特征是时间从顶部到底端:交互从图例顶端开始,终止于底部(也就是说,越低越晚)
A popular use for them is to document the dynamics in an object-oriented system. For each key collaboration, diagrams are created that show how objects interact in various representative scenarios for that collaboration.
它的一个典型用法是用来演示面向对象的系统中的动态演化。对于每个关键的协作而言,就会为之建立一些顺序图来演示该协作各个场景中对象是如何进行交互的。
The UML sequence diagram gallery contains many examples, but here's a typical sequence diagram based on a system use case scenario :
The diagram above shows how objects interact in the "rent item" collaboration when the item is not available during the requested period.
上面的图例显示了当在需求期间租赁物品不可用时,“租赁物品”协作过程中对象进行的交互。
To clarify how execution switches from one object to another, a blue highlight was added to represent the flow of control. Note that this highlight is not part of the diagram itself.
为了阐述从一个对象到另一个对象过程中,执行是如何进行转换的,我们添加了一个蓝色的路径来表示控制流。注意,该路径不是顺序图的一部分。
As with all UML diagrams, comments are shown in a rectangle with a folded-over corner :
对于所有的UML图例,注释放在一个带重叠边角的矩形框中。
To relate the comment to whatever diagram elements it is about, connect them with dashed lines.
如果要对某个图标元素添加注释,就可以使用点画线将其连接到该元素。
Targets(目标)
Objects as well as classes can be targets on a sequence diagram, which means that messages can be sent to them. A target is displayed as a rectangle with some text in it. Below the target, its lifeline extends for as long as the target exists. The lifeline is displayed as a vertical dashed line.
对象和类型能够作为顺序图的目标,也就是说消息可以发送给它们。目标显示为在矩形框中添加某些文本,它的生命周期就是目标存在的时间。生命周期显示为一个垂直的点画线。
Object(对象)
The basic notation for an object is
一个对象的基本标记是:
Where 'name' is the name of the object in the context of the diagram and 'Type' indicates the type of which the object is an instance. Note that the object doesn't have to be a direct instance of Type, a type of which it is an indirect instance is possible too. So 'Type' can be an abstract type as well.
‘name’是顺序图例内对象的的名字,’type’指得是对象所属类型。注意,对象不一定非得必须是类型的直接实例(也就是说,对象可以是该类型子类的具体实例)。
Both name and type are optional, but at least one of them should be present. Some example :
对象名和类型名是可选的,但是至少必须存在一个。
As with any UML-element, you can add a stereotype to a target. Some often used stereotypes for objects are «actor», «boundary», «control», «entity» and «database». They can be displayed with icons as well :
对于UML元素,你可以添加一个场景给目标。常用于对象的场景有«actor», «boundary», «control», «entity» and «database», 它们也可以以图标的形式展示出来:
An object should be named only if at least one of the following applies
如有存在如下几种情况中的一种,对象必须命名:
·         You want to refer to it during the interaction as a message parameter or return value
·         你在以后的交互中必须引用它作为消息参数或者是返回数值。
·         You don't mention its type
·         你没有指定它的类型。
·         There are other anonymous objects of the same type and giving them names is the only way to differentiate them
·         存在其他具有相同类型的对象,给它指定一个名字是唯一区分它和其他对象的方法。
Try to avoid long but non-descriptive names when you're also specifying the type of the object (e.g. don't use 'aStudent' for an instance of type Student). A shorter name carries the same amount of information and doesn't clutter the diagram (e.g. use 's' instead).
MultiObject(多个对象)
When you want to show how a client interacts with the elements of a collection, you can use a multiobject. Its basic notation is
如果你想要显示客户端如何和一集数据进行交互时,则可以使用multiobject,它的基本标记为:
Again, a name and/or type can be specified. Note however that the 'Type' part designates the type of the elements and not the type of the collection itself.
同样,可以指定名字和类型。注意,’type”部分指定的是其中元素的类型,而不是数据集合的类型。
 
Class(类型)
The basic notation for a class is
类型的基本标记为:
Only class messages (e.g. shared or static methods in some programming languages) can be sent to a class. Note that the text of a class is not underlined, which is how you can distinguish it from an object.
只有类型消息(如某些编程语言中的共享或者静态方法)能够发送给一个类型。注意,类型的文本没有下划线,而对象的文本却又下划线。
Messages(消息)
When a target sends a message to another target, it is shown as an arrow between their lifelines. The arrow originates at the sender and ends at the receiver. Near the arrow, the name and parameters of the message are shown.
如果一个对象发送一个消息给另一个目标,它在他们的生命周期之间显示为一个箭头。箭头始于发送者,终于接收者。在箭头附近,显示了消息的名字和参数。
Synchronous message(同步消息)
A synchronous message is used when the sender waits until the receiver has finished processing the message, only then does the caller continue (i.e. a blocking call). Most method calls in object-oriented programming languages are synchronous. A closed and filled arrowhead signifies that the message is sent synchronously.
如果消息发送者必须等到接收者处理完消息以后才返回,就可以使用同步消息。在面向对象语言中,许多的函数调用是同步的。实心填充的箭头标明消息被同步发送出去。
The white rectangles on a lifeline are called activations and indicate that an object is responding to a message. It starts when the message is received and ends when the object is done handling the message.
生命周期中的空白矩形方框被称为激活,用来指示对象对消息作出的反应。它从接收到消息开始,直到消息被处理结束为止。
When a messages are used to represent method calls, each activation corresponds to the period during which an activation record for its call is present on the call stack.
当一个消息用于体现消息调用,每个激活对应于一个激活记录存在于调用堆栈上的时间。
If you want to show that the receiver has finished processing the message and returns control to the sender, draw a dashed arrow from receiver to sender. Optionally, a value that the receiver returns to the sender can be placed near the return arrow.
如果要显示接收者完成处理消息,并且返回控制给发送者,可以画一个从接收者到发送者的点画箭头。可选地,你可以将返回值放置在返回箭头附近。
If you want your diagrams to be easy to read, only show the return arrow if a value is returned. Otherwise, hide it.
如果你想要你的图更加清晰,那么最好只画出那些必须返回值的箭头。
Instantaneous message(即时消息)
Messages are often considered to be instantaneous, i.e. the time it takes to arrive at the receiver is negligible. For example, an in-process method call. Such messages are drawn as a horizontal arrow.
消息通常被认为是即时的,比如说,消息发送到接收者的时间是可以忽略的。像进程内的方法调用,这样的消息可以画为一个水平的箭头。
Sometimes however, it takes a considerable amount of time to reach the receiver (relatively speaking of course) . For example, a message across a network. Such a non-instantaneous message is drawn as a slanted arrow.
但是有时候,可能某些情况下,消息到达接收者需要花费一定的时间(相对而言),必须说通过网络传送的消息。这样的非即时消息画作一个斜的箭头。
You should only use a slanted arrow if you really want to emphasize that a message travels over a relatively slow communication channel (and perhaps want to make a statement about the possible delay). Otherwise, stick with a horizontal arrow.
只有在想要表示消息传送需要花费一定的时间,才使用斜箭头,否则使用水平箭头。
Found message (匿名信息)
A found message is a message of which the caller is not shown. Depending on the context, this could mean that either the sender is not known, or that it is not important who the sender was. The arrow of a found message originates from a filled circle.
匿名信息是发送者未知(或者是对于接收者而言无足轻重)的消息,匿名消息起始于一个填充的圆心。
Asynchronous messages(异步消息)
With an asynchronous message, the sender does not wait for the receiver to finish processing the message, it continues immediately. Messages sent to a receiver in another process or calls that start a new thread are examples of asynchronous messages. An open arrowhead is used to indicate that a message is sent asynchrously.
对于一个一步消息而言,发送者无需等待接收者处理完消息也可以继续执行。发送给另一个进程或者启动一个新线程的调用的消息就是一部消息的示例。一个未填充的箭头用来指示消息异步传送。
A small note on the use of asynchronous messages : once the message is received, both sender and receiver are working simultaneously. However, showing two simultaneous flows of control on one diagram is difficult. Usually authors only show one of them, or show one after the other.
值得注意的是,对于异步消息,一旦消息被接受,发送者和接收者同时工作。然而,要在顺序图中显示两个同时进行的控制流是很困难的,所以通常作者只显示它们中的一个或者是一个在另一个的后面。
Message to self(发送给自己的消息)
A message that an object sends itself can be shown as follows :
一个对象发送消息给自己可以显示如下:
Keep in mind that the purpose of a sequence diagram is to show the interaction between objects, so think twice about every self message you put on a diagram.
注意,顺序图的用途是显示对象之间的交互,所以在使用发送给自己消息之前,慎重思考。
Creation and destruction(创建和释放)
Targets that exist at the start of an interaction are placed at the top of the diagram. Any targets that are created during the interaction are placed further down the diagram, at their time of creation.
在交互开始时存在的目标放置在顺序图的顶端。任何在交互期间创建的目标放置在顺序图的下方(也就是它被创建时的位置)。
A target's lifeline extends as long as the target exists. If the target is destroyed during the interaction, the lifeline ends at that point in time with a big cross.
目标的生命周期和目标存在的时间一样长。如果目标在交互期间被消除,生命周期随机结束,并在该处画上一个大叉。
Conditional interaction(条件交互)
A message can include a guard, which signifies that the message is only sent if a certain condition is met. The guard is simply that condition between brackets.
消息能设置一个警戒,它表示消息只有在某个特定的条件满足的条件下,消息才会发送出去。警戒是在大括号之间的条件。
If you want to show that several messages are conditionally sent under the same guard, you'll have to use an 'opt' combined fragment. The combined fragment is shown as a large rectangle with an 'opt' operator plus a guard, and contains all the conditional messages under that guard.
如果要设定几个消息在同一个警戒条件下被发送出去,可以使用’opt’联合片段。’opt’联合片段显示为一个大的’opt’矩形以及一个警戒条件,在警戒条件下包含了所有的要发送的条件信息。
A guarded message or 'opt' combined fragment is somewhat similar to the if-construct in a programming language.
警戒消息或者’opt’联合片段就有点像编程语言中的if结构。
If you want to show several alternative interactions, use an 'alt' combined fragment. The combined fragment contains an operand for each alternative. Each alternative has a guard and contains the interaction that occurs when the condition for that guard is met.
如有想显示在不同条件下几种不同的交互,可以使用’alt’联合片段。联合片段为每种选择提供了一个操作数(operand) 每种选择有一种警戒,并且包含了当该警戒条件满足时将发生的交互。
At most one of the operands can occur. An 'alt' combined fragment is similar to nested if-then-else and switch/case constructs in programming languages.
操作数中的最多只能有一个发生。’alt‘联合片段和编程语言中的套入式if-then-else以及switch/case结构有些相似。
Repeated interaction(重复交互)
When a message is prefixed with an asterisk (the '*'-symbol), it means that the message is sent repeatedly. A guard indicates the condition that determines whether or not the message should be sent (again). As long as the condition holds, the message is repeated.
如果消息前面添加了一个星号前缀,这意味该消息将被重复地发送。有一个警戒用来指示该消息是否应该重复发送的条件,只要条件满足了,消息就会重复发送。
The above interaction of repeatedly sending the same message to the same object is not very useful, unless you need to document some kind of polling scenario.
上面的交互中,重复给同一个对象发送相同的消息没有多大的用处,除非你需要写一些调查场景。
A more common use of repetition is sending the same message to different elements in a collection. In such a scenario, the receiver of the repeated message is a multiobject and the guard indicates the condition that controls the repetition.
一个更加常用的重复交互是发送相同的消息给某一集合中不同元素。在该场景下,重复消息的接收者是一个多对象(muliobject),警戒表明了控制该重复的控制条件。
This corresponds to an iteration over the elements in the collection, where each element receives the message. For each element, the condition is evaluated before the message is sent. Usually though, the condition is used as a filter that selects elements from the collection (e.g. 'all', 'adults', 'new customers' as filters for a collection of Person objects). Only elements selected by the filter will receive the message.
上面的顺序图对应集合中的某一元素的交互,该集合中每个元素都将接受消息。对于每个元素,消息在发出去之前都得必须先进行条件测试。通常这些条件都是用来在集合中进行过滤选择一些元素(比如在一群人中选择“所有”,“成人“, ”新顾客“等)。只有通过过滤的元素才能收到消息。
If you want to show that multiple messages are sent in the same iteration, a 'loop' combined fragment can be used. The operator of the combined fragment is 'loop' and the guard represents the condition to control the repetition.
如果想要显示多个消息在同一个重复过程中发送,则可以使用一个’loop’联合片段。联合片段的操作者(operator)’loop’,警戒条件代表控制重复的条件。
Again, if the receiver of a repeated message is a collection, the condition is generally used to specify a filter for the elements.
同样,如果一个重复消息的接收者是一个集合,通常设定一个条件来对集合中的元素进行筛选。
For example, to show that the bounds of a drawing are based on those of its visible figures we could draw the following sequence diagram :
比如,要表示一幅画的边界是基于其可见部分数据,可以做出如下的顺序图。
Several things are worth noting in this example
这个示例中,有几点值得注意的:
·         a local variable 'r' was introduced to clarify that it is the result of getBounds that is added.
·         引入了一个本地变量’r’来表明它是添加的getBounds返回的结果。
·         naming the resulting Rectangle 'bounds' avoids the introduction of an extra local variable.
·          为生成的矩形命名’bounds’避免了引入额外的本地变量。
·         the loop condition is used as a filter on the elements of the figures collection.
·         循环条件用来过滤数据集合中元素。
Tool support (工具支持)