Templates aka generics aka instantiable types in UML
Hello,
Since Java and .NET have generics the usage of so called class templates has become much more widespread than in the days before where only C++ had reasonable support for templates. Therefore I thought it would be interesting for our readers to read a bit about how templates are integrated in the UML.
Template definition in the UML specification
Chapter 17.5 in the ULM 2.1.1 superstructure specification is devoted to the topic of templates. It describes in detail how templates are modeled in the UML metamodel but only briefly illustrates how templates are used at the model level.
Description of the template concept
In general, the important concepts are template (usually - and in the case of Java and .NET always – template classes) and template parameter (therefore, templates are also called “parameterized types”). The template class defines a class but for one or more of the types used in the class (as member and/or as part of the argument list of some method(s) and/or as return value of some method(s)) a placeholder is used instead of concrete, defined types. These placeholders are declared as part of the template definition, immediately before the template class:
C++ example:
template <class T> class Mypair { public: Mypair(T first, T second) { values[0] = first; values[1] = second; } T values [2]; };
This template definition defines the template parameter T and the template class Mypair. An instance of this class stores two values of type T. The template parameter T is used to declare a member – the array values – and the types of two arguments of a method – the constructor Mypair(T first, T second).
Such a class cannot be used directly. You have to create a concrete template instantiation to use the class. Conceptually, each template instantiation is a new class which is independent of all other instantiations of the same template class. In the context of the example above you could create two instantiations of the Mypair class, one for variables of type int, the other one for variables of type double:
Mypair<int> intPair(0, 42); Mypair<double> doublePair( 2.71, 3.1415);
then intPair and doublePair can be used to store two variables of type int or double, respectively. Both template instantiations would be treated as discrete types.
The most popular application is the creation of strongly typed container classes, e.g. lists or hash tables. In this case the library developers provide an implementation of a container as template class and the client of the library may then instantiate the container for a given data type. The instantiated class can then handle only elements which are of the same type as the template parameter. This assures compile time type safety. Sometimes, the type parameters must have certain characteristics to be used with the template class. (e.g. a template parameter for a C++ STL container must have a default constructor). Java and .NET have some language level construct to specify such constraints, whereas C++ does not have such capabilities.
Although containers are probably the most prominent example for templates, there are of course many other applications for templates.
Finally, I’d like to mention that in UML not only classes but also packages and operations can be parametrized.
Template representation
The UML specification suggests a graphical representation (”Notation”, as they call it) but strangely it is only presented verbal and not as image.
Here is, how it looks in Enterprise Architect:
If you have any questions concerning this topic please ask!
Best regards,
Andreas
Update: I found another post on the same topic.

July 21st, 2008 at 10:44 pm
[...] zu lösende Aufgabe bestand darin, einen String zu parsen, der einen “instantiierten Typ” – i.A. template oder generic genannt – verarbeitet und die verwendeten Typen als Liste [...]