2

I am trying to represent my program in a UML diagram. I have 2 classes as below:

Mesh.h

class Mesh
{
public:
    Mesh();
    ~Mesh();
    VertexArrayObject* vao;
};

VertexArrayObject.h

class VertexArrayObject
{
public:
    VertexArrayObject(Mesh* mesh);
    ~VertexArrayObject();
    Mesh* mesh;
};

I'd imagine it'd be drawn like this:

uml diagram

However that doesn't look right at all. How is it best to represent a relationship where both classes have references to each other using UML?

  • As @ThomasKilian said, your UML model is not true. Please post complete API (Method declarations only) for `Mesh` and `VertexArrayObject`. I think they are important in distinguishing between Association and Composition. For example in `Mesh` class, if there is not any method that put instances to `vao`, it can be Composition. – Gholamali Irani Jan 13 '18 at 23:14

2 Answers2

4

No. That's plain wrong. Composite aggregation can only be on one side. (Imagine the car/wheel example: each aggregating the other is nonsense.) Remove the diamonds and you're done.

You can go further and put dots instead of the diamonds. That will mean that both sides have attributes referring the class at the other side. See this answer.

enter image description here

qwerty_so
  • 35,448
  • 8
  • 62
  • 86
0

This depends a lot on what the logical construct is you are wanting to represent. You could specify the relationship between Mesh and Vertex in many ways and get the message across; what I understand from the model you have posted is that you think composition may be an important aspect of the relationship, e.g. Vertex is a composite part of Mesh (or vice versa).

In UML you can’t really have a bidirectional composition since this leads to a logical paradox — I.e. you cant simultaneously have two classes that are at the same time both composite parts of each other. If composition is important then you will need to choose which is composed of which.

Given my (poor) understanding of 3D (I guess that’s what your program is about), I would assume that a Mesh includes a collection of Verticies, so the composition would run from the Vertex type to the Mesh type with the solid diamond at the Mesh end. You might also want to add multiplicities (so that it is obvious that multiple Vertex may exist within the Mesh) and also consider whether the relationship is composite or a shared aggregate (can a Vertex exist outside a Mesh? If so a shared aggregate (white diamond) is needed).

You probably don’t need to represent the relationship from Mesh to Vertex, as this is an implementation detail that may be safely abstracted away in UML. The fact that you’re using a pointer to an array is probably not relevant to the logic that Mesh is composed of many Vertex.

That said, if you do want to specify what you have in code precisely then this can be done in UML. I would recommend abstracting out the Array of Vertex from Vertex — there are three logical types in your code — Mesh, Vertex and VertexArray. You might illustrate Mesh having a basic association with VertexArray (with an arrow to identify direction of reference), and this in turn being composed of Vertex (black diamond at VertexArray end).

Of course, I am assuming that VertexArray represents an array of Vertex objects, not a single Vertex!

muszeo
  • 2,312
  • 9
  • 13