Why Relationships Matter
Suppose I give you these classes:
class User {}
class Order {}
class Product {}
class Payment {}Do we have a design?
No
We only have isolated objects.
The real design begins when we answer:
How are these objects connected?
Relationships are the backbone of every software system.
Without relationships:
Objects are islandsWith relationships:
Objects become a systemThink Like a Real World System
Imagine:
Customer places Order
Order contains Products
Payment pays for OrderThis immediately creates a network.
Customer
|
|
v
Order
|
|
v
Product
Order
|
|
v
PaymentThis network is called an:
Object GraphTypes of Relationships
There are five major types of relationships every designer must know:
1. Association
2. Aggregation
3. Composition
4. Dependency
5. InheritanceRelationship 1 β Association
The simplest relationship.
Meaning:
Object A knows Object BExample:
Teacher teaches StudentTeacher exists independently.
Student exists independently.
Suppose:
There are two classes Student and Course.
A student enrols in a course.
Question: Does Student own Couse?
No.
Question: Does Course own Student?
No.
They simply know about each other.
This is association.Definition
Association is:
A relationship where one object knows about another object.
No ownership implied.
No lifecycle dependency implied.
Diagram:
Teacher ------ StudentCode:
class Student {}
class Teacher {
students: Student[];
}Teacher knows students.
Association.
class Course
{
};
class Student
{
private:
Course* course;
};Student knows about Course. Nothing more.
Characteristics:
Objects can exist independently.
Association implies:
Awareness
Communication
CollaborationBut NOT:
Ownership
Lifecycle ControlExample:
Teacher resignsStudents still exist.
Example:
Student leaves schoolTeacher still exists.
Therefore:
Weak relationshipReal Examples
User <--> GroupFood Delivery
Customer <--> RestaurantCardinality
Association usually has cardinality.
One-to-One
Person <--> PassportOne passport.
One person.
One-to-Many
Customer <--> OrdersOne customer.
Many orders.
Many-to-Many
Student <--> CourseMany students.
Many courses.
Relationship 2 β Aggregation
Aggregation means:
Whole-Part Relationshipbut
Part can exist independently.
Example:
Team --> PlayersA team contains players.
But players can exist without team.
Definition
Aggregation is:
A whole-part relationship where parts can exist independently of the whole.
Diagram:
Team βββββ Player
Empty White diamondCode:
class Player {}
class Team {
players: Player[];
}If team is deleted:
Players survive.Real World Example
Department
Department
|
+---- EmployeesDelete department.
Employees still exist.
Lifecycle
Department destroyed:
Department diesEmployees:
Still aliveAggregation Rule
Ask:
If parent dies,
does child survive?If yes:
AggregationRelationship 3 β Composition
Composition is stronger.
Meaning:
Part cannot exist without whole.Example:
House ---> RoomRoom belongs to house.
Destroy house.
Room also disappears.
Definition
Composition is:
A strong ownership relationship where parts cannot exist independently.
Diagram:
House βββββ Room
Filled Black diamondCode:
class Room {}
class House {
rooms: Room[];
}Difference:
Aggregation:
Parent removed
Child survives
Composition:
Parent removed
Child removedReal Example
Order
Order
|
+---- OrderItemsOrderItem has no meaning outside Order.
Composition.
Example:
class Order {
items: OrderItem[];
}Delete Order.
OrderItems disappear.
Aggregation vs Composition
Aggregation:
Department β Employee
Team β Player
Library β BookChild survives.
Composition:
Order β OrderItem
House β Room
Invoice β InvoiceLineChild dies with parent.
Comparison Table
| Aggregation | Composition |
|---|---|
| Weak ownership | Strong ownership |
| Independent lifecycle | Shared lifecycle |
| Part survives owner | Part dies with owner |
| Empty diamond | Filled diamond |
Easy Memory Trick
Ask:
Can child live alone?YES
AggregationNO
CompositionRelationship 4 β Dependency
Weakest relationship.
Meaning:
Object temporarily uses another object.Example:
class PaymentService {
process(order: Order) {}
}PaymentService uses Order.
But doesn't own it.
Dependency.
Definition
Dependency is:
A relationship where one object temporarily relies on another.
Diagram:
PaymentService -----> OrderCharacteristics:
Short-lived
Temporary
No ownershipDependency means:
Uses
Needs
Callsbut not:
Contains
OwnsReal Example
class EmailService
{
public:
void sendEmail()
{
}
};class PaymentService
{
public:
void processPayment(
EmailService& email)
{
email.sendEmail();
}
};Temporary usage. No ownership.
Why Dependency Matters
Because dependency creates coupling.
Bad:
class UserService {
private paymentService =
new RazorpayService();
}Strong dependency.
Good:
class UserService {
constructor(
paymentService:
PaymentService
) {}
}Dependency Injection.
Relationship 5 β Inheritance
Most abused relationship.
Meaning:
IS-AExample:
Dog IS-A AnimalCode:
class Animal {}
class Dog extends Animal {}Inheritance.
Diagram:
Animal
β²
|
+----+----+
| |
Dog CatValid Examples
Car IS-A Vehicle
Dog IS-A Animal
Admin IS-A UserInvalid Examples
Engine IS-A CardNo
Engine is part of car.
Composition.
Inheritance Test
Ask:
Can I replace βis aβ?Example:
Dog is an AnimalValid.
Example:
Engine is a CarInvalid.
No inheritance.
The Inheritance Trap
Beginners overuse inheritance.
Bad:
class Vehicle {}
class Car extends Vehicle {}
class ElectricCar extends Car {}
class Tesla extends ElectricCar {}
class ModelS extends Tesla {}Deep inheritance tree.
Nightmare.
Modern design prefers:
Composition over InheritanceReal Example:
Bad:
class Bird {
fly()
}Then:
class Penguin extends Bird {}Problem:
Penguins cannot fly.
Inheritance broken.
This violates:
Liskov Substitution PrincipleRelationship identification Framework
When modeling systems ask these questions:
Question 1:
Is-A?If yes:
InheritanceQuestion 2:
Whole-Part?If yes:
Continue.
Question 3:
Can part exist independently?If yes:
AggregationIf no:
CompositionQuestion 4:
Does object simply know another?If yes:
AssociationQuestion 5
Does object merely use another?If yes:
DependencyRelationship Selection Framework
When modeling objects ask:
Question 1
Is it IS-A?Yes
InheritanceQuestion 2
Is it Part-Of?Yes, then either Aggregation or Composition.
Ask:
Can child survive?Yes
AggregationNo
CompositionQuestion 3
Does object merely know another object?AssociationQuestion 4
Temporarily uses?DependencyGolden Rule of Professional LLD
When in doubt:
Prefer CompositionOver:
InheritanceThis single rule prevents many design disasters.
Relationship Strength Spectrum
Weakest
|
v
Dependency
Association
Aggregation
Composition
Inheritance*
^
|
Strongest(*Inheritance is conceptually different but often forms strong coupling.)
Leave a comment
Your email address will not be published. Required fields are marked *
