In today's discussion, we will discuss Aggregation and Composition in detail using C#. So, let's get started. Let me go ahead and create simple console App for demo purpose.
- namespace CSharpStuffs
- {
- public class Patient
- {
- public string PatientName { get; set; }
- public string AdmissionDate { get; set; }
- public string ReleaseDate { get; set; }
- public DoctorInfo DoctorInfo { get; set; }
- public LifeInsurance Insurance { get; set; }
- }
- }
- namespace CSharpStuffs
- {
- public class LifeInsurance
- {
- public string PolicyName;
- public string CompanyName;
- }
- }
- namespace CSharpStuffs
- {
- public class Patient
- {
- public string PatientName { get; set; }
- public string AdmissionDate { get; set; }
- public string ReleaseDate { get; set; }
- public DoctorInfo DoctorInfo { get; set; }
- public LifeInsurance Insurance { get; set; }
- public Patient(string patientName,string admissionDate,string releaseDate,string doctorName,string doctorSpeciality, string workingAt)
- {
- PatientName = patientName;
- AdmissionDate = admissionDate;
- ReleaseDate = releaseDate;
- //Composition comes into picture now
- //It also means when we construct Patient, we also constructed DoctorInfo
- //Hence, scope of Doctorinfo is dependent on Patient
- DoctorInfo = new DoctorInfo
- {
- DoctorName = doctorName,
- DoctorSpeciality = doctorSpeciality,
- WorkingAt = workingAt
- };
- }
- //Overrideenn ToString() for convience
- public override string ToString()
- {
- string result = PatientName + " " +
- AdmissionDate + " " +
- ReleaseDate + " ";
- //Filling Other objects
- if (DoctorInfo != null)
- {
- result += DoctorInfo.DoctorName + " "
- + DoctorInfo.DoctorSpeciality + " "
- + DoctorInfo.WorkingAt;
- }
- if (Insurance != null)
- {
- result += " "+ Insurance.PolicyName + " "
- + Insurance.CompanyName;
- }
- return result;
- }
- }
- }
Now, main program looks like the following:
- using System;
- namespace CSharpStuffs
- {
- class Program
- {
- static void Main(string[] args)
- {
- Patient patient = new Patient("Tim","12-08-2015","20-08-2015","Cook","Physician","Fortis");
- Console.WriteLine(patient.ToString());
- Console.ReadLine();
- }
- }
- }

However, I can modify the main program to add more info to this via aggregation as shown below.
- using System;
- namespace CSharpStuffs
- {
- class Program
- {
- static void Main(string[] args)
- {
- Patient patient = new Patient("Tim","12-08-2015","20-08-2015","Cook","Physician","Fortis");
- Console.WriteLine(patient.ToString());
- //Now, we can add insurance by aggregation
- patient.Insurance = new LifeInsurance
- {
- CompanyName = "HDFC",
- PolicyName = "LIFE-AXA"
- };
- Console.WriteLine(patient.ToString());
- Console.ReadLine();
- }
- }
- }
Therefore, in a nutshell, Aggregation is the stuff which can be added at later point of time to the object, but composition gets constructed with object creation itself.
Thanks!

Raja TPosted Nov 14, 2015, 12:31 PM
Thanks for sharing...
Jasminder SinghPosted Nov 14, 2015, 11:49 AM
Hello @Rahul, I have a question regarding this concept. You have created DoctorInfo inside the Patient constructor and LifeInsurance object later on. The point is we could have also created the LifeInsurance object at the same time when the DoctorInfo was created OR create the DoctorInfo like LifeInsurance was created. Then how does this compares composition and aggregation. It seems like (not related to ur example, just a generic understanding), to implement this concept, we have to do more of logical thinking of when we should create the instance, rather then technial stuff. Correct me if I am missing any point here.