how to show the typedef type in UML class diagram model (visual paradigm)

936 Views Asked by At

I am creating a class diagram in visual paradigm. I have a typedef :

typedef std::vector<A> Va;

and I want to represent this on visual paradigm by showing the type std::vector<A> in the representation but I don't see how

va

1

There are 1 best solutions below

4
Christophe On

Is one stereotype enough?

Visual Paradigm offers a <<Typedef>> stereotype out of the box that can be used as shown in your diagram. It looks like a <<primitive>> type. Unfortunately, it fails to acknowledge that a typedef is only an alias. So how could the model tell anything about attributes or operations of a typedef?

How to show the alias in UML?

The closest way is to use specialization (inheritance). If vA would be an UML specialization of vector<A>, it would have the same (public) attributes and operations.

But the specialization does not fully reflect the true nature of a typedef: a typedef does not define a new distinct type; it only defines a new alternative name for an existing type. You can use any of the aliases interchangeably:

typedef int distance; 
typedef int weight;  
distance d=10; weight w=20; 
auto s=d+w;    // valid, d and w are just int

A very pragmatic and effective way to document this kind of relationship in UML, is to define a stereotype «typedef» stereotype for the specialization, being understood that the C++ semantic would apply:

enter image description here

(The templated vector is not required, you could start at vector<A> if you'd wished)

How to define the specialization stereotype in Visual Paradigm?

First, draw the generalization shape (inheritance).

Right click on the new generalization that you've created from vector<A> to vA, and chose Stereotypes > Edit Stereotypes... in the contextual menu.

Then in the popup window, click on the button Edit Stereotypes.... In the new popup window, click on Add... button, and in the new popup type in typedef as stereotype. Click on ok, and the new stereotype is created, click on ok and it's added to the list of available stereotypes:

enter image description here

Up from now, you can easily select the home-made stereotype to generalizations. You could even decide to selectively show and hide the stereotyped specializations in the diagram, while having all the information in the model (see comments for details).

Shorter way?

If you don't need in your model the operations and attributes of vA, but just want a compact hint about the real type, you could make use of tagged values. These can be associated to stereotypes.

You can for example enrich the built-in <<Typedef>> stereotype for classes, by adding a tagged value named Alias for that takes as value a Model element:

enter image description here

From then onward, for every class that has the <<Typedef>> stereotype you can open its specification and enrich the tab "Tagged values", selecting other elements in your model (not necessarily in the same diagram).

To visualize tagged values, you first need to activate them in the diagram, via the contextual menu (Presentation options > Show tagged values > Show non-empty). You'd then have something like:

enter image description here

Not related:

You may want to consider using:

using vA = std::vector<A>;   // alias instead of typedef

If you think that it's just a matter of stylistic preference, I'd warmheartedly recommend you Scott Meyer's book "Effective Modern C++" and in particular the dedicated section "Item 9: prefer alias declaration to typedefs".