Aggregation Vs Composition: A Simple Practical Approach

Before proceeding I'd like to share one of the possible situations that you may encounter in your daily need or maybe in an interview. Let's dive into it in a practical way.

The question is, "What is the difference between composition and aggregation?" In this article I'll provide my understanding of composition and aggregation.

The following image is fictional and it may be one of the possible scenarios with warrior's fight.

Aggregation Vs Composition

Aggregation

Aggregation means “The act of gathering something together”.

As inheritance gives us an "is-a" and composition gives us a "part-of" whereas aggregation gives us a "has-a" relationship.

For example, a mobile “has an” operating system. Aggregation doesn't manage the lifetime of other objects. For an example, mobile uses (has-an) operating system that may be of a different service provider. like Android, iOS and Windows. Here Aggregation theory says that a mobile has an operating system of a specific service provider.

Aggregation diagram is shown as a UML diagram with unfilled diamond notation as depicted below:

Aggregation

The preceding diagram states that a Mobile class may utilize an operating system from a different service provider. A mobile is not directly dependent on an operating system class. Both are independent entities. For example a Samsung is a mobile provider whereas an Android is a Google Product.

A code snippet shown below that is an OperatingSys object is injecting into the Mobile class at the constructor level. The creation of the OperatingSys class is entirely independent from Mobile Creation. Kindly have a look at the code segment as shown below:

  1. public class Mobile  
  2. {  
  3.     public OperatingSys objOS;  
  4.     public Mobile(OperatingSys objOperatingSys)  
  5.     {  
  6.        this.objOS = objOperatingSys;  
  7.     }  
  8.     public string MobileName { getset; }  
  9.     public string OperatingSystem { getset; }  
  10.     public string GetMobileDetails()  
  11.     {  
  12.         return objOS.OSName() + " Used by " + MobileName;  
  13.     }  
  14. }     
  15.   
  16. public class OperatingSys  
  17. {  
  18.     public string OS { getset; }  
  19.     public string OSName()  
  20.     {  
  21.         return "This is Android OS being";  
  22.     }  
  23. }  
  24.   
  25. class Program  
  26. {  
  27.     static void Main(string[] args)  
  28.     {  
  29.         OperatingSys objOperatingSys = new OperatingSys();  
  30.         Mobile objMobile = new Mobile(objOperatingSys);  
  31.         objMobile.MobileName = "Samsung";  
  32.         Console.WriteLine(objMobile.GetMobileDetails());  
  33.         Console.ReadLine();  
  34.     }  
  35. }  
Composition

Composition means mixture of ingredients.

Composition gives us a "part-of" relationship or a mixture of ingredients. Composition is a higher degree of association than aggregation. Composition derives the management of the objects and its lifecycle. In case of composition the lifetime of the owned object is the responsibility of the owner object. Composition is shown on a UML diagram as a filled diamond as depicted below in the screen.

Composition

As you can see in the example code snippet below, Mobile manages the lifetime of DisplayScreen, since the DisplayType object depends on the Mobile object. If Mobile is garbage collected, the DisplayType object is also garbage collected.
  1. public class Mobile  
  2. {  
  3.     public string MobileName { getset; }  
  4.     public string DisplayType { getset; }  
  5.      
  6.     public Mobile()  
  7.     {  
  8.   
  9.     }          
  10.       
  11.     public string GetMobileDetails()  
  12.     {  
  13.         return this.DisplayType + " Used by " + this.MobileName;  
  14.     }  
  15. }        
  16.   
  17. public class DisplayScreen  
  18. {  
  19.     public static string ScreenType()  
  20.     {  
  21.         return "This LED screen display";  
  22.     }  
  23. }     
  24.   
  25. class Program  
  26. {  
  27.     static void Main(string[] args)  
  28.     {  
  29.        // OperatingSys objOperatingSys = new OperatingSys();  
  30.         Mobile objMobile = new Mobile();  
  31.         objMobile.MobileName = "Samsung Galaxy Mobile";  
  32.         objMobile.DisplayType = DisplayScreen.ScreenType();  
  33.         Console.WriteLine(objMobile.GetMobileDetails());  
  34.         Console.ReadLine();  
  35.     }  
  36. }  
Output

Output

Note: It might be one of the questions in an interview.