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

Anastasia picture Anastasia · Mar 5, 2017 · Viewed 11.6k times · Source
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?

Answer

qwerty_so picture qwerty_so · Mar 6, 2017

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.