Search code examples
umlrelationshipclass-diagramobject-oriented-analysis

On which side shall I put the diamond sign in an UML diagram


img1

I am trying to figure out where should the diamond be placed in this example, I understand its a has-a relationship and diamond should be placed to indicate the whole and part relationship. I have created the UML so that the diamond is on the manufacturer side for both car and engine, like below

img2

because it makes sense right? that the car and engine is being produced by the manufacturer, but the correct answer I was given in the class was to put the diamonds on car and engine respectively... it seems a bit odd to me since wouldn't the manufacturer be the "whole" in this relationship? Below is the spec:

img3

I am trying to figure out where should I place the diamond sign


Solution

  • The diagram of the top is wrong, given the specifications:

    • The black diamond ("composite aggregation" in UML-speak or "composition" as shortcut) on the side of the engine would mean that Engine owns Manufacturer, and in particular if an engine is destroyed, so is the owned manufacturer. Same principle for the Car.

    • Moreover, according to the UML specifications, an instance of Manufacturer can only belong to one composite at a time, either a n instance of Car or Engine, but not both at the same time. Which is a nonsense in real life, where a manufacturer can produce many car and many engines at the same time.

    • The black diamond MUST be on the side of the owner.

    If the top diagram is what was taught in the classroom, the teacher has not understood UML very well, and you should consider a different guidance.

    The diagram on the bottom is correct about the side of the diamond, but I'd recommend not to use composition at all. Here the comments to the second model:

    • Composition seems to be too strong. Indeed, I once bought a Chrysler car. But when Chrysler disappeared to become Daimler-Chrysler, my engine and my car were not destroyed. While one can argue the suitability of my example, and discuss the pros and cons of composition, my advice is: use composition, only if this kind of ownership is absolutely needed. In all other cases, prefer a normal association.
    • Also the white diamond ("shared aggregation" in UML-speak or "aggregation" as an ambiguous shortcut), while still popular and used by millions of people, is in reality ambiguous and of no use. Indeed the UML specifications do no longer define any semantic for it (and this since more than 20 years!). Also here, prefer a simple association (unless you have to use it to please your teacher).
    • You may also want to use the official shortcut of * instead of 0..*. The latter is correct, but the former is nicer to read and is officially defined in the UML specifications to be strictly equivalent.