umlclass-diagram

Why using association?


I am currently learning about UML class diagrams. I have learned that an association just shows that "There is a relationship between these classes". Meanwhile, composition and aggregation show that "The relationship of these classes must like this", which is adding specific information to the association. I don't know why we keep using association. Isn't it more specific to use composition or aggregation instead ? If no, in what scenario we should use association over the other 2 ?

Hope someone can explain this for me.


Solution

  • There are many cases where you would use an association that is neither a composition or an aggregation.

    Consider a many-to-many Likes association between Person and Food. This is neither composition or aggregation.

    Having said that, the aggregation semantics (aggregate, composite or none) are often contextual and/or debatable. For example, consider a ParentChild association between a Person class and itself. This is not a composition, because a child object needs to be able to exist independently of its parent object(s). But is it an aggregate association? The answer may depend on whether you are modelling families, or family trees, or forests of related family trees. (Noting that even a family tree can be a DAG.)