# Association, Aggregation and Composition — Object-Oriented Programming (OOP)

Source: https://www.skillbyai.com/en/oop/r-assoc

> Distinguish association, aggregation and composition by ownership and lifetime.

## Has-a relationships of different strengths

Objects relate to each other in several ways. **Association** is a general relationship where objects use or know about each other: a teacher teaches students, and both exist independently. **Aggregation** is a **whole-part** association with **weak ownership**: a department has professors, but if the department is closed the professors still exist and can join another; parts can be shared. **Composition** is a whole-part relationship with **strong ownership**: a house has rooms, an order has order lines; the parts belong to exactly one whole and their **lifetime** is tied to it, so destroying the whole destroys the parts. **Dependency** is the weakest link: one class temporarily uses another, for example as a method parameter. These distinctions matter for design (who creates and deletes what, what can be shared) and appear in UML diagrams. In code, composition usually means the whole **creates** its parts and does not expose them for others to keep; aggregation usually means parts are **passed in** from outside.

## From association to composition

Relationships range from loose links to parts that live and die with their whole.

![Three pairs of boxes: one joined by a plain line, one by a line with a hollow diamond, one by a line with a filled diamond.](assets/figures/oop/section-5-map.svg) — Figure 5.1 — Association, aggregation (hollow diamond) and composition (filled diamond).

## Aggregation versus composition in code

Who creates the parts reveals the relationship.

```java
// aggregation: professors exist independently and are passed in
public class Department {
    private final List<Professor> professors = new ArrayList<>();
    public void addProfessor(Professor p) { professors.add(p); }   // shared, created elsewhere
}

// composition: order lines are created by and belong to one Order
public class Order {
    private final List<OrderLine> lines = new ArrayList<>();

    public void addLine(String sku, int qty, long unitPricePaise) {
        lines.add(new OrderLine(sku, qty, unitPricePaise));          // created inside
    }

    public long totalPaise() {
        return lines.stream().mapToLong(OrderLine::totalPaise).sum();
    }

    private record OrderLine(String sku, int qty, long unitPricePaise) {
        long totalPaise() { return qty * unitPricePaise; }
    }
}
```

## A college, its departments and its students

A building and its rooms are composition: demolish the building and the rooms are gone. A club and its members are aggregation: the club can close, and the members carry on and join other clubs.

**Quiz:** An Order creates its OrderLine objects, and they cannot exist without it. Which relationship is this?

- [x] Composition
- [ ] Association
- [ ] Aggregation
- [ ] Dependency

*Answer:* Composition. Strong ownership with a shared lifetime is composition.
