Composition Vs Aggregation

Sudhakarj21
Posted by in OOPS category on for Beginner level | Views : 37790 red flag
Rating: 5 out of 5  
 1 vote(s)

Composition and Aggregation are the most confusing terms in OOPS. These 2 specifies relations between classes.
Introduction

Composition and Aggregation is most commonly used relations in class diagram. But many people gets confused with these 2 and some times even confused to implement the relations. The most simple thing about these is about the relations and life time of the object.
Composition

Lets take a simple example of Cycle. Cycle should contain 2 tyres and if we convert it to logical model below is the UML diagram




The above UML describes that Cycle contains 2 Tyres and the life time of each Tyre is maintained by Cycle. It is like "Tyre is Part Of Cycle". Let us see how it can be implemented in C#.

public class Tyre
{
. . .
}

public class Cycle

{

Tyre[] tires = new Tyre[]{new Tyre(), new Tyre()};

.......

}
Aggregation

On the other hand aggregation relation just represents reference between classes. For example consider Employee and contact information. Blow diagram represents the UML



The above UML describes Aggregation relation which mentions that Employee refers to Address. And life time of Address is not managed by Employee. It is like "Employee has a Address". Let us see how it can be implemented in C#.

public class Address
{
. . .
}

public class Employee

{

private Address address;

public Employee(Address address)

{

this.address = address;

}

. . .

}

Conclusion

Composition and Aggregation are important part of classes designs but most of the OOPS concepts are rounded with fundamentals only which doesn't describes about these relations.

Page copy protected against web site content infringement by Copyscape

About the Author

Sudhakarj21
Full Name: Sudhakar Kottapalli
Member Level: Bronze
Member Status: Member
Member Since: 10/5/2009 7:05:50 AM
Country:



Login to vote for this post.

Comments or Responses

Posted by: Vishvvas on: 7/20/2011 | Points: 25
Very good article. There are few apprehensions about example of composition, from the perspective of cycle manufacturer its aggregreations as he can manufacture and sell the tyres seprately but from the perspective of end user its a composition as without a tyre , he can't ride/ use the cycle. A good example could be A cirlce composed of Points., An "Order" composed of "Orderline". For aggregration, the examples could be Window"--- "Shapes"', "StudentClass", "StudentGroup" ---"Student". The example of Employee and Address looks like simple association than aggregration as Address doen't give rise to Employee rather you can have entity "EmployeeDetails'" and "EmployeeAddress" as an example of aggregration.
Hope this helps.
Posted by: Chiragkanzariya on: 11/22/2012 | Points: 25
Nice Article

You can also check Basic concept of OOP here http://chiragkanzariya.blogspot.in/2012/11/oops-concept-with-real-world-example.html

Login to post response

Comment using Facebook(Author doesn't get notification)