Have a look into the following two concepts (Generalization, Specialization), before dig into our topic (Association, Aggregation and Composition).
→Generalization
Generalization describes is-a relationship which represent a hierarchy between classes of objects. E.g.:- an "animal" is a generalization of "dog", "cat", "buffalo" and many others. Fruit is the generalization of Mango.
→Specialization
Specialization means an object can inherit the common state and behavior of a generic object. However, each object needs to define its own special and particular state and behavior. Specialization means to subclass. Animal is the generalization and pet is the specialization, indicating that a pet is a special kind of animal.
Association
Define the relationship between the classes. “IS-A” relationship is exists in Association. In association all objects have their own life cycle and there is now owner.
Example is Person and Employee class relationship. Employee is a Person. So we can say that there is an Association is exists in between Person and Employee classes. Another exmple is a Doctor and a Patient classes. Multiple patients can associate with single docotor and single patient can assoicate with multiple doctors, but there is no ownership between objects and both have their own life cycle. Both can create and delete independently.
Aggregation
Aggregation is a specialize form of Assocaition where all object have thier own life cycle but ther is an ownership like parent and child. Child object can not belong to another parent object at the same time. When one class object has an object of another class, when one class is a part of another class, or one class containing the other class (containment relationship), it is called Aggregation. Describe “HAS-A” relationship between objects of the classes.
Example: University has a chancellor. So we can say that University Aggregate chancellor or University has a chancellor. Another example like a single employee can not belong to multiple companies, but if Company deleted, Employee object will not destroy.
Composition
Composition is again specialize form of Aggregation. It is strong type of Aggregation. Here the parent child have coincident lifetimes. Child object does have it's own lifecycle and if parent object gets deleted, then all of its child objects will also deleted.
Like the above example that university has a Chancellor as well as university also have faculties.
In this case we can say that University Compose the Faculties.
Difference b/w the Association, Aggregation and Composition
Association is the more general term that define the relationship between two classes, whereas the aggregation and composition are relatively special.
Association is a (*a*) relationship between two classes, where one class use another. But aggregation describes a special type of an association. Aggregation is the (*the*) relationship between two classes. When object of one class has a (*has*) object of another, if second is a part of first (containment relationship) then we called that there is an aggregation between two classes. Unlike association, aggregation always insists a direction.
In this case we can say that University aggregate Chancellor or University has a (*has-a*) Chancellor. But even without a Chancellor a University can exists.
But a University cannot exist without Faculties, the life time of a University attached with the life time of its Faculty (or Faculties). If Faculties are disposed the University will not exist or wise versa. In that case we called that University is composed of Faculties. So that composition can be recognized as a special type of an aggregation.
So in summary, we can say that aggregation is a special kind of an association and composition is a special kind of an aggregation.
Aggregation Examples:
Example A:
Room and Equipment’s in it (Like chair, Table, Bed etc.).
Example B:
Garden in its plants.
Example C:
Departments and Teachers or Departments and Employee.
Composition Examples:
Example A:
Let’s take another example of relationship between Questions and options. Single questions can have multiple options and option cannot belong to multiple questions. If we delete questions options will be automatically deleted.
Example B:
Ali and his Body parts.
Example C:
University and Departments.
Examples of Aggregation and Composition in Code
A. Composition
B. Aggregation
class Program
{
static void Main(string[] args)
{
University _University = new University();
//
Composition
//Comment
un comment this line of code to see the output/result
//
=======> _University = null;
//We institate
the Department in University Parameter less constructor.
//
Because of this when we setting null to the university object of parameter less
constructor, its automaticly set null to the Department object
//Department
is fully depend on the University. It is called Compositon. (University compose
the Department).
//It
also make sense in real world scenerio, there is no department exists if there
is no university.
Console.WriteLine("");
Console.ReadKey();
}
}
public class University
{
public Department objDepartment = null;
public University()
{
objDepartment = new Department();
}
}
public class Department
{
public string DepartmentName;
public int TotalNumberOfFaculity;
}
B. Aggregation
class Program
{
static void Main(string[] args)
{
List<Employee> objEmployees = new List<Employee>();
objEmployees.Add(new Employee { EmpID = 1, Name =
"Ghazanfar" });
objEmployees.Add(new Employee { EmpID = 2, Name =
"Ali" });
objEmployees.Add(new Employee { EmpID = 3, Name =
"Ghazi" });
Department objDepartment = new Department(employees);
//Aggregation
//Comment
unComment this line of code to see the output/result
//======> ObjDepartment = null;
//Comment
unComment this line of code to see the output/result
//=======> objEmployees.First();
//Setting
the null to a Department obj dept not effecting the employee existance
//Employee
is still exist (or it exists as a person or human being) even though the
Department is not exists. It is correct in this scenerio, but in real world
// this
example is wrong because when the department is not exists the employee
//
having no designation as a Employee post.
Console.WriteLine("" + employees.First());
Console.ReadKey();
}
}
public class Department
{
List<Employee> employees;
public Department(List<Employee> employees)
{
this.employees = employees;
}
}
public class Employee
{
private int _empID;
public int EmpID
{
get { return _empID; }
set { _empID = value; }
}
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
}
Another code examples ofAssociation, Aggregation and Composition
A. Association
public class Teacher
{
public string Name { get; set; }
}
public class Student
{
public string Name { get; set; }
public List<Teacher> Teachers { get; set; } = new List<Teacher>();
}
public class Program
{
public static void Main(string[] args)
{
Teacher teacher1 = new Teacher { Name = "Mr. Smith" };
Teacher teacher2 = new Teacher { Name = "Ms. Johnson" };
Student student = new Student { Name = "John Doe" };
student.Teachers.Add(teacher1);
student.Teachers.Add(teacher2);
Console.WriteLine($"{student.Name} is taught by {string.Join(", ", student.Teachers.Select(t => t.Name))}");
}
}
Aggregation
public class Book
{
public string Title { get; set; }
}
public class Library
{
public string Name { get; set; }
public List<Book> Books { get; set; } = new List<Book>();
}
public class Program
{
public static void Main(string[] args)
{
Book book1 = new Book { Title = "C# Programming" };
Book book2 = new Book { Title = "Data Structures" };
Library library = new Library { Name = "City Library" };
library.Books.Add(book1);
library.Books.Add(book2);
Console.WriteLine($"{library.Name} contains books: {string.Join(", ", library.Books.Select(b => b.Title))}");
}
}
Composition
public class Room
{
public string Name { get; set; }
public Room(string name)
{
Name = name;
}
}
public class House
{
public string Address { get; set; }
public List<Room> Rooms { get; set; }
public House(string address)
{
Address = address;
Rooms = new List<Room>();
}
public void AddRoom(string roomName)
{
Rooms.Add(new Room(roomName));
}
}
public class Program
{
public static void Main(string[] args)
{
House house = new House("123 Main St");
house.AddRoom("Living Room");
house.AddRoom("Bedroom");
Console.WriteLine($"House at {house.Address} has rooms: {string.Join(", ", house.Rooms.Select(r => r.Name))}");
}
}
(Practice Makes a Man Perfect)
No comments:
Post a Comment