Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you model a Java for each loop on a UML sequence diagram?

for (Item i : collection) {
i.foo();
} 

In some abstract sequence diagrams you have a loop combined fragment with a condition along the lines of e.g. [for each item].

On a sequence diagram featuring Java implementation; what would you use as a loop guard?

If all lifelines must represent an object instance, how would you show that i: Item is not a single instance, but on each iteration a different object from the collection?

like image 897
Anastasia Avatar asked Mar 05 '17 18:03

Anastasia


2 Answers

You don't show each single bit in a SD but just an overview: it's an abstraction.

enter image description here

This quite clearly shows the intention of your loop.

You would show different Item instances only if you want to show different behavior during the loop.

like image 115
qwerty_so Avatar answered Sep 28 '22 03:09

qwerty_so


For the second part of your question, there is a notation to draw multiple instances in the same lifeline using stacked boxes, for example:

UML sequence diagram loop with multiple instances

from A Quick Introduction to UML Sequence Diagrams

The stacked boxes used for the figures variable indicate it refers to multiple instances, which can be interpreted as being a different instance around the loop. (As usual with UML, the semantics a somewhat in the eye of the beholder)

P. 571 of the UML 2.5 spec states:

The Lifeline head has a shape that is based on the classifier for the part that this lifeline represents. Often the head is a white rectangle containing the name.

The word "Often" can be interpreted as "take what is needed" as it does not obtrude the use of a single specific form.

like image 21
Pete Kirkham Avatar answered Sep 28 '22 05:09

Pete Kirkham