Updated on 25 Jun, 202617 mins read 17 views

When beginners learn Object-Oriented Programming, they usually focus on:

Classes
Objects
Methods
Inheritance

But 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
Payment

The 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
Dependency

can 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 About

Association does NOT imply:

Ownership
Lifecylce Control
Creation Responsibility

It simply means:

One object can communicate with another.

Real-World Analogy

Imagine:

Doctor ←→ Patient

A 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 Order

Diagram:

+------------+      +------------+
| 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 With

Think:

Association

Association vs Ownership

A common misunderstanding.

Many developers think:

Customer → Order

means Customer owns Order.

Not necessarily.

Association only says:

There is a relationship.

Ownership is introduced later through:

Aggregation
Composition

Multiplicity

Association alone is incomplete.

We must answer:

How many obejcts participate?

This is called multiplicity.

Example:

One Customer Many Orders

Diagram:

Customer 1 -------- * Order

Meaning:

One customer can have many orders.

Multiplicity Notation

Exactly One

1

Example:

Order 1 ---- 1 Payment

One payment per order.

Zero or One

0..1

Example:

Order ---- 0..1 Coupon

Coupon optional.

Many

*

Meaning:

Unlimited

Example:

Customer 1 ---- * Order

One or More

1..*

Example:

Team 1 ---- 1..* Player

Range

2..5

Example:

Car 1 ---- 4..5 Tire

Four or five tires.

Reading Multiplicity Correctly

Students often read multiplicity backward.

Example:

Customer 1 -------- * Order

Correct 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 ------- * Student

Interpretation:

One teacher teaches many students.

One-to-One Association

Example:

Person 1 -------- 1 Passport

Meaning:

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 ------ * Employee

Meaning:

One department contains many employees.

C++ Example:

class Employee
{
};

class Department
{
private:
    vector<Employee*> employees;
};

Many-to-Many Association

Example:

Student * ------- * Course

Meaning:

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 ------> Order

Meaning:

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 <------> Order

Meaning:

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 Employee

Diagram:

Employee
    ^
    |
    |
Employee

Meaning:

Manager is also an Employee.

C++ Example:

class Employee
{
private:
    Employee* manager;
};

Common Uses:

Organization Hierarchies

Folder Structures

Comment Threads

Category Trees
Buy Me A Coffee

Leave a comment

Your email address will not be published. Required fields are marked *