Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to indicate open generic type construction in UML class diagrams

As far as I could find out, supplying type arguments for an open generic type can be expressed in UML class diagrams by using the bind stereotype.

For instance, take the following classes:

public class A<T> {
}

public class B {
  public A<string> Value { get; set; }
}

This could be modelled as a UML class diagram as follows:

class diagram with closed generic type

But how about the case where we pass through the generic type argument, thereby constructing an open generic type based on A<T>?

public class A<T> {
}

public class B<X> {
  public A<X> Value { get; set; }
}

Based on the above notation, I think the UML class diagram should look like this:

class diagram with open generic type

But this looks somewhat confusing, given that it is not immediately obvious where X comes from - after all, it is just declared within the scope of B, but is seemingly used like a globally valid declaration in A<X>. The problem becomes obvious once multiple types reference A and supply some generic parameter called X - e.g. let's add the following class to the mix:

public class C<X> {
  public A<X> Data { get; set; }
}

This leads to the situation where we either end up with multiple nodes that seem absolutely identical:

class diagram with seemingly identical nodes

... or with an identifier X that represents multiple distinct (separately declared!) things at a time.

class diagram with ambiguous X

Is there a common convention on how to handle this?


Note that the class labels of the constructed types might look even nicer if I wrote e.g. A<string>, but I could not find out how to do this in PlantUML without the software rendering string as an actual open generic parameter. Therefore, square brackets were used in this post, resulting in labels such as A[string].

like image 275
F-H Avatar asked Oct 19 '25 19:10

F-H


1 Answers

You are addressing here a key weakness of templates in UML, which leave many modern generic programming constructs ambiguous.

The problem, is that nothing relates the X used outside the template with the template parameter X. The template parameter is only unambiguously defined within the template and when you use a binding expression (by the way, you should write «bind» T->string or «bind» T->X to clarify which template parameter you're binding). As soon as you use it outside the template and without binding, it is in reality not related to the template parameter and you only visually can associate them (see also this other answer) and hope that your readers share the same understanding.

The problem starts already here:

public class B<X> {
  public X Value { get; set; }
}

If you model this with an attribute Value:X in the class, it is clear that it's of template type. But if you model it as a property, i.e. the owned end of an association with X, it is ambiguous. This makes it especially difficult to model the use of several template classes combined.

Your example is just the next level of difficulty.

like image 169
Christophe Avatar answered Oct 23 '25 11:10

Christophe