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:
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:
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:
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:
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:
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:
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:
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:
Comments
Post a Comment