2
class Foo{};

class Demo
{
public:
    std::vector<Foo> foo_vec;
};

How to draw the relationship between them?

How to tell the others that the foo_vec is vector in the class diagram(I found this)? And some posts tell me to add notation <:vector> on the line that represent composition.

Christophe
  • 68,716
  • 7
  • 72
  • 138
John
  • 2,963
  • 11
  • 33

1 Answers1

1

If you want to disclose the details of the implementation, you could go for something as developed as:

enter image description here

The key of this design is to disclose that vector is a templated class, and that it is instantiated by binding the template parameter T to the class Foo. To put Foo in the picture, you could either just show it at the instantiation level, but I preferred to show some kind of relation with T. I used here a realization, since T implicitly defines in the C++ semantic an interface for the template parameter, and Foo is expected to implement this (implicit) interface. I also used composition, since C++ vectors are by value, and the vector owns the lifecycle of its elements.

Unfortunately, this makes the design look more complex than it is in reality. Moreover it doesn't show the full reality of a std::vector that has a second template parameter Allocator. Last but not least, to be fully transparent, you should use a qualified composition, with a std::vector::size_type qualifier (to document that there is an indexed access possible). You'd end-up with a very complex diagram.

I therefore recommend to consider a simplified approach. Since everybody knows what a std::vector is, you may use the more compact (and still legit) form of a direct binding, assuming that the template details are defined elsewhere:

enter image description here

This is very close to your implementation and stays very readable. However, you could simplify one step further: is vector a fundamental design choice? Or is it just one practical way to implement a one-to-many composition? If the real intent is just that Demo may be composed of several Foo you could be as simple as:

enter image description here

Here you would let to the implementer to chose the best way to implement the composition instead of graving the implementation choice in the marble of your model.

Christophe
  • 68,716
  • 7
  • 72
  • 138
  • Should I add the multiply(i.e.: `0...*`) in the last diagram. If I add the said multiply, it's misleading that somebody may think there are many `vector`(i.e.: `vector>`).. – John Apr 25 '22 at 05:29
  • For the first diagram, why you say `T` is a composition of `vector`? – John Apr 25 '22 at 05:34
  • @John yes 0..* would be better. The ambiguity is due to the naming of the property, which bears its type. This is no longer seen as a good practice by the way. Call it foo of foos (there are some debate about that) or something that says something about which foos it is. The vector is only the mean to the end. If you think it’s important to keep the vector as something to be modelled, then consider the second diagram which makes a distinction between the container and its eleements. you’d have multiplicity 1 then 0..* . Dot notation could be used on the vector end of left association – Christophe Apr 25 '22 at 07:02
  • @John aggregation, shared (white diamond) and composite (black diamond) are placed on the side of the whole and not the side of the part. Do the first diagram says that vector is the composite whole made of plenty of T componenents/parts ;-) – Christophe Apr 25 '22 at 07:05