What if a relationship is so strong that the “part” is meaningless and cannot even exist without the “whole”?
This is world of Composition. It represents the strongest form of “has-a” relationship, where the whole owns the parts and controls their lifecycle.
When you use composition, you are saying:
- This object is composed of other objects, and if the container goes away, so do its parts.
Historical Motivation
Early OOP heavily emphasized:
InheritanceThe belief was:
Inheritance = ReuseExample:
class Animal
{
};
class Dog : public Animal
{
};This looked elegant.
But large systems revealed problems:
Deep inheritance trees
Fragile base classes
Unexpected side effects
Complex dependenciesEngineers began searching for alternatives.
They discovered:
Compositionwhich often produced simpler, more maintainable systems.
What Is Composition?
Composition is a special type of association that signifies strong ownership between objects. The “whole” class is fully responsible for creating, managing, and destroying the “part” objects. In fact, the parts cannot exist without the whole.
Definition:
Composition is a whole-part relationship where the whole exclusively owns the parts, and the parts cannot exist independently.
Key phrase:
Cannot Exist IndependentlyThis is defining characteristic.
Key Characteristics of Composition
- Represents a strong “has-a” relationship.
- The whole owns the part and control its lifecycle.
- When the whole is destroyed, the parts are also destroyed.
- The parts are not shared with any other object.
- The part has no independent meaning or identity outside the whole.
If the part makes no sense without the whole, use composition.
Real-World Analogy
Consider:
Houseand
RoomA house contains rooms.
Question:
Can a room exist independently?Usually:
NoDestroy the house:
House demolishedRooms disapper.
The room's lifecycle depends on the house.
This is Composition.
UML Notation
Composition is represented by:
Filled Diamond (◆) at the "whole" end of the relationship.Diagram:
House ◆-------- RoomThe diamond is placed on:
The Owner side.
Meaning:
House owns Rooms.Ownership Mental Model
Think of ownership levels:
Association = Relationship
|
|
Aggregation = Weak Ownership
|
|
Composition = Strong OwnershipThe Lifecycle Rule
The most important Composition rule:
If the whole dies, the parts die.
Remember this forever.
C++ Example
Room:
class Room
{
};House:
class House
{
private:
vector<Room> rooms;
};Notice:
Roomis stored directly.
Not:
Room*This implies ownership.
Why This Is Composition
Question: Who creates Rooms?
Answer: House
Question: Who destroys Rooms?
Answer: House
Question: Can Rooms survive independently?
Answer: NoComposition confirmed.
Association vs Aggregation vs Composition
| Feature | Association | Aggregation | Composition |
| Ownership | None | Weak (has-a) | Strong (has-a) |
| Lifecycle | Independent | Independent | Dependent (part dies with whole) |
| Tightness | Loose coupling | Moderate coupling | Tight coupling |
| Multiplicity | Flexible (1:1, 1:N, N:N) | Whole can group many parts | Whole composed of integral parts |
| Resuability | High (parts reusable) | Moderate (parts reused) | Low (parts not reused outside) |
| UML Symbol | Solid Line | Hollow Diamond | Filled Diamond |
| Who creates parts? | Either side or external | External (passed in) | Whole (created internally) |
| Real Example | Student — Course | Playlist and Song | Order and Item |
Think of it like this:
Association is a general connection: two classes simply know about each other.
Aggregation is a grouping: the whole and parts can exist independently.
Composition is an ownership: the part's existence is bound to the whole.
Decision Flowchart

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