Aggregation vs Composition
In object-oriented programming, both aggregation and composition are concepts that describe relationships between classes. They both represent ways to establish connections between objects. Let’s explore the differences between aggregation and composition using examples
Aggregation:
Aggregation represents a “has-a” or “contains-a” relationship between classes. In aggregation, one class contains another class as a part, but the contained class can exist independently. If the container class is destroyed, the contained class can still exist. Aggregation is often depicted as a weaker relationship than composition. Aggregation represents a “has-a” relationship where one class contains another class as a part, but the contained class can exist independently.
Example 1: Library and Books
public class Library {
private List<Book> books;
public Library(List<Book> books) {
this.books = books;
}
}
public class Book {
private String title;
public Book(String title) {
this.title = title;
}
}
A Library
class contains multiple Book
objects, but the books can exist outside the library.
Example 2: University and Students
public class University {
private List<Student> students;
public University(List<Student> students) {
this.students = students;
}
}
public class Student {
private String name;
public Student(String name) {
this.name = name;
}
}
A University
class has multiple Student
objects. Students can graduate and continue their lives outside the university.
Example 3: Company and Employees
public class Company {
private List<Employee> employees;
public Company(List<Employee> employees) {
this.employees = employees;
}
}
public class Employee {
private String name;
public Employee(String name) {
this.name = name;
}
}
A Company
class has multiple Employee
objects. If the company is dissolved, employees can still exist independently.
Example 4: Car Dealership and Cars
public class CarDealership {
private List<Car> cars;
public CarDealership(List<Car> cars) {
this.cars = cars;
}
}
public class Car {
private String model;
public Car(String model) {
this.model = model;
}
}
A CarDealership
class has a collection of Car
objects. If the dealership closes, the cars are not necessarily destroyed.
Composition:
Composition represents a stronger relationship where one class is composed of one or more other classes, and the composed classes cannot exist independently without the container class.
Example 1: Computer and Processor
public class Computer {
private Processor processor;
public Computer(Processor processor) {
this.processor = processor;
}
}
public class Processor {
// Processor details
}
A Computer
class is composed of a Processor
class. A computer without a processor is not functional
Example 2: House and Rooms
public class House {
private List<Room> rooms;
public House(List<Room> rooms) {
this.rooms = rooms;
}
}
public class Room {
// Room details
}
A House
class is composed of multiple Room
objects. A house without rooms would not be a valid concept.
Example 3: Car and Engine
public class Car {
private Engine engine;
public Car(Engine engine) {
this.engine = engine;
}
}
public class Engine {
// Engine details
}
A Car
class is composed of an Engine
class. A car without an engine is just a shell.
Example 4: Human Body and Heart
public class HumanBody {
private Heart heart;
public HumanBody(Heart heart) {
this.heart = heart;
}
}
public class Heart {
// Heart details
}
A HumanBody
class is composed of a Heart
class. The body cannot function without the heart.
Example 5: Playlist and Songs
public class Playlist {
private List<Song> songs;
public Playlist(List<Song> songs) {
this.songs = songs;
}
}
public class Song {
// Song details
}
A Playlist
class is composed of various Song
objects. A playlist without songs wouldn't make sense.