Skip to content
Low Level Design Mastery Logo
LowLevelDesign Mastery

Aggregation

Weak ownership - parts can exist without the whole.

Aggregation is a “has-a” relationship where one class contains another, but the contained class can exist independently. It represents a weak ownership relationship.

Aggregation represents:

  • “Has-a” relationship (weak ownership)
  • Parts can exist independently of the whole
  • Parts can belong to multiple wholes
  • Lifecycle independence - parts aren’t destroyed when whole is destroyed
  • Weak ownership - Container doesn’t own the parts
  • Independent lifecycle - Parts can exist without container
  • Multi-ownership - Parts can belong to multiple containers
  • Hollow diamond in UML diagrams
aggregation_example.py
class University:
def __init__(self, name: str):
self.name = name
self.students = [] # Aggregation - students can exist without university
def add_student(self, student):
self.students.append(student)
class Student:
def __init__(self, name: str, student_id: str):
self.name = name
self.student_id = student_id
# Student can exist without being in a university
# Students can exist independently
student1 = Student("Alice", "S001")
student2 = Student("Bob", "S002")
university = University("MIT")
university.add_student(student1)
university.add_student(student2)
# Students still exist even if university is deleted
del university
# student1 and student2 still exist
Diagram
shopping_cart_aggregation.py
class Product:
def __init__(self, sku: str, name: str, price: float):
self.sku = sku
self.name = name
self.price = price
class ShoppingCart:
def __init__(self, customer_name: str):
self.customer_name = customer_name
self.items = [] # Aggregation - products exist independently
def add_item(self, product, quantity: int = 1):
self.items.append({"product": product, "quantity": quantity})
def get_total(self):
return sum(item["product"].price * item["quantity"] for item in self.items)
# Products exist independently
laptop = Product("LAP-001", "Laptop", 999.99)
mouse = Product("MOU-001", "Mouse", 29.99)
# Multiple carts can reference same products
cart1 = ShoppingCart("Alice")
cart2 = ShoppingCart("Bob")
cart1.add_item(laptop, 1)
cart1.add_item(mouse, 2)
cart2.add_item(laptop, 1) # Same product in different cart
print(f"Cart 1 total: ${cart1.get_total():.2f}") # $1059.97
print(f"Cart 2 total: ${cart2.get_total():.2f}") # $999.99
FeatureAggregationComposition
OwnershipWeakStrong
LifecycleIndependentDependent
Multi-ownershipYesNo
UML SymbolHollow diamondFilled diamond
ExampleUniversity → StudentsCar → Engine

Use Aggregation when:

  • Parts can exist without the whole
  • Parts can belong to multiple wholes
  • You need flexibility in relationships
  • Parts have independent lifecycle
  • Relationship is “part-of” but not essential

Examples:

  • University → Students (students can transfer)
  • Shopping Cart → Products (products exist independently)
  • Team → Players (players can change teams)
  • Library → Books (books can be removed)
  • Department → Employees (employees can move departments)