25 Oct 2023




Beginner
AspectCompositionAggregation
DefinitionA "whole" object contains one or more "parts" or "components." When the whole is destroyed, the parts are typically destroyed as well.A "whole" object contains one or more "parts," but the parts can exist independently and have their own lifecycle.
Relationship StrengthStrong relationship - parts are tightly coupled to the whole.Weaker relationship - parts can exist independently of the whole.
OwnershipThe whole object owns the parts. When the whole is deleted, the parts are automatically deleted.The whole object may reference parts, but it doesn't necessarily own them. Parts can exist on their own and be shared among multiple wholes.
Lifecycle DependencyStrong dependency - parts' lifecycles are tied to the whole.Weaker dependency - parts have their own lifecycles and can be created and destroyed independently.
ExampleA car is composed of an engine, wheels, and other components. When the car is scrapped, the engine and wheels are also scrapped.A library contains books, but the books can exist independently, be borrowed, and returned without affecting the library's existence.
Representation in UMLShown with a filled diamond (black diamond) at the whole end of the association line.Shown with an unfilled diamond (white diamond) at the whole end of the association line.
MultiplicityTypically, multiplicity at the whole end is 1, indicating that there is one and only one whole for the parts.Multiplicity at the whole end can be 0..1 or 0..*, indicating that there may be zero or multiple parts associated with the whole.

Examples:

ConceptComposition ExampleAggregation Example
DefinitionComposition represents a "whole-part" relationship where one object is composed of other objects. It is a stronger form of association. The parts are dependent on the whole.Aggregation represents a "has-a" relationship where one object contains or uses another object. It is a weaker form of association. The contained object can exist independently.
Example 1A car has an engine. The engine is an integral part of the car, and it cannot exist independently. It is specific to that car.A university contains departments. Departments can exist independently, and they can be associated with multiple universities.
Example 2A house has rooms. Rooms are part of the house and cannot exist independently. If the house is destroyed, the rooms cease to exist.A library contains books. Books can be checked out and exist independently of the library. They can also be part of different libraries.
Example 3A computer has RAM (Random Access Memory). RAM is a vital component of the computer, and it cannot exist independently. It is tightly coupled to the computer.A company has employees. Employees can work in multiple companies, and they exist independently.

In summary, composition represents a strong relationship where the parts are owned by the whole and have a strong dependency on the whole's lifecycle. In contrast, aggregation represents a weaker relationship where the parts can exist independently and have a looser connection to the whole.

object-oriented-programming
compositon
aggregation
difference