UML Class Diagram – How to Represent Two-Way Composition

class-diagramuml

If class A has many of class B and class B has many of class A, should we place a diamond on both sides of the line between them?

Like in the picture below, or is it fine to just indicate the amount in both directions and have the diamond only on one side?

enter image description here

Best Answer

Simple answer is: no. You can't have diamonds on both ends of the same line.

Aggregation is a whole/part relationship (having diamond on a whole side). A single association represents a particular relationship between instances of a class and thus the same object cannot be simultaneously a part of other object and a whole containing the same object. While hard to imagine and bringing other problems on table one could try to build such construct, where object of type A is build of objects B and object of type B is build of objects of type A (the only examples that come to my mind at the moment are some math structures) but this would be anyway two different relationships (so two different lines connecting the same classes A and B but each of the lines having the diamond on different class end).

In your example the tyre is a part of (at most one) car so the diamond will be on car side. If you want to show navigability from a tyre to a car (i.e. tyre "knows" to which car it is connected) use the arrow in the car direction just before the diamond. Plus use multiplicity of course (the multiplicity at whole part can be zero.

Related Topic