I have a MyServer class that contains a Map whose keys are MyClientType objects and whose values are MyClient objects. I'd like to depict this relationship in a class diagram but I can't figure out a clean way to do that.
6 Answers
You can use a qualified association:
┌──────────┐ 1 ┌───────┐
│ MyServer │Key│───────────│ Value │
└──────────┘ └───────┘
See: http://etutorials.org/Programming/UML/Chapter+6.+Class+Diagrams+Advanced+Concepts/Qualified+Associations/ (cause it is hard to draw using ASCII)
Note also that a qualified association changes the multiplicity:
┌──────────┐ 0..* ┌───────┐
│ MyServer │───────────────│ Value │
└──────────┘ └───────┘
┌──────────┐ 1 ┌───────┐
│ MyServer │Key│───────────│ Value │
└──────────┘ └───────┘
The top illustrates an association from a server to 0-n values. By contrast, the qualified association says that any given key will be associated with only one value, and that you can't have a key an absent value.
-
2This is one of two correct answers. The other is @chimp's. – Jim L. Mar 04 '14 at 01:45
-
If MyServer was "expanded" to show operations and attributes, what would go inside the attribute compartment of MyServer to model the map relationship? – Emile Cormier Aug 26 '15 at 01:38
I would just show an association from MyServer to MyClient with a multiplicity of 0..* at the MyClient end. Everything else is implementation detail and can be left to the programmer.

- 1,823
- 9
- 14
-
-
I upvoted solely because the username is chimp ... not. Nice name AND nice answer. – JimLohse Jan 16 '19 at 23:54
MyServer
|
|*
T1toT2
/ \
1/ \1
Key Value
The difference to Mark's solution is that the server has a many-relation to the containers. That's also how the Eclipse Modeling Framework (EMF) proposes to implement maps.
You might also throw in some more UML-specific things, like specifying that the keys have to be unique (through stereotypes).

- 7,274
- 1
- 32
- 50
First of all I and some others think, UML should contain some basic collection types as it did in some earlier versions. They could be taken for example from the OCL...
The "EMF way" seems right, however it gives imho too much importance to the to type, which is really unimportant imho, so I would model it just as an association class. This will enable you to capture all map specific constraints (as e.g. multiplicity) which can be captured using regular class, but won't make that class as important as the other ones.

- 18,240
- 8
- 37
- 52
MyServer
|
|
|
Map
|
| |
| |
MyClientTypeKey MyClientType
Should it not be quite simple like above?
- MyServer has a one to one assoication with the Map
- The Map has 1 to many associations with both the keys and values.

- 28,783
- 8
- 63
- 92
-
1That's the first thing that came to my mind as well, but it feels the map doesn't deserve to be a class. It's just a method of containment. I don't know, just doesn't feel right. – elifiner Jul 28 '09 at 08:50
-
But then just, say, having the key and type classes directly associated with MyServer doesn't show how they are related. The Map is a container but also shows the relationship between the key and type. – Mark Jul 28 '09 at 09:21
Kru's answer is the best, but it still only hints at a Map.
I would argue it depends on the level of abstraction at which your diagram sits. If it's relatively high, I'd go with chimp's response. If it's relatively low and you really need to show a map, intentionally showing implementation-related detail, I'd go with the following:
MyServer
|
|
Map
|
|*
T1toT2
/ \
1/ \1
Key Value
How the map is then implemented in code is totally irrelevant (T1toT2
run-time objects might not actually come to be).
As mentioned by Gabreil, this could be also be modelled using an association class
MyServer
|
|
Map
|
|*
T1toT2
|
|
1--------1
Key Value
Of course is only matters if you really really need to show or specify a map.

- 746
- 5
- 19