umlobject-diagram

In a UML Object Diagram how to represent relationships through lists/collections?


If I am modelling in a UML Object Diagram (also known as an "instance diagram") of a Year class which has a field which is a list of Months, should I explicitly create an box for the List instance as well? To me, it seems, like if I draw lines connecting the instance of Year to the 12 Month instances directly, then I expect to see 12 fields on Year like month1, month2, etc.

If you do connect them directly, then how do you in the diagram make it clear that the relationship is not direct but through a list? Or is this diagram more conceptual and the precise nature of these relationships ambiguous?

tl;dr: In a UML Object Diagram, do collections like lists get their own boxes through which your models connect? If not, how to represent this?


Solution

  • In fact, you should first determine your class diagram. An object diagram will then derive naturally.

    I would represent the class diagram this way:

    diagram1

    And with this class diagram, what you say yourself for an object diagram is correct (drawing lines connecting the instance of Year to the 12 Month instances) because each connecting line is itself an instance of the arrow in the class diagram. (in UML terminology, a Link is an instance of an Association)

    diagram2

    Of course, if you class diagram instead just exposes an attribute months : List, the answer will be different. But it does not seem suitable.

    Note1: you see in my class diagram that there is no hint that it is actually a List. It's because to my mind it is merely an implementation detail. It could as well be an array, for example, but the important point is the multiplicity 12, which means that it is a multiple value.

    Note2: do not worry about ehat you say about 12 fields in the ckass Year, it is not the case. In an object diagram, field values are represented directly in the class instance, like attributes in the class diagram

    Edit:

    If you want to make the distinction absolutely clear, you may name the arrow tip months for every month instance, thus stating that all correspond to a unique same months field. They would be named month1, month2, ... in the other case. The same way, you would name the arrow tip in the class diagram as well.