Association vs composition vs aggregation

Association, composition, and aggregation are key concepts in object-oriented programming (OOP) that define relationships between classes. These relationships play a crucial role in modeling and designing complex systems.

Association vs aggregation vs composition
Association vs aggregation vs composition

UML (unified modeling language) is a standardized notation used to model and visualize software systems. Below is a visualization of UML notations for association, aggregation, and composition.

UML notations
UML notations

Association

An association is a relationship where all objects have their own lifecycle, and there is no owner. It describes how items are related to one another and how they use one another's functionality. An association can be divided into two types: composition and aggregation. First, let's understand an association with a scenario

Consider the case of teachers and students. Numerous students can be associated with a single teacher, and a single student can be associated with numerous teachers. However, there is no ownership between the two objects, and each has its own lifecycle. Both can be created and deleted on their own.

Now, let's have a look at the UML diagram for this scenario.

code_drawing


The code below shows a sample implementation of association in the teacher-student scenario as described above.

import java.util.ArrayList;
import java.util.List;
class Teacher {
private String name;
private List<Student> students;
public Teacher(String name) {
this.name = name;
students = new ArrayList<>();
}
public void addStudent(Student student) {
students.add(student);
}
}
class Student {
private String name;
private List<Teacher> teachers;
public Student(String name) {
this.name = name;
teachers = new ArrayList<>();
}
public void addTeacher(Teacher teacher) {
teachers.add(teacher);
}
}

Code explanation:

  • In the Main class, Teacher and Student objects are created.

  • The addStudent() method is used to associate students with their respective teachers. The Teacher class has a private instance variable students, which is a list of Student objects associated with the teacher.

  • The addTeacher() method is used to associate teachers with their respective students.The Student class has a private instance variable teachers, which is a list of Teacher objects associated with the student.

Aggregation

An aggregation is a subset of an association in which all objects have their own lifespan, yet ownership exists, and child objects cannot belong to another parent object. Let's also understand an aggregation with a scenario.
Consider the teacher and their departments as an example. A single teacher cannot belong to more than one department. However, deleting the department does not destroy the teacher's object. We can consider it a "has-a" relationship.

code_drawing


The code below shows a sample implementation of aggregation in the teacher-department scenario as described above.

import java.util.List;
import java.util.ArrayList;
public class Department {
private String name;
private List<Teacher> teachers;
public Department(String name) {
this.name = name;
this.teachers = new ArrayList<>();
}
public void addTeacher(Teacher teacher) {
teachers.add(teacher);
}}
public class Teacher {
private String name;
public Teacher(String name) {
this.name = name;
}}

Code explanation:

  • The Department class has an aggregation relationship with the Teacher class, as it maintains a list of teachers.

  • The addTeacher() method is used to associate a teacher with a department by adding the teacher to the list of teachers.

Composition

A composition is yet another specialized type of aggregation which we can refer to as a death relationship. It is a powerful form of aggregation. In it, a child object has no lifecycle, and if the parent object is erased, all child objects are deleted as well. Let's understand a composition with a scenario:

Consider the link between a house and its rooms. A house can have numerous rooms - no room has an autonomous life, and no room can belong to two different houses. If we delete the house, the room will be deleted as well. Similarly, another composition relation is between questions and options. A single question can have several answers, and the answers cannot be the same.

code_drawing


The code below shows a sample implementation of composition in the house-room scenario as described above.

import java.util.ArrayList;
import java.util.List;
public class House {
private String address;
private List<Room> rooms;
public House(String address) {
this.address = address;
this.rooms = new ArrayList<>();
}
public void addRoom(int number) {
Room room = new Room(number);
rooms.add(room);
}
}
public class Room {
private int number;
public Room(int number) {
this.number = number;
}
}
public class Main {
public static void main(String[] args) {
House house = new House("123 Main St");
house.addRoom(1);
house.addRoom(2);}}

Code explanation:

  • the House class, the rooms attribute is declared as a list of Room objects.

  • The addRoom() method creates a new Room object and adds it to the list of rooms associated with the house.

Association vs aggregation vs composition


Association

Aggregation

Composition

Relationship

Objects of two classes interact with each other, but have independent lifecycles.


Objects of one class contain a reference to objects of another class.


Objects of one class are composed of objects of another class as integral parts.

Ownership

No ownership or strong dependency.


Shared ownership, where the contained objects can be associated with multiple containers.

Strong ownership, where the contained objects cannot exist independently

Lifecycle


Objects have independent lifecycles and can exist outside of the association.


The contained objects can exist independently and have shared ownership.


The contained objects cannot exist independently and have a lifecycle dependent on the container.

UML representation

Typically represented as a line connecting the related classes.


Represented with a diamond-shaped arrow pointing towards the container class.


Represented with a diamond-shaped arrow pointing towards the container class, accompanied by a solid line.

Conclusion

In conclusion, understanding the concepts of composition, association, and aggregation is important in object-oriented programming and system modeling. Understanding these relationships helps in accurately modeling and designing software systems. Each relationship type has its own characteristics and implications, allowing for the representation of different dependencies and ownership scenarios

Match The Answer
Select an option from the left-hand side

Consider the relationship between a car and its engine. A car cannot exist without an engine, and an engine cannot be shared among multiple cars.

Composition

In a social media platform, consider the relationship between a user and their friends. A user can have multiple friends, and a friend can be associated with multiple users.

Aggregation

Consider the relationship between a bank and its associated accounts. Each account belongs to only one bank, and a bank can have multiple accounts. If a bank goes bankrupt, all the accounts associated with that bank become inaccessible

Association


Copyright ©2024 Educative, Inc. All rights reserved