What is the difference between association, aggregation and composition?

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

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

19条回答
  •  半阙折子戏
    2020-11-22 01:59

    I know this question is tagged as C# but the concepts are pretty generic questions like this redirect here. So I am going to provide my point of view here (a bit biased from java point of view where I am more comfortable).

    When we think of Object-oriented nature we always think of Objects, class (objects blueprints) and the relationship between them. Objects are related and interact with each other via methods. In other words the object of one class may use services/methods provided by the object of another class. This kind of relationship is termed as association..

    Aggregation and Composition are subsets of association meaning they are specific cases of association.

    enter image description here

    • In both aggregation and composition object of one class "owns" object of another class.
    • But there is a subtle difference. In Composition the object of class that is owned by the object of it's owning class cannot live on it's own(Also called "death relationship"). It will always live as a part of it's owning object where as in Aggregation the dependent object is standalone and can exist even if the object of owning class is dead.
    • So in composition if owning object is garbage collected the owned object will also be which is not the case in aggregation.

    Confused?

    Composition Example : Consider the example of a Car and an engine that is very specific to that car (meaning it cannot be used in any other car). This type of relationship between Car and SpecificEngine class is called Composition. An object of the Car class cannot exist without an object of SpecificEngine class and object of SpecificEngine has no significance without Car class. To put in simple words Car class solely "owns" the SpecificEngine class.

    Aggregation Example : Now consider class Car and class Wheel. Car needs a Wheel object to function. Meaning the Car object owns the Wheel object but we cannot say the Wheel object has no significance without the Car Object. It can very well be used in a Bike, Truck or different Cars Object.

    Summing it up -

    To sum it up association is a very generic term used to represent when a class uses the functionalities provided by another class. We say it's composition if one parent class object owns another child class object and that child class object cannot meaningfully exist without the parent class object. If it can then it is called Aggregation.

    More details here. I am the author of http://opensourceforgeeks.blogspot.in and have added a link above to the relevant post for more context.

提交回复
热议问题