Proper uses of Association Aggregation and Composition


In this article, I will explain the definition and proper use of terms Association, Aggregation and Composition of the OOPs with suitable C# code example.This article helps us to concept and process of Association Aggregation and Composition in C#.

When we tried to define the relationship between objects, we used terms i.e.Association, Aggregation and Composition. These all are related to each other i.e. Aggregation and Composition both are specialized form of Association. Composition is again specialize form of Aggregation.
You can keep these term in this manner.

Association ==> Aggregation ==> Composition

Association


It is a relationship where all object have their own lifecycle and there is no owner. Let's take an example of Teacher and Student. Multiple students can associate with single teacher and single student can associate with multiple teachers but there is no ownership between the objects and both have their own lifecycle. Both can create and delete independently.

Ex:


public class Student
{
public string Name;
public Student(string StudentName)
{
Name = StudentName;
}
}

public class Department
{
public string Name;
public Department(string DepartName)
{
Name = DepartName;
}
}

public class Course
{
Student StdObject;
Department DeptObject;
public string CourseDetail;
public Course(Student StudentObject, Department DepartmentObject, string CourseName)
{
StdObject = StudentObject;
DeptObject = DepartmentObject;
CourseDetail = CourseName;
}
}

class TestClass
{
static void Main(string[] args)
{
List StudentList = new List();
List DepartmentList = new List();

//Adding Student
StudentList.Add(new Student("Devesh"));
StudentList.Add(new Student("Ajay"));
StudentList.Add(new Student("Utkarsh"));


//Adding Department
DepartmentList.Add(new Department("Math Dept"));
DepartmentList.Add(new Department("Physics Dept"));
DepartmentList.Add(new Department("Computer Science dept"));


Course Course1 = new Course(StudentList[0], DepartmentList[0], "Course1");
Course Course2 = new Course(StudentList[0], DepartmentList[2], "Course2");
Course Course3 = new Course(StudentList[1], DepartmentList[1], "Course3");
Course Course4 = new Course(StudentList[2], DepartmentList[0], "Course4");

//Now deleting Course Object
Course1 = null;
Course2 = null;

//But still the scope of Student and Department object will not destroy.
}
}


Now in case if I delete the object of class the Student and Department class will remain exists, because they have their own life cycle.

Multiple students can associate with a single Department and single student can associate with multiple Departments, but there is no ownership between the objects and both have their own lifecycle. Both can create and delete independently.

Aggregation


It is a specialize form of Association where all object have their own lifecycle but there is ownership and child object can not belongs to another parent object. Let's take an example of Company and Employee. A single teacher can not belongs to multiple departments, but if we delete the Company, Employee object will not destroy. We can think about "has-a" relationship.

Ex:


public class Employee
{
public string Name;
public Employee(string EmployeeName)
{
Name = EmployeeName;
}
}

public class Company
{
public string CompanyName;
public List EmployeeLIst;
public Company()
{
EmployeeLIst = new List();
}
}
class TestClass
{
static void Main(string[] args)
{
Company CompanyObject = new Company();
CompanyObject.EmployeeLIst.Add(new Employee("Devesh Kumar"));
CompanyObject.EmployeeLIst.Add(new Employee("Utkarsh Agarwal"));
CompanyObject.EmployeeLIst.Add(new Employee("Ajit Kumar"));

//Now I am deleting the CompanyObject object.
CompanyObject = null;
//The Employee Object will also delete.
}
}


A single Employee can not belong to multiple Companies (legally!! ), but if we delete the Company, Employee object will not destroy.


Composition


It is again specialize form of Aggregation and we can call this as a "death" relationship. It is a strong type of Aggregation. Child object does not have their lifecycle and if parent object deletes all child object will also be deleted. Let's take again an example of relationship between House and rooms. House can contain multiple rooms there is no independent life of room and any room can not belongs to two different house if we delete the house room will automatically delete. Let's take another example relationship between Questions and options. Single questions can have multiple options and option can not belong to multiple questions. If we delete questions options will automatically delete.

Ex:

public class Room
{
string RoomType;
bool IsFurnished;
public Room(string RType, bool Furnished)
{
RoomType = RType;
IsFurnished = Furnished;
}
}

public class House
{
string HouseName;
List RoomList;
public House(string Name)
{
HouseName = Name;
RoomList = new List();
}
public void CreateRoom(string RoomType, bool Furnished)
{
RoomList.Add(new Room(RoomType, Furnished));
}
}
class TestClass
{
static void Main(string[] args)
{
House MyHouse = new House("Mr. Sharma's House");
MyHouse.CreateRoom("Bed Room", true);
MyHouse.CreateRoom("Dining Room", true);
MyHouse.CreateRoom("Kitchen", true);

//Here we are not explicitly creating the object of Room class.
}
}


House can contain multiple rooms there is no independent life for room and any room can not belong to two different houses. If we delete the house room will also be automatically deleted.

Note:
To elaborate more on these terms on the definition points of view, I took some information available on net.


Comments

Author: Utkarsh Agrawal20 Oct 2010 Member Level: Bronze   Points : 1

Good & Detailed explanation for a topic that seems so confusing... Good Work dude... :-)

Author: Utkarsh Agrawal20 Oct 2010 Member Level: Bronze   Points : 1

Good & Detailed explanation for a topic that seems so confusing... Good Work dude... :-)

Guest Author: sanjeev30 Apr 2013

There is difference between what you are saying in aggregation definition i.e. "if we delete the Company, Employee object will not destroy." and in code example "Now I am deleting the CompanyObject object -- The Employee Object will also delete."

Can you please make it clear and clarify your definition?



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: