tags:

views:

502

answers:

5

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.

A: 
             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.
Mark
That'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.
gooli
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
A: 

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.

chimp
+1  A: 
           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).

ShiDoiSi
A: 

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.

Gabriel Ščerbák
+2  A: 

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)

Chris