Search code examples
umlassociationsaggregation

Order of aggregation with generalization and specialization


I came across this problem/question when designing a DCD. I have a generalization products and 2 specializations milk and pizza. I have doubts about if should use the aggregation between the order and product or order and pizza/milk.

  • Which one is correct?
  • Which one is preferred if both are correct?

aggregation order and product

aggregation order and milk/pizza

I have simplified the uml to make my question as clear as possible.


Solution

  • Both diagrams are correct, but have a very different meaning:

    • The first diagram says that an order is an aggregation of products, and these products can be more specialized (e.g. milk and pizza). This design decouples the order from specific products: You can therefore easily imagine other product specializations such as Cake without changing the meaning of the order (it is said to be "open for extension").
    • The second diagram says that an order is an aggregate of pizzas and milk, and that pizza an milk are by the way specializations of products. This is much more restrictive: the order is coupled to specific products, so every time your order should deal with a new kind of products that is neither some kind of milk or some kind of pizza (e.g. Cake), the definition of your order would have to be modified.

    Considering the Open-Closed Principle the first diagram is definitely the one to chose. The second one is too cumbersome.

    Remark: by the way, while aggregation is still very popular for historical reasons, the UML specifications don't define its semantic. You could therefore remove the white diamonds and express exactly the same meaning.