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.
Diagram:
Teacher ------ StudentCode:
class Student {}
class Teacher {
students: Student[];
}Teacher knows students.
Association.
Characteristics:
Objects can exist independently.
Example:
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.
Diagram:
Team âââââ Player
White diamondCode:
class Player {}
class Team {
players: Player[];
}If team is deleted:
Players survive.Real World Example
Department
Department
|
+---- EmployeesDelete department.
Employees still exist.
Aggregation 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.
Diagram:
House âââââ Room
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.
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.
Diagram:
PaymentService -----> OrderCharacteristics:
Short-lived
Temporary
No ownershipReal Example
NotificationService
â
UserNotificationService uses User.
Dependency.
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
â˛
|
DogValid Examples
Car IS-A Vehicle
Dog IS-A Animal
Admin IS-A UserInvalid Examples
Engine IS-A CardNo
Engine is part of car.
Composition.
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 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 Professinal LLD
When in doubt:
Prefer CompositionOver:
InheritanceThis single rule prevents many design disasters.
Leave a comment
Your email address will not be published. Required fields are marked *
