When beginners learn Object-Oriented Programming, they usually focus on:
Classes
Objects
Methods
InheritanceBut in real software systems, the most important question is often:
How do objects related to each other?
Consider an E-Commerce system.
We have:
Customer
Order
Product
PaymentThe classes themselves are simple.
The challenge is understanding:
Who knows whom?
Who can access whom?
Who owns whom?
Who communicates with whom?
How many relationships exist?Most real-world design mistakes occur not because a class is poor written, but because relationships between classes are poorly modeled.
Association is the first and most fundamental relationship in UML.
Everything else:
Aggregation
Composition
Dependencycan be viewed as specialized forms of association.
What Is an Association?
Definition:
Association represents a structural relationship where one object knows about another object.
The key phrase is:
Knows AboutAssociation does NOT imply:
Ownership
Lifecylce Control
Creation ResponsibilityIt simply means:
One object can communicate with another.Real-World Analogy
Imagine:
Doctor ←→ PatientA doctor knows about patients.
A patient knows about doctors.
Neither owns the other.
Neither controls the other's lifecycle.
This is a classic association.
First UML Association
Example:
Customer places OrderDiagram:
+------------+ +------------+
| Customer |------| Order |
+------------+ +------------+Simple line.
That's all UML requires.
Interpretation:
Customer and Order are related.Association in C++
Suppose:
class Order
{
};
class Customer
{
private:
vector<Order*> orders;
};Customer knows Orders
Association exists.
Notice:
Order can exist independently.Customer can exist independently.Association does not imply ownership.
Mental Model
Whenever you hear:
Uses
Knows
Works With
References
Interacts WithThink:
AssociationAssociation vs Ownership
A common misunderstanding.
Many developers think:
Customer → Ordermeans Customer owns Order.
Not necessarily.
Association only says:
There is a relationship.Ownership is introduced later through:
Aggregation
CompositionMultiplicity
Association alone is incomplete.
We must answer:
How many obejcts participate?This is called multiplicity.
Example:
One Customer Many OrdersDiagram:
Customer 1 -------- * OrderMeaning:
One customer can have many orders.Multiplicity Notation
Exactly One
1Example:
Order 1 ---- 1 PaymentOne payment per order.
Zero or One
0..1Example:
Order ---- 0..1 CouponCoupon optional.
Many
*Meaning:
UnlimitedExample:
Customer 1 ---- * OrderOne or More
1..*Example:
Team 1 ---- 1..* PlayerRange
2..5Example:
Car 1 ---- 4..5 TireFour or five tires.
Reading Multiplicity Correctly
Students often read multiplicity backward.
Example:
Customer 1 -------- * OrderCorrect reading:
One Customer can have many Orders.NOT:
One Order has many Customers.Rule:
Look at the multiplicity near the opposite class.
Example:
Teacher 1 ------- * StudentInterpretation:
One teacher teaches many students.One-to-One Association
Example:
Person 1 -------- 1 PassportMeaning:
Each person has one passport.
Each passport belongs to one person.C++ Example:
class Passport
{
};
class Person
{
private:
Passport* passport;
};One-to-Many Association
Example:
Department 1 ------ * EmployeeMeaning:
One department contains many employees.C++ Example:
class Employee
{
};
class Department
{
private:
vector<Employee*> employees;
};Many-to-Many Association
Example:
Student * ------- * CourseMeaning:
Student can enroll in manay courses.
Course can contain many students.C++ Example:
class Course;
class Student
{
private:
vector<Course*> courses;
};
class Course
{
private:
vector<Student*> students;
};Navigability
Association may be directional.
Question:
Who knows whom?Unidirectional Association
Example:
Customer ------> OrderMeaning:
Customer knows Order.
Order does NOT know Customer.C++ Example:
class Order
{
};
class Customer
{
private:
vector<Order*> orders;
};Only one side has a reference.
Bidirectional Association
Example:
Customer <------> OrderMeaning:
Customer knows Order.
Order knows Customer.C++ Example:
class Customer;
class Order
{
private:
Customer* customer;
};
class Customer
{
private:
vector<Order*> orders;
};Both objects know each other.
When to Use Bidirectional Associations
Many beginners create bidirectional relationships everywhere.
Bad idea.
Example:
Customer ↔ Order
might be useful
But:
Product ↔ Every Other Class
create chaos.Rule:
Use bidirectional association only when both directions are genuinely needed.
Self Association
A class can associate with itself.
Example:
Employee manages EmployeeDiagram:
Employee
^
|
|
EmployeeMeaning:
Manager is also an Employee.C++ Example:
class Employee
{
private:
Employee* manager;
};Common Uses:
Organization Hierarchies
Folder Structures
Comment Threads
Category TreesLeave a comment
Your email address will not be published. Required fields are marked *


