One of the least known and understood concepts of UML is templates. Section 17.5 on version 2.1.1 of the UML specification covers the Templates package in 31 pages. What follows is an attempt at providing a summary of the mechanism in a way that is easy to understand without actually omitting any important details.
A simple example
The following example in TextUML should be easy to understand for any developer familiar with C++ parametrized types or Java generics:
class Foo end; class Bar<T> attribute prop1 : T; operation op11(par1 : T); end; class Fred attribute attr1 : Bar<Foo>; end;
Class ‘Bar’ is a template class, whose template signature contains a single parameter: ‘T’. The type of the property ‘prop1′ is defined as the template parameter ‘T’. Class ‘Fred’ declares ‘someOp1′, an operation that takes a parameter whose type is a binding of the template class ‘Bar’. ‘Bar”s template parameter ‘T’ is bound to the class ‘Foo’. Implicitly, the type of ‘Fred.attr1′ when expanded against Foo should look something like:
class BarOfFoo attribute prop1 : Foo; operation op11(par1 : Foo); end;
Note that the expanded class has actually no name, but I am calling it ‘BarOfZoo’ for pedagogical reasons.
Looking closer at the abstractions
- TemplateableElements - abstract super-class for elements that can be declared as templates, or that can bind other templates to a set of parameters. Four kinds of elements can be declared as templates in UML 2.*: Classifier, Operation, Package and StringExpression, and thus only those metaclasses specialize TemplateableElement*.
- ParameterableElements - abstract class that is specialized by any type of element that can be used as parameters to templates.
- TemplateSignature - a template signature is owned by a template element and contains the set of parameters declared by a template element.
- TemplateBinding - a template binding represents the “instantiation” of a template in the form of a directed relationship between a template signature and a a bound element, another templateable element. In addition to tying the template to the bound element through the template’s signature, the template binding contains a set of template parameter substitutions. Which takes us to the next abstraction…
- TemplateParameterSubstitution - a template parameter substitution is created for every template parameter declared by a template signature. It binds an open parameter to an actual parameter, which is a ParameterableElement.
Would you like to play with templates in UML? For now, you will have to look elsewhere. There is some support for templates in the TextUML Toolkit 1.0 M2, but it is, to put it mildly, half-baked. Full template support is planned for M3 (whenever it happens), and that is exactly what I am working right now. I know I am close to getting it right, but assignment compatibility involving template/bound classifiers get be really tricky to implement. Well, whenever I am done, you will learn it first here.
*in the Eclipse UML2 API, Property also specializes TemplateableElement. That seems to be a deviation from the spec, and a bug report has been submited.