In a file of my library I have a class that inherits from a template.
Example of my code:
class data{ ... };
class dataA: public data{ ... };
class dataB: public data{ ... };
//inheritance from a template
template<typename dataHandler>
class myClass: public dataHandler{ ... };
The template dataHandler
in my code can be either data
, dataA
or dataB
.
I would like to write an UML diagram for the class myClass
making clear the inheritance form the previous datatype.
It seems it can't be done in UML. According to UML specification ,
Class
is a kind of EncapsulatedClassifier
- clause 11.4 EncapsulatedClassifier
is a kind of StructuredClassifier
- clause 11.3 StructuredClassifier
is a kind of Classifier
- clause 11.2 Classifier
is a kind of TemplateableElement
- clause 9.2 TemplateableElement
is a kind of Element
- clause 7.3 Thus Class
is not a kind of TemplateParameter
(clause 7.3) and dataHandler
is an instance of TemplateParameter
and myClass
is an instance of Class
.
The problem that you encounter is related to the way UML deals with the substitution of template parameters with the bound elements. And this is primarily caused by the notation and not the inheritance.
In fact, if you would have a myClass
with only composition:
template<typename T>
class myClass {
T a;
vector<T> v;
...
};
using myClassDataA = myClass<dataA>;
you could represent it in a self-contained way in UML with, the template parameter being used only within the template class:
But there is as far as I know, no way to templatize an equivalent expression of the same class, if you would like to show the T properties as association end:
And for the inheritance, it's the same issue: the inherited class is outside the template class. But here, there is no notational shortcut that could magically include it in the template box.
If it is mainly for graphical communication of a C++ design, you could suggest visually, that a group of classes all depend on the same template parameters. In my composition example above, it would look like:
For your inheritance situation it would be:
But keep in mind that there is no semantic defined in UML for that. For instance, nothing would allow an UML tool to identify all the members of myClass<dataA>
that would be inherited from dataA
. In other words, no UML code generator or tool would be able to produce something meaningful with the models, but fellow C++ programmers will perfectly understand what you mean.
While your template construction is very powerful, it is also very error prone as the operations that you can perform on the instantiation of the same template, would depend on the bindings. This means that you would need different test cases for every single instantiation of the template.
So it's worth to remind the general principle:
So just create a dataHandler
member in your template class, and forward a set of well-identified operations that should be common to all handlers to this member. You then could model it in UML without difficulty using the existing UML semantics.
I know, it's not so flexible; but I think the little extra hassle for the operation forwarding, will save you a lot of troubles later. If really you need more flexibility, you could also think to invert the inheritance using CRTP but this time with a much clearer and unambiguous UML modeling .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.