The knowledge sharing zone - Guruzon.com

Aggregation Vs Composition Vs Association

This page contains the differences between Aggregation, Composition & Association, Along with the necessary example,use case & application of all of them.
reviews

Difference between Association, Aggregation and Composition

Following is the comparison between all the varieties of HAS-A relationships:

  1. Association
  2. Aggregation
  3. Composition
Association Aggregation Composition
Class A uses Class B. Class A contains Class B , Or Class A has instance of Class B. Class A owns Class B.

Example:

  • Employee uses BusService for transportation. 
  • Client-Server model.
  • Computer uses keyboard as input device.

Example:

  • Manager has N Employees for a project. 
  • Team has Players.

Example:

  • Order consists of LineItems / Order owns LineItems!!
  • Body consists of Arm, Head, Legs.
  • BankAccount consists of Balance and TransactionHistory.

An association is used when one object wants another object to perform a service for it. 

Eg. Computer uses keyboard as input device.

An aggregation is used when life of object is independent of container object But still container object owns the aggregated object.

Eg. Team has players, If team dissolve, Player will still exists.

A composition is used where each part may belong to only one whole at a time.

Eg. A line item is part of an order so A line item cannot exist without an order.

Association UML Notation:
Associations are represented by just the line (no diamond).
Aggregation UML Notation:
Aggregations are represented by the line with diamond.
Composition UML Notation:
Compositions are represented by the line with filled diamond.
Life or existance of the associated objects are independent of each other, They just provide some kind of service to each other. Life or existance of the aggregated objects are independent of each other, But one object is playing the role of Owner of the other object. Life or existance of the composite object is dependent on the existance of container object, Existence of composite object is not meaningful without its container object.

What If You are Not Sure What Type of Whole-Part Relationship to Use?

  • Do not get too confuse if you are unable to decide whether a particular whole-part relationship is best described as an aggregation or composition because the distinction is helpful, it is not critical.
  • If you are not even sure whether the relationship is best described as Association or Composition or Aggregation, then model it as an Association. 
  • In fact, there is nothing wrong with modelling all whole-part relationships as simple Associations.
  • You lose a little bit of nuance in the model, But it will make no difference to the resulting IT solution.

You would also like to read:

What is Realization?
What is Aggregation?
What is Generalization?
What is Inheritance?
Difference between IS-A and HAS-A relationships
What is Association?
What is Composition?

Comments :


Nice comparison.. thanks a lot, it's very useful for me..

up vote icon down vote icon

Nice description!! I will never forget this now ,, thanks

up vote icon down vote icon

Very good explanation and comparison. Very very useful . Thanks alot.

up vote icon down vote icon

Nice article. thanks! really helped me get a clear idea of these oop concepts.

up vote icon down vote icon

I think "Class A contains Class B" is Aggregation and "Class A owns Class B" is Composition. Please correct, because people may get badly confused. See: http://en.wikipedia.org/wiki/Class_diagram#Aggregation

up vote icon down vote icon

clarity of differentiatio ...just a doubt on terminology ....composition and aggregation should be interchanged ....what does UML say ...plz revert

up vote icon down vote icon

thanks to help me on this topic.

up vote icon down vote icon

Best Comparison ever at a glance.

up vote icon down vote icon

Best and Shortest Comparison

up vote icon down vote icon
Name :
Email :
Comment :
Verify Text :
capch image, refresh page if not loaded somehow