如何在UML序列图中绘制多个返回

时间:2012-06-12 13:57:29

标签: uml sequence-diagram

在UML序列图中,一个方法只能有一个返回行(至少在Astah中我无法从一个方法中绘制两个返回行),如何在if-else块中对多个返回进行建模?

在下面的例子中,返回行'a'结束methodX(),如何绘制返回行'b'?

String methodX(int i) {
  if (i>0)
    return 'a';
  else
    return 'b';
}

     +------------+            +------------+
     |    Foo     |            |     Bar    |
     +-----+------+            +------+-----+
           |                          |
           |        methodX(i)         |
          +-+----------------------->+-+
          | |                        | |
  +-----+------------------------------------+
  | alt | | |  [i>0]                 | |     |
  +-----+ | |           a            | |     |
  |       | |<- - - - - - - - - - - -+-+     |
  |       | |                         |      |
  |       | |                         |      |
  +------------------------------------------+
  |       | |  [else]                 |      |
  |       | |                         |      |
  |       | |           b             |      |
  |       | |<- - - - - - - - - - - - | ??   |
  |       | |                         |      |
  |       | |                         |      |
  +------------------------------------------+
          | |                         |
          +-+                         |
           +                          +

2 个答案:

答案 0 :(得分:1)

快速观察:

(1)请记住,类/对象可以向自己发送消息(“DoSomething”), 这使得“替代品”更难以实现。

(2)当使用“alt”时,你必须提到条件。 “alt”表示“if-then-else”,“switch-case”以及编程语言中的类似概念。

这些句子有条件,必须添加到UML图表中(“[condition]”)。

(3)在“alt”的每个条件或案例中,在几个对象中,或者对于他们自己,或者没有(仅“返回”箭头)可能有多条消息。

..........................................................
.......+---------+..............+---------+...............
.......|   Foo   |..............|   Bar   |...............
.......+----+----+..............+----+----+...............
............|........................|....................
..........+-+-+....................+-+-+..................
..........|   |......methodX().....|   |..................
..........|   +------------------->+   |..................
..........|   |....................|   |..DoSomething()...
..........|   |....................|   +---+..............
..........|   |....................|   |...|..............
..........|   |....................|   |...|..............
..........|   |....................|   |...|..............
..........|   |....................|   |<--+..............
..........|   |....................|   |..................
..+-----+-------------------------------------+...........
..|.alt.|.|   |....................|   |......|...........
..+-----+-------------------------------------+...........
..|.[option=1].....................|   |......|...........
..|.......|   |....................|   |......|...........
..|.......|   |....................|   +---+..|...........
..|.......|   |....................|   |...|..|...........
..|.......|   |....................|   |...|..|...........
..|.......|   |....................|   |...|..|...........
..|.......|   |....................|   |<--+..|...........
..|.......|   |<-------------------+   |......|...........
..|.......|   |....................|   |......|...........
..+-----+-------------------------------------+...........
..|.[option=2].....................|   |......|...........
..|.......|   |....................|   |......|...........
..|.......|   |....................|   |......|..// The wide bar its kept,
..|.......|   |<-------------------+   |......|..// even if there is a
..|.......|   |....................|   |......|..// previous return arrow
..+-------------------------------------------+...........
..|.[else]|...|....................|   |......|...........
..|.......|   |....................|   |......|...........
..|.......|   |....................|   |......|...........
..|.......|   |<-------------------+   |......|...........
..|.......|   |....................|   |......|...........
..+-------------------------------------------+...........
..........|   |....................|   |..................
..........+-+-+....................+-+-+..................
............|........................|....................
............|........................|....................
............X........................X....................
..........................................................

干杯。

Pd积。猫的任何奶酪牛肉或金枪鱼 - 鱼类?

答案 1 :(得分:1)

问题在于您的工具,而不是UML。查看visual paradigm for UML。您可以在每个alt片段的开头手动添加激活,并在激活结束时发送返回消息。