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
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:
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:
- public class Mobile
- {
- public OperatingSys objOS;
- public Mobile(OperatingSys objOperatingSys)
- {
- this.objOS = objOperatingSys;
- }
- public string MobileName { get; set; }
- public string OperatingSystem { get; set; }
- public string GetMobileDetails()
- {
- return objOS.OSName() + " Used by " + MobileName;
- }
- }
-
- public class OperatingSys
- {
- public string OS { get; set; }
- public string OSName()
- {
- return "This is Android OS being";
- }
- }
-
- class Program
- {
- static void Main(string[] args)
- {
- OperatingSys objOperatingSys = new OperatingSys();
- Mobile objMobile = new Mobile(objOperatingSys);
- objMobile.MobileName = "Samsung";
- Console.WriteLine(objMobile.GetMobileDetails());
- Console.ReadLine();
- }
- }
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.
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.
- public class Mobile
- {
- public string MobileName { get; set; }
- public string DisplayType { get; set; }
-
- public Mobile()
- {
-
- }
-
- public string GetMobileDetails()
- {
- return this.DisplayType + " Used by " + this.MobileName;
- }
- }
-
- public class DisplayScreen
- {
- public static string ScreenType()
- {
- return "This LED screen display";
- }
- }
-
- class Program
- {
- static void Main(string[] args)
- {
-
- Mobile objMobile = new Mobile();
- objMobile.MobileName = "Samsung Galaxy Mobile";
- objMobile.DisplayType = DisplayScreen.ScreenType();
- Console.WriteLine(objMobile.GetMobileDetails());
- Console.ReadLine();
- }
- }
Output
Note: It might be one of the questions in an interview.