25 Oct 2023
Beginner
| Aspect | Composition | Aggregation |
|---|---|---|
| Definition | A "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 Strength | Strong relationship - parts are tightly coupled to the whole. | Weaker relationship - parts can exist independently of the whole. |
| Ownership | The 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 Dependency | Strong dependency - parts' lifecycles are tied to the whole. | Weaker dependency - parts have their own lifecycles and can be created and destroyed independently. |
| Example | A 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 UML | Shown 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. |
| Multiplicity | Typically, 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:
| Concept | Composition Example | Aggregation Example |
|---|---|---|
| Definition | Composition 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 1 | A 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 2 | A 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 3 | A 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.