What is the difference between association, aggregation and composition?

后端 未结 19 1749
伪装坚强ぢ
伪装坚强ぢ 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)

    0 讨论(0)
  • 2020-11-22 02:10

    As others said, an association is a relationship between objects, aggregation and composition are types of association.

    From an implementation point of view, an aggregation is obtained by having a class member by reference. For example, if class A aggregates an object of class B, you'll have something like this (in C++):

    class A {
        B & element;
      // or B * element;
    };
    

    The semantics of aggregation is that when an object A is destroyed, the B object it is storing will still exists. When using composition, you have a stronger relationship, usually by storing the member by value:

    class A {
        B element;
    };
    

    Here, when an A object is destroyed, the B object it contains will be destroyed too. The easiest way to achieve this is by storing the member by value, but you could also use some smart pointer, or delete the member in the destructor:

    class A {
        std::auto_ptr<B> element;
    };
    
    class A {
        B * element;
    
        ~A() {
            delete B;
        }
    };
    

    The important point is that in a composition, the container object owns the contained one, whereas in aggregation, it references it.

    0 讨论(0)
  • 2020-11-22 02:12

    https://www.linkedin.com/pulse/types-relationships-object-oriented-programming-oop-sarah-el-dawody/

    Composition: is a "part-of" relationship.

    for example “engine is part of the car”, “heart is part of the body”.

    Association: is a “has-a” type relationship

    For example, suppose we have two classes then these two classes are said to be “has-a” relationships if both of these entities share each other’s object for some work and at the same time they can exist without each other's dependency or both have their own lifetime.

    The above example showing an association relationship because of both Employee and Manager class using the object of each other and both their own independent life cycle.

    Aggregation: is based is on "has-a" relationship and it's is \\a special form of association

    for example, “Student” and “address”. Each student must have an address so the relationship between Student class and Address class will be “Has-A” type relationship but vice versa is not true.

    0 讨论(0)
  • 2020-11-22 02:13

    Composition: This is where once you destroy an object (School), another object (Classrooms) which is bound to it would get destroyed too. Both of them can't exist independently.

    Aggregation: This is sorta the exact opposite of the above (Composition) association where once you kill an object (Company), the other object (Employees) which is bound to it can exist on its own.

    Association.
    Composition and Aggregation are the two forms of association.

    0 讨论(0)
  • 2020-11-22 02:13
        Simple rules:
        A "owns" B = Composition : B has no meaning or purpose in the system 
        without A
        A "uses" B = Aggregation : B exists independently (conceptually) from A
        A "belongs/Have" B= Association; And B exists just have a relation
        Example 1:
    
        A Company is an aggregation of Employees.
        A Company is a composition of Accounts. When a Company ceases to do 
        business its Accounts cease to exist but its People continue to exist. 
        Employees have association relationship with each other.
    
        Example 2: (very simplified)
        A Text Editor owns a Buffer (composition). A Text Editor uses a File 
        (aggregation). When the Text Editor is closed,
        the Buffer is destroyed but the File itself is not destroyed.
    
    0 讨论(0)
  • 2020-11-22 02:16

    Dependency (references)
    It means there is no conceptual link between two objects. e.g. EnrollmentService object references Student & Course objects (as method parameters or return types)

    public class EnrollmentService {
        public void enroll(Student s, Course c){}
    }
    

    Association (has-a)
    It means there is almost always a link between objects (they are associated). Order object has a Customer object

    public class Order {
        private Customer customer
    }
    

    Aggregation (has-a + whole-part)
    Special kind of association where there is whole-part relation between two objects. they might live without each other though.

    public class PlayList {
        private List<Song> songs;
    }
    

    OR

    public class Computer {
        private Monitor monitor;
    }
    

    Note: the trickiest part is to distinguish aggregation from normal association. Honestly, I think this is open to different interpretations.

    Composition (has-a + whole-part + ownership)
    Special kind of aggregation. An Apartment is composed of some Rooms. A Room cannot exist without an Apartment. when an apartment is deleted, all associated rooms are deleted as well.

    public class Apartment{
        private Room bedroom;
        public Apartment() {
           bedroom = new Room();
        }
    }
    
    0 讨论(0)
提交回复
热议问题