2

As per this answer, class template could be represented like this:

___________:  T  :
|          :.....:
|              |
|  ClassName   |
|              |
|______________|

Then how to represent the composition relationship with class template?

For example, given

template <typename T>
class FooRes
{
public:
     T  res;
     bool valid;
};

,how to represent the relationship with the classes below?

class Demo
{
    public:
       int height;
       int width;
       int area;
};

class MultiRes
{
private:
     FooRes<Demo>   res1;
     FooRes<int>    res2;
     FooRes<double> res3;
};

I think a class diagram for the said example could make it clear enough.

Could somebody shed some light on this matter?

Christophe
  • 68,716
  • 7
  • 72
  • 138
John
  • 2,963
  • 11
  • 33
  • Why do you insist on composition? These are just three (generic) attributes. – qwerty_so Apr 20 '22 at 08:05
  • @qwerty_so No, I am not. I am just a newbie in the class diagram. How to draw the relationship between them? *Generic attributes*? I think the relationship between `class MultiRes` and ` FooRes` is composition. – John Apr 20 '22 at 08:17
  • Composite aggregation is about lifetime of object. That's usually of no interest. Shared aggregation has no semantics at all. Don't use any of the diamonds unless you know why (most people just use them because they think they must). Just go with simple attributes here (or roles along associations). I'm not that firm with <> and generics so I will not write an answer. – qwerty_so Apr 20 '22 at 08:20
  • @qwerty_so 1.So If I understand you correctly, you think the relationship between `class MultiRes` and `FooRes` is association. Am I right? 2.What do you mean by “shared aggregation has no semantics at all”? Could you please explain that in more detail for me? – John Apr 20 '22 at 08:27
  • See https://stackoverflow.com/questions/9640885/uml-aggregation-vs-association and my answer in https://stackoverflow.com/questions/48268986/is-correct-relationships-of-class-diagram-in-uml – qwerty_so Apr 20 '22 at 08:35

1 Answers1

0

In this answer to your other question, I developed the different ways to represent templated classes and their instantiation. You could use any of them for MultiRes, except that you would have several instantiations of a template instead of a single one.

You could use a similar technique here, but brought to the next level of compactness:

  • First, you'll model the template class FooRes with parameter T and show how it related to this abstract T.
  • Then, once the template well defined, you could use it in other diagrams either with direct bindings, or even as bound properties of MultiRes

The first would facilitate showing the links to the types used as template parameter. It would look like:

enter image description here

The second would look much more compact:

enter image description here

Christophe
  • 68,716
  • 7
  • 72
  • 138