4
votes

Some classes in my class diagram need to reference specific instances of other classes or themselves. Should I just model those the same way i would in an object diagram or is there a better option? In general: Can one combine class and object diagrams, since object diagrams are themselves class diagrams?

thanks!

2
Can you elaborate on "class need to reference instance". That, honestly, does not make any sense to me. - qwerty_so
I'm trying to model a list. I have a class "List" that's composed of the class "List element". The class "List element" is supposed to reference its successor, an instance of "List element" and "List" is supposed to reference the first element of the list. - Khayet
This is something different to what you asked. Further you still mix instance and class. What do you mean? - qwerty_so
I'm still mixing instance and class because that's what I'm trying to model! A list consists of elements and each element is supposed to know which other element of the list is its successor. Furthermore, the list as a whole needs to know where the list starts (the first element of the list). The first element of the list is one concrete object. "List" and "List element" however are classes. - Khayet
Sorry to be picky, but you can NOT mix class and instance. What you are asking for does simply not make sense. - qwerty_so

2 Answers

3
votes

UML2 introduced Composite Structure Diagram exactly to solve this issue.

In a Composite Structure Diagram you can show classifiers (e.g. Classes) together with theirs internal composition in terms of instances.

This way you can specify exactly which instances are linked to your objects.

See this article for a good explanation.

0
votes

I agree with @Thomas Killian : you want to mix two different representations.

On the class diagram you will be able to show cardinalities, but not instances relationships. It seems your List class is not a simple list but a chain / linked list.

A ListElement is in fact part of a LinkedList. Two predecessor and successor attributes themselves of type ListElement (or how you wish to call this class, the node role could be noted) will be enough to suggest the behavior. As an attribute has a 1:1 cardinality with the encapsulating class, this respects what you wish to model. Renaming the list class would be a hint for the diagram reader.

Take a look at the Java Linked List source if you want some ideas: this could help you to design a clear class structure, the initial author is not so bad at classes design. In this case, he pushes encapsulation to the excess but the idea is right.