Aggregation and Association and Composition and Its differences with examples

 Aggregation, association, and composition are object-oriented programming concepts that define relationships between classes or objects. Here are their definitions and differences:

1. Aggregation:

Aggregation is a relationship where one object has a reference to another object, but the referenced object can exist independently of the object that references it. The object that references the other is known as the container or owner, while the referenced object is known as the contained or member object.

Here are a few examples of aggregation in C#:

Example - 1 :

A university has many departments, and each department has many students. In this case, the Department class is the container, and the Student class is the contained object.

Here's an example code:

public class Department
{
    public List<Student> Students { get; set; }
}

public class Student
{
    public string Name { get; set; }
}

Example - 2 :

A car has an engine, and the engine can be swapped with another engine. Here, the Car class is the container, and the Engine class is the contained object.

Here's an example code:

public class Car
{
    public Engine Engine { get; set; }
    // Other properties and methods of the Car class
}

public class Engine
{
    public string Model { get; set; }
    // Other properties and methods of the Engine class
}

Example - 3 :

A house has many rooms, and each room has a different function (e.g., bedroom, kitchen, bathroom). Here, the House class is the container, and the Room class is the contained object.

Here's an example code:

public class House
{
    public List<Room> Rooms { get; set; }
    // Other properties and methods of the House class
}

public class Room
{
    public string Name { get; set; }
    public string Function { get; set; }
}

Summary :

In all of these examples, the container class (e.g., Department, Car, House) has a reference to the contained class (e.g., Student, Engine, Room), but the contained class can exist independently of the container class.


2. Association:

Association is a relationship between two or more objects where they interact with each other. In association, the participating objects may or may not have a reference to each other, and they may or may not depend on each other.

Here are a few examples of association in C#:

Example - 1 :

A person can use a calculator to perform arithmetic operations. In this case, the Person and Calculator classes are associated with each other. 

Here's an example code:

public class Person
{
    public string Name { get; set; }
    public void UseCalculator(Calculator calculator)
    {
        // Use the calculator to perform arithmetic operations
    }
}

public class Calculator
{
    // Properties and methods of the Calculator class
}

Example - 2 :

An online store has many customers, and each customer can place orders. In this case, the Customer and Order classes are associated with each other.

Here's an example code:

public class Customer
{
    public string Name { get; set; }
    public List<Order> Orders { get; set; }
}

public class Order
{
    public int OrderNumber { get; set; }
    public DateTime OrderDate { get; set; }
    // Other properties and methods of the Order class
}

Example - 3 :

A dog can bark at a cat. In this case, the Dog and Cat classes are associated with each other.

Here's an example code:

public class Dog
{
    public string Name { get; set; }
    public void BarkAt(Cat cat)
    {
        // Bark at the cat
    }
}

public class Cat
{
    public string Name { get; set; }
}

Summary :

In all of these examples, the associated classes (e.g., Person and Calculator, Customer and Order, Dog and Cat) interact with each other, but they may or may not have a reference to each other, and they may or may not depend on each other.


3. Composition:

Composition is a relationship where one object is composed of one or more objects of other classes. The composed objects cannot exist independently of the object that contains them. In other words, the composed objects are parts of the containing object, and when the containing object is destroyed, the composed objects are destroyed as well.

Here are a few examples of composition in C#:

Example - 1 :

A car has wheels, and each wheel is a part of the car. In this case, the Car class is composed of the Wheel class.

Here's an example code:

public class Car
{
    public List<Wheel> Wheels { get; set; }
    // Other properties and methods of the Car class
}

public class Wheel
{
    // Properties and methods of the Wheel class
}


Example - 2 :

A house has rooms, and each room is a part of the house. In this case, the House class is composed of the Room class.

Here's an example code:

public class House
{
    public List<Room> Rooms { get; set; }
    // Other properties and methods of the House class
}

public class Room
{
    // Properties and methods of the Room class
}


Comments

Popular posts from this blog

Send API POST Request in MS SQL Server

Delete Empty Rows or Null Rows in Datatable in C#

Restrict Special Characters in Windows Application using c#

Use immutable data structures to avoid unnecessary change detection In Angular

Domain Driven Design (DDD) in .NET Core

Difference of High-Level Design (HLD) and Low-Level Design (LLD)

Configure the API gateway in Microservices Architecture with example in .NET Core

Export to Excel in Angular 6 | 7 | 8 | 9 | 10

Create Custom Form Control for ng-select in Angular

Display PDF Or Tiff Files In IFrame