Skip to content

Association

Association is a relationship between two classes where one class knows about or is connected to another class. It represents a “uses-a” relationship where objects can communicate with each other.

  • Models real-world relationships where entities interact with each other
  • Enables collaboration between objects
  • Forms the basis for more specific relationships (aggregation, composition)

A teacher and a student have an association:

  • They know about each other
  • They interact (teacher teaches, student learns)
  • They have independent lifetimes — a teacher exists without a specific student
  • The relationship can be one-way (student knows their teacher) or two-way

One class knows about the other, but not vice versa.

class Order {
constructor(id, customer) {
this.id = id;
this.customer = customer; // Order knows about Customer
}
}
class Customer {
constructor(name) { this.name = name; }
}

Both classes know about each other.

class Doctor {
constructor(name) {
this.name = name;
this.patients = [];
}
addPatient(patient) {
this.patients.push(patient);
patient.addDoctor(this);
}
}
class Patient {
constructor(name) {
this.name = name;
this.doctors = [];
}
addDoctor(doctor) { this.doctors.push(doctor); }
}
TypeDescriptionExample
One-to-OneOne object maps to one otherPerson → Passport
One-to-ManyOne object maps to many othersDepartment → Employees
Many-to-ManyMany objects map to many othersStudents → Courses

ConceptKey Point
AssociationRelationship between objects (uses-a)
UnidirectionalOne-way knowledge
BidirectionalTwo-way knowledge
Cardinality1:1, 1:N, N:M
LifetimeObjects have independent lifetimes

Previous Topic: Object Relationships → Next Topic: Aggregation →