Updated on 02 Jul, 202613 mins read 128 views

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:

Inheritance

The belief was:

Inheritance = Reuse

Example:

class Animal
{
};

class Dog : public Animal
{
};

This looked elegant.

But large systems revealed problems:

Deep inheritance trees

Fragile base classes

Unexpected side effects

Complex dependencies

Engineers began searching for alternatives.

They discovered:

Composition

which 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 Independently

This 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:

House

and

Room

A house contains rooms.

Question:

Can a room exist independently?

Usually:

No

Destroy the house:

House demolished

Rooms 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 ◆-------- Room

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

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

Room

is 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: No

Composition confirmed.

Association vs Aggregation vs Composition

FeatureAssociationAggregationComposition
OwnershipNoneWeak (has-a)Strong (has-a)
LifecycleIndependentIndependentDependent (part dies with whole)
TightnessLoose couplingModerate couplingTight coupling
MultiplicityFlexible (1:1, 1:N, N:N)Whole can group many partsWhole composed of integral parts
ResuabilityHigh (parts reusable)Moderate (parts reused)Low (parts not reused outside)
UML SymbolSolid LineHollow DiamondFilled Diamond
Who creates parts?Either side or externalExternal (passed in)Whole (created internally)
Real ExampleStudent —  CoursePlaylist and SongOrder 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

Buy Me A Coffee

Leave a comment

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