Skip to content
Low Level Design Mastery Logo
LowLevelDesign Mastery

Association

Classes that know about each other and can communicate.

Association is a relationship where one class knows about another class and can use its services. It’s a “uses-a” or “knows-a” relationship where classes can exist independently.

Association represents a relationship where:

  • Classes know about each other
  • Classes can communicate with each other
  • Classes have independent lifecycles
  • Usually implemented with references or pointers
  • Independent existence - Both classes can exist without each other
  • Communication - Classes can call each other’s methods
  • References - One class holds a reference to another
  • Weaker than composition - No ownership implied

One class knows about another, but not vice versa.

unidirectional_association.py
class Teacher:
def __init__(self, name: str):
self.name = name
self.courses = [] # Association with Course
def teach(self, course):
"""Teacher uses Course"""
self.courses.append(course)
return f"{self.name} is teaching {course.name}"
class Course:
def __init__(self, name: str):
self.name = name
# Usage
teacher = Teacher("Dr. Smith")
course = Course("Python Programming")
print(teacher.teach(course)) # "Dr. Smith is teaching Python Programming"
# Course doesn't know about Teacher

Both classes know about each other.

bidirectional_association.py
class Student:
def __init__(self, name: str):
self.name = name
self.courses = [] # Student knows about courses
def enroll(self, course):
self.courses.append(course)
course.students.append(self) # Course knows about student
class Course:
def __init__(self, name: str):
self.name = name
self.students = [] # Course knows about students
# Usage
student = Student("Alice")
course = Course("Data Structures")
student.enroll(course)
print(f"{student.name} enrolled in {course.name}")
print(f"{course.name} has {len(course.students)} student(s)")
Diagram
library_association.py
class Book:
def __init__(self, isbn: str, title: str):
self.isbn = isbn
self.title = title
self.available = True
class Loan:
def __init__(self, member, book):
self.member = member # Association - references Member
self.book = book # Association - references Book
self.borrow_date = None
self.return_date = None
def borrow(self):
if self.book.available:
self.book.available = False
self.borrow_date = "2024-01-01"
return f"{self.member.name} borrowed {self.book.title}"
return "Book not available"
class Member:
def __init__(self, name: str, member_id: str):
self.name = name
self.member_id = member_id
def borrow_book(self, book):
loan = Loan(self, book) # Creates association
return loan.borrow()
# Usage
member = Member("Alice", "M001")
book = Book("12345", "Python Guide")
print(member.borrow_book(book)) # "Alice borrowed Python Guide"

Use Association when:

  • Classes need to communicate with each other
  • Classes have independent lifecycles
  • One class uses another’s services
  • You need flexibility to change relationships
  • Classes are loosely coupled

Examples:

  • Teacher ↔ Course
  • Student ↔ Course
  • Order ↔ Product
  • Loan ↔ Book
  • Customer ↔ Order