0
0
LLDsystem_design~7 mins

Relationships (association, aggregation, composition) in LLD - System Design Guide

Choose your learning style9 modes available
Problem Statement
When designing software, unclear or incorrect relationships between objects cause tight coupling, memory leaks, or unexpected object lifetimes. This leads to bugs where objects are deleted too early or remain in memory unnecessarily, making the system fragile and hard to maintain.
Solution
Use clear relationship types to define how objects connect and depend on each other. Association means objects know each other but manage their own lifetimes. Aggregation means one object contains others but they can exist independently. Composition means one object owns others and controls their lifetimes, deleting them when it is deleted.
Architecture
Object A
(Association)
Object B
Object D
(Aggregation)
Object G
(Composition)
Object H

The diagram shows three types of relationships: association where objects reference each other loosely, aggregation where one object contains others but they can live independently, and composition where one object owns and controls the lifetime of contained objects.

Trade-offs
✓ Pros
Association allows flexible connections without ownership, reducing tight coupling.
Aggregation models whole-part relationships without forcing lifetime dependency.
Composition ensures contained objects are cleaned up automatically with the owner, preventing memory leaks.
✗ Cons
Association can lead to dangling references if not managed carefully.
Aggregation requires careful lifetime management to avoid orphaned objects.
Composition can cause tight coupling and less reuse since contained objects depend fully on the owner.
Use association when objects interact but manage their own lifetimes. Use aggregation when parts can exist independently but belong to a whole. Use composition when parts must be created and destroyed with the whole, ensuring strict ownership.
Avoid composition if parts need to be shared or reused independently. Avoid aggregation if lifetime management is unclear or complex. Avoid association if ownership and lifecycle need to be strictly controlled.
Real World Examples
Amazon
Uses composition in their order system where an Order object owns OrderItems, ensuring items are deleted when the order is canceled.
Uber
Uses aggregation between Drivers and Vehicles, as vehicles can exist independently of drivers and be reassigned.
LinkedIn
Uses association between User profiles and Groups, where users can join or leave groups without ownership.
Code Example
The before code shows a Car tightly coupled to an Engine it creates internally, making reuse and testing harder. The after code shows association where Vehicle references a Driver without owning it, aggregation where Team contains members that can exist independently, and composition where House creates and owns Room objects, controlling their lifetime.
LLD
### Before: No clear relationship, tight coupling and unclear ownership
class Engine:
    def __init__(self):
        self.status = 'off'

class Car:
    def __init__(self):
        self.engine = Engine()  # tightly coupled

car = Car()
print(car.engine.status)


### After: Clear relationships
# Association example
class Driver:
    def __init__(self, name):
        self.name = name

class Vehicle:
    def __init__(self):
        self.driver = None  # association: vehicle knows driver but does not own

vehicle = Vehicle()
driver = Driver('Alice')
vehicle.driver = driver

# Aggregation example
class Team:
    def __init__(self):
        self.members = []  # aggregation: members can exist independently

    def add_member(self, member):
        self.members.append(member)

team = Team()
member1 = Driver('Bob')
team.add_member(member1)

# Composition example
class Room:
    def __init__(self):
        self.lights_on = False

class House:
    def __init__(self):
        self.rooms = [Room() for _ in range(3)]  # composition: house owns rooms

house = House()
print(len(house.rooms))
OutputSuccess
Alternatives
Dependency Injection
Instead of owning or containing objects, dependencies are passed in from outside, reducing tight coupling.
Use when: When you want to increase testability and flexibility by decoupling object creation from usage.
Event-driven communication
Objects communicate via events rather than direct references, reducing direct dependencies.
Use when: When you want loose coupling and asynchronous interactions between components.
Summary
Clear object relationships prevent bugs related to ownership and lifetime management.
Association, aggregation, and composition differ by how strongly objects are connected and who controls their lifetimes.
Choosing the right relationship type improves code clarity, reuse, and resource management.