Click here to Skip to main content
Click here to Skip to main content

Understanding Association, Aggregation, and Composition

By , 15 Mar 2012
 

Table of contents

Introduction

In this article, we will try to understand three important concepts: association, aggregation, and composition.

We will also try to understand in what kind of scenarios we need them. These three concepts have really confused a lot of developers and in this article, my attempt would be to present the concepts in a simplified manner with some real world examples.

Extracting real world relationships from a requirement

The whole point of OOP is that your code replicates real world objects, thus making your code readable and maintainable. When we say real world, the real world has relationships. Let’s consider the simple requirement listed below:

  1. Manager is an employee of XYZ limited corporation.
  2. Manager uses a swipe card to enter XYZ premises.
  3. Manager has workers who work under him.
  4. Manager has the responsibility of ensuring that the project is successful.
  5. Manager's salary will be judged based on project success.

If you flesh out the above five point requirement, we can easily visualize four relationships:-

  • Inheritance
  • Aggregation
  • Association
  • Composition

Let’s understand them one by one.

Requirement 1: The IS A relationship

If you look at the first requirement (Manager is an employee of XYZ limited corporation), it’s a parent child relationship or inheritance relationship. The sentence above specifies that Manager is a type of employee, in other words we will have two classes: parent class Employee, and a child class Manager which will inherit from the Employee class.

Note: The scope of this article is only limited to aggregation, association, and composition. We will not discuss inheritance in this article as it is pretty straightforward and I am sure you can get 1000s of articles on the net which will help you in understanding it.

Requirement 2: The Using relationship: Association

Requirement 2 is an interesting requirement (Manager uses a swipe card to enter XYZ premises). In this requirement, the manager object and the swipe card object use each other but they have their own object life time. In other words, they can exist without each other. The most important point in this relationship is that there is no single owner.

The above diagram shows how the SwipeCard class uses the Manager class and the Manager class uses the SwipeCard class. You can also see how we can create objects of the Manager class and SwipeCard class independently and they can have their own object life time.

This relationship is called the “Association” relationship.

Requirement 3: The Using relationship with Parent: Aggregation

The third requirement from our list (Manager has workers who work under him) denotes the same type of relationship like association but with a difference that one of them is an owner. So as per the requirement, the Manager object will own Worker objects.

The child Worker objects can not belong to any other object. For instance, a Worker object cannot belong to a SwipeCard object.

But… the Worker object can have its own life time which is completely disconnected from the Manager object. Looking from a different perspective, it means that if the Manager object is deleted, the Worker object does not die.

This relationship is termed as an “Aggregation” relationship.

Requirements 4 and 5: The Death relationship: Composition

The last two requirements are actually logically one. If you read closely, the requirements are as follows:

  1. Manager has the responsibility of ensuring that the project is successful.
  2. Manager's salary will be judged based on project success.

Below is the conclusion from analyzing the above requirements:

  1. Manager and the project objects are dependent on each other.
  2. The lifetimes of both the objects are the same. In other words, the project will not be successful if the manager is not good, and the manager will not get good increments if the project has issues.

Below is how the class formation will look like. You can also see that when I go to create the project object, it needs the manager object.

This relationship is termed as the composition relationship. In this relationship, both objects are heavily dependent on each other. In other words, if one goes for garbage collection the other also has to be garbage collected, or putting from a different perspective, the lifetime of the objects are the same. That’s why I have put in the heading “Death” relationship.

Putting things together

Below is a visual representation of how the relationships have emerged from the requirements.

The source code

You can download the sample source code for this article.

Summarizing

To avoid confusion henceforth for these three terms, I have put forward a table below which will help us compare them from three angles: owner, lifetime, and child object.

Association Aggregation Composition
Owner No owner Single owner Single owner
Life time Have their own lifetime Have their own lifetime Owner's life time
Child object Child objects all are independent Child objects belong to a single parent Child objects belong to a single parent

Video on Association, Aggregation, and Composition

I have also added a video in case you do not want to read this long article.

Just a note: I have recorded around 500 videos, do have a look at my videos on .NET, OOP, SQL Server, WCF, Silver light , LINQ , VSTS, SharePoint, Design Patterns, UML and a lot more.

Thanks for reading my article.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Shivprasad koirala
Architect http://www.questpond.com
India India
Member

I am a Microsoft MVP for ASP/ASP.NET and currently a CEO of a small
E-learning company in India. We are very much active in making training videos ,
writing books and corporate trainings. Do visit my site for 
.NET, C# , design pattern , WCF , Silverlight
, LINQ , ASP.NET , ADO.NET , Sharepoint , UML , SQL Server  training 
and Interview questions and answers


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionComposition Example - QueriesmemberKausik J. Nag25 Jun '12 - 2:21 
Thanks for the article.
This is regarding the Composition section of your Article. In composition relationship, "Lifetime of Part depends on the Lifetime of Whole". In your example, "Project" and "Manager" is not developing a part-whole relation. Your are passing the the "Manager" object in "Project"'s constructor. You are creating the "Manager" object outside the "Project". So, your "Manager" can still survive even "Project" is destroyed.As far as my concept goes, I agree this is a case of "Aggregation" and also might result to a dependency but not a Composition. Please correct me if I am wrong.
Generalmy vote of 5memberRahul Rajat Singh24 Jun '12 - 15:53 
I spent entire weekend reading all your articles. Gotta say, I loved them. It was time well spent. I spent all my Monday early morning voting, categorizing and bookmarking my favourite ones. Loved the content, became fan of presentation style.
Every now and then say, "What the Elephant." "What the Elephant" gives you freedom. Freedom brings opportunity. Opportunity makes your future.

GeneralMy vote of 5memberLaxmikant_Yadav20 Mar '12 - 22:46 
Simple And Nice Article.
SuggestionUML DiagramsmemberSakshi Smriti19 Mar '12 - 21:22 
UML diagrams should have been put at the top of each of these concepts
GeneralRe: UML DiagramsmemberKeith.Badeau22 Mar '12 - 11:17 
I agree with you; UML would have augmented this nicely. The article and video are a good combo though.
GeneralMy vote of 5memberAnurag Gandhi16 Mar '12 - 3:13 
One of the very good article.
I am already your fan. Smile | :)
QuestionNice articlememberGanesanSenthilvel15 Mar '12 - 6:54 
Nice article
QuestionOn Composition ...memberDoc Lobster15 Mar '12 - 6:44 
Hi,
 
for composition, I would usually go for an example like this:
 
A car is composed of four wheels and an engine.
 
class Car {
    Wheel wheels[4];
    Engine engine;
}
 
In this concept, there is a owner-relationship I don't see in the Project/Manager example. Also see http://en.wikipedia.org/wiki/Object_composition[^]
AnswerRe: On Composition ...memberShivprasad koirala15 Mar '12 - 21:00 
That's definitely more simplified. I have not see the Wheel class. But the wheel class should also refer the car object so that there is tight bonding or else i can attach the wheel to trucks also which probably is more of aggregation.
 
Thanks
My book .NET interview questions with 500 mostly asked questions in .NET world .NET Interview questions and answers

GeneralRe: On Composition ...memberDoc Lobster16 Mar '12 - 2:35 
Yep, thats exactly the difference (Wheels and engine in my example don't reference the car, nethertheless I say this is composition). I wrote this with C++ in mind, so engine and wheels cannot extend their lifecycle beyond that of the car. They also cannot be moved to another car, only copied.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 15 Mar 2012
Article Copyright 2012 by Shivprasad koirala
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid