2

In a UML class diagram, the value of one of the attributes of a class is a (typed) dictionary. What is a proper UML type annotation? (Within the box; not as an association.)

For example, in Python I would annotate this attribute with Dict[A,B] or possibly Mapping[A,B]. I am not asking about Python; this is just one example of a somewhat generic attribute annotation. I am open to the possibility that UML does not offer a language agnostic equivalent to this, but I am hoping that is not true.

Comment: I am asking about generic attribute annotation, directly with the class box. I do not wish to add a separate class box for Dict to the diagram. Therefore this is not a duplicate of How can I represent a Python dictionary in UML?

Alan
  • 9,410
  • 15
  • 20

1 Answers1

3

In UML Dict is typically model as a template class with two template parameters for instance named key and value.

Let's say you want the class C has the attribute a being a Dict[A,B], so you have a binding having the template param substitutions key -> A and value -> B.

There are several ways to show that in a class diagram.

For instance using the class DictAB to model Dict[A,B] :

enter image description here

or if you prefer :

enter image description here

But it is also possible to not use the auxiliary class DictAB and to use the textual representation of the binding expression (see formal/2017-12-05 § 7.3.4 page 26) :

enter image description here

bruno
  • 32,421
  • 7
  • 25
  • 37
  • The very last example is what I am looking for. Is such an example in the UML docs? (As an aside, I guess I am really wishing UML directly supported a few ADTs.) Thanks. – Alan Jun 13 '20 at 15:57
  • 1
    @Alan sorry I missed to put the reference to the norm, I edited my anwser to add : [formal/2017-12-05](https://www.omg.org/spec/UML/2.5.1/PDF) § 7.3.4 page 26 – bruno Jun 13 '20 at 16:02
  • 1
    @Alan It seems also the name of the template parameters in `typing.Dict` are *KT* and *KV*, but the way to do is the same of course – bruno Jun 13 '20 at 16:06