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;
- }
- }
Once, the class gets created, It's time to feed the value via constructor. Hence, the following is the final code for the same.
- 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;
-
-
-
-
- DoctorInfo = new DoctorInfo
- {
- DoctorName = doctorName,
- DoctorSpeciality = doctorSpeciality,
- WorkingAt = workingAt
- };
-
- }
-
-
- public override string ToString()
- {
- string result = PatientName + " " +
- AdmissionDate + " " +
- ReleaseDate + " ";
-
- if (DoctorInfo != null)
- {
- result += DoctorInfo.DoctorName + " "
- + DoctorInfo.DoctorSpeciality + " "
- + DoctorInfo.WorkingAt;
- }
- if (Insurance != null)
- {
- result += " "+ Insurance.PolicyName + " "
- + Insurance.CompanyName;
- }
- return result;
- }
- }
- }
You will also notice that here, I have used Composition concept.
Composition is tied with actual object which is patient here and hence
DoctorInfo also got constructed.
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();
- }
- }
- }
And, when I run the program, it will produce the following output.
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());
-
-
-
- patient.Insurance = new LifeInsurance
- {
- CompanyName = "HDFC",
- PolicyName = "LIFE-AXA"
- };
- Console.WriteLine(patient.ToString());
- Console.ReadLine();
- }
- }
- }
With the above change in place, it will display the following info.
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!