Tuesday, July 15, 2008

Composition vs Aggregation. What's Association?


Composition vs Aggregation. How is Association related to them?


Association - it's a structural relationship between two classifiers - classes or use cases. Cyclic association is also possible when the same entity plays two different roles and contains an association describing the relationship between the two roles.


In UML notation an association is depicted by a solid line. For example: suppose we have two entities Employee (EmpNo, Name, EmailId, Salary, DeptNo, Address) and Department (DeptNo, DeptName). Now these two entities are having an association. An association can have any cardinality at any of the two ends depening on the particular relationship.


Aggregation - this is special type of association used for modelling 'Possession' (not Ownership) with the restriction that it's NOT cyclic. That means, an entity can't have an aggregation relationship with itself. This is the reason why an aggregation relationship doesn't form a Graph, instead it forms a Tree.


Aggegation is normally understood as a "has-a" relationship. Here both the entities continue to have their own independent existence. In UML notation, an aggregation is depicted by an unfilled diamond and a solid line. For example: Consider two entities Employee (EmpNo, Name, EmailId, Salary, DeptNo, Address) and Address (FlatNo, PlotNo, StreetName, Area, PinCode). As we might have noticed that every Employee has an Address, but the lifetime of the an Address instance is not governed by any Employee instance. It may continue to exist even after the reclaimation of the Employee instance. Right?


Composition - this is also a special type of association used for modelling 'Ownership'. This is very similar to Aggregation with the only difference that it depicts a Whole-part relationship and the 'part' entity doesn't have its own independent existence. The existence of entity depicting 'whole' giverns the lifetime of the 'part' entity as well. For example: Consider two entities Employee (EmpNo, Name, EmailId, Salary, DeptNo, Address) and EmailId (ID, domain). Now an independent instance of EmailId entity doesn't really make sense unless it's associated with an Employee instance.


Composition is normally understood as a "contains-a" relations. Similar to Aggregation, a Composition also forms a Tree only and not a Graph as it can't be cyclic. In UML notation, a Compsoition is depicted by a filled diamond and a solid line.


Aggregation vs Composition

  • Aggregation represents a "has-a" relationship whereas Composition represents a "contains-a" OR "whole-part" relationship.
  • In case of Aggregation, both the entities will continue to have their independent existence whereas in case of Composition the lifetime of the entity representing 'part' of the "whole-part" is governed by the entity representing 'whole'.
  • Aggregation represents a loose form of relationship as compared to that represented by Composition which depicts a stronger relationship.
  • In UML notation, an Aggregation relationship is depicted by an unfilled diamond and a solid line whereas a Composition relationship is depicted by a filled diamond and a solid line.
  • A 'part' of a Composition relationship can have only one 'whole' at a time i.e., a multiplicity of 1 whereas an Aggregation relationship can have any multiplicity 0..* at the aggregate end.

Liked the article? You may like to Subscribe to this blog for regular updates. You may also like to follow the blog to manage the bookmark easily and to tell the world that you enjoy GeekExplains. You can find the 'Followers' widget in the rightmost sidebar.



Share/Save/Bookmark


9 comments:

Anonymous said...

gr8 blog,,, thanks

Geek said...

Welcome... I hope you'll keep finding it interesting in future as well. Keep visiting/posting!

hariprakash said...

this was very useful and lucid post than the others in the internet... thnx

hariprakash said...

got a better idea about these uml relations... a lot lucid than other posts on the internet... thnx geek

Anonymous said...

really good article... helped me lot.. :-)

Neha said...

cool post! very clear!bomand

Anonymous said...

Very nice and precise explanation.
Geek...Can you add about the dependency also?

Nk said...

I have read many articles on this topic. This is the 1st one that makes sense.

Thank you so much

Anonymous said...

Very good and precise explanation.
Thanks for your efforts and time.