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

你说的曾经没有我的故事 提交于 2019-12-23 10:17:47

问题


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?


回答1:


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

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.




回答2:


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

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.




回答3:


Unfortunately it cannot be done. The loop operator has a guard condition and a number of iterations between minint and maxint. I think, this doesn't allow "for each loop" semantics. If you want to add this, it must be done with a user defined stereotype «for each loop»: An example usage is:

Please note that the note symbol is a not a comment, but the notation for the stereotype properties. They reference the property e of Client1 and i of the Interaction Example1 (see diagram below). The i-Property is represented by a Lifeline and will refer to a different object in each occurrence of the loop content. This is achieved here by the user defined semantics of my «for each loop».

As an alternative you could add a semantic free comment with the same content. Misusing the guard for this purpose is not recommended.

If you want to use a java like iterator the diagram could look like this:

This diagram also shows, how to refer to different objects over time: The return value of a reply message is assigned to a Property of the Interaction or of the Class represented by the Lifeline or a Parameter.



来源:https://stackoverflow.com/questions/42612567/how-do-you-model-a-java-for-each-loop-on-a-uml-sequence-diagram

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!