Association vs Aggregation vs Composition
Understanding the nuances between Association, Aggregation, and Composition is essential for designing robust, scalable object-oriented systems. These relationships define how classes interact with each other β think of them as the “glue” in your application architecture.
π Java Core Concept: Association vs Aggregation vs Composition
π§ What Are These?
These are all forms of “has-a” relationships between objects, but they differ in strength of connection and lifecycle dependency.
Concept | Type of Relationship | Lifespan Dependency | Ownership |
---|---|---|---|
Association | “uses-a” / “has-a” | Independent | No |
Aggregation | WholeβPart (loose) | Independent | Shared |
Composition | WholeβPart (strong) | Dependent | Exclusive |
1οΈβ£ Association
Definition: A general relationship between two classes where one class uses or references another.
- No ownership.
- Both can exist independently.
- Often implemented via a field, method parameter, or local variable.
π¦ Example:
class Driver {
String name;
Driver(String name) {
this.name = name;
}
void drive(Car car) {
System.out.println(name + " is driving the car.");
}
}
class Car {
String model;
Car(String model) {
this.model = model;
}
}
π Notes:
Driver
andCar
have an association.Driver
uses aCar
, but doesnβt own or control its lifecycle.
2οΈβ£ Aggregation
Definition: A specialized form of Association where one class has another, but they can live independently.
- Represents a “whole-part” relationship.
- Weak ownership: Parts can belong to multiple wholes.
π¦ Example:
class Department {
String name;
List<Teacher> teachers;
Department(String name, List<Teacher> teachers) {
this.name = name;
this.teachers = teachers;
}
}
class Teacher {
String name;
Teacher(String name) {
this.name = name;
}
}
π Notes:
Department
aggregatesTeacher
s.- If a department is deleted, the teachers still exist.
- One teacher could belong to multiple departments.
3οΈβ£ Composition
Definition: A strong form of Aggregation where the child object cannot exist without the parent.
- Strong ownership.
- Lifecycles are tightly coupled.
π¦ Example:
class House {
private Room room;
House() {
this.room = new Room();
}
void show() {
room.describe();
}
}
class Room {
void describe() {
System.out.println("This is a room in the house.");
}
}
π Notes:
Room
exists only insideHouse
.- If
House
is destroyed,Room
goes with it. - You compose objects to build complex ones.
π Visual Metaphor
Relation | Metaphor |
---|---|
Association | A person uses a bike. |
Aggregation | A university has students. If the university shuts down, students still exist. |
Composition | A body has a heart. If the body dies, the heart is no longer useful alone. |
π§ Why It Matters
Understanding these distinctions helps you:
- Design better data models
- Avoid memory leaks and tight coupling
- Clearly define ownership, responsibility, and object lifecycles
π Homework for Students
π Tasks:
Association:
- Create
Customer
andOrder
classes where:Customer
has a methodplaceOrder(Order order)
- Show that
Customer
andOrder
are independent.
- Create
Aggregation:
- Create a
Library
class and aBook
class. - A library has many books, but books can exist without a library.
- Delete the library object β books should remain.
- Create a
Composition:
- Create a
Computer
class composed ofCPU
andRAM
classes. CPU
andRAM
should only be created withinComputer
’s constructor.- Prove they cannot exist independently.
- Create a
Ready to move into Static Keyword, Final Keyword, or start OOP design patterns like Factory Method or Singleton?