7

在 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             |      |
  |       | |<- - - - - - - - - - - - | ??   |
  |       | |                         |      |
  |       | |                         |      |
  +------------------------------------------+
          | |                         |
          +-+                         |
           +                          +
4

2 回答 2

2

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

于 2012-06-13T13:14:44.033 回答
1

快速观察:

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

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

而那些句子,有条件,必须添加到 UML 图(“[条件]”)。

(3) 在“alt”的每个条件或情况下,可能在几个对象之间有多个消息,或者对它们自己,或者没有(只有“return”箭头)。

..........................................................
.......+---------+..............+---------+...............
.......|   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 有适合猫吃的芝士汉堡或金枪鱼汉堡吗?

于 2012-06-12T18:23:01.450 回答