What is the difference between association, aggregation and composition?

后端 未结 19 1832
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 01:44

What is the difference between association, aggregation, and composition? Please explain in terms of implementation.

19条回答
  •  囚心锁ツ
    2020-11-22 02:09

    It's amazing how much confusion exists about the distinction between the three relationship concepts association, aggregation and composition.

    Notice that the terms aggregation and composition have been used in the C++ community, probably for some time before they have been defined as special cases of association in UML Class Diagrams.

    The main problem is the widespread and ongoing misunderstanding (even among expert software developers) that the concept of composition implies a life-cycle dependency between the whole and its parts such that the parts cannot exist without the whole, ignoring the fact that there are also cases of part-whole-associations with non-shareable parts where the parts can be detached from, and survive the destruction of, the whole.

    As far as I can see, this confusion has two roots:

    1. In the C++ community, the term "aggregation" was used in the sense of a class defining an attribute for referencing objects of another independent class (see, e.g., [1]), which is the sense of association in UML Class Diagrams. The term "composition" was used for classes that define component objects for their objects, such that on destruction of the composite object, these component objects are being destroyed as well.

    2. In UML Class Diagrams, both "aggregation" and "composition" have been defined as special cases of associations representing part-whole relationships (which have been discussed in philosophy for a long time). In their definitions, the distinction between an "aggregation" and a "composition" is based on the fact if it allows sharing a part between two or more wholes. They define "compositions" as having non-shareable (exclusive) parts, while "aggregations" may share their parts. In addition they say something like the following: very often, but not in all cases, compositions come with a life-cycle dependency between the whole and its parts such that the parts cannot exist without the whole.

    Thus, while UML has put the terms "aggregation" and "composition" in the right context (of part-whole relationships), they have not managed to define them in a clear and unambiguous manner, capturing the intuitions of developers. However, this is not surprising because there are so many different properties (and implementation nuances) these relationships can have, and developers do not agree on how to implement them.

    See also my extended answer to the SO question of Apr 2009 listed below.

    And the property that was assumed to define "composition" between OOP objects in the C++ community (and this belief is still widely held): the run-time life-cycle dependency between the two related objects (the composite and its component), is not really characteristic for "composition" because we can have such dependencies due to referential integrity also in other types of associations.

    For instance, the following code pattern for "composition" was proposed in an SO answer:

    final class Car {    
      private final Engine engine;
    
      Car(EngineSpecs specs) {
        engine = new Engine(specs);
      }
    
      void move() {
        engine.work();
      }
    }
    

    The respondent claimed that it would be characteristic for "composition" that no other class could reference/know the component. However, this is certainly not true for all possible cases of "composition". In particular, in the case of a car's engine, the maker of the car, possibly implemented with the help of another class, may have to reference the engine for being able to contact the car's owner whenever there is an issue with it.

    [1] http://www.learncpp.com/cpp-tutorial/103-aggregation/

    Appendix - Incomplete list of repeatedly asked questions about composition versus aggregation on StackOverflow

    [Apr 2009]
    Aggregation versus Composition [closed as primarily opinion-based by]
    [Apr 2009]
    What is the difference between Composition and Association relationship?
    [May 2009]
    Difference between association, aggregation and composition
    [May 2009]
    What is the difference between composition and aggregation? [duplicate]
    [Oct 2009]
    What is the difference between aggregation, composition and dependency? [marked as duplicate]
    [Nov 2010]
    Association vs. Aggregation [marked as duplicate]
    [Aug 2012]
    Implementation difference between Aggregation and Composition in Java
    [Feb 2015]
    UML - association or aggregation (simple code snippets)

提交回复
热议问题