What is Aggregation?
Aggregation is a special case of association when an object ‘has-a’ another object, which you can have an aggregation between them. The direction between them helps you to specify which object contains the other object. Aggregation is also known as a “Has-a” relationship. It is a subtype of an association relationship in UML.
What is Composition?
The composition is a special case of Aggregation that helps you to specify a whole-part relationship between the composition class and a subordinate (part) class.
- Aggregation is one type of association between two objects describing the “have a” relationship while Composition is a specific type of Aggregation which implies ownership.
- Aggregation is indicated using a straight line with an empty arrowhead at one end. On the other hand, the composition is indicated using a straight line with a filled arrowhead at any one of the ends.
- In an aggregation relationship, objects that are associated with each other can remain in the scope of a system without each other. But in a composition relationship, objects that are associated with each other cannot remain in the scope without each other.
- In Aggregation, linked objects are not dependent upon the other object whereas in composition, objects are highly dependent upon each other.
- In Aggregation, deleting a single element does not affect another associated element. On the contrary, in composition, deleting a single element affects another associated element.
- Aggregation is denoted by a filled diamond while composition is denoted by an empty diamond.
Aggregation vs Composition: Key statistics
|Owner||No owner||Single owner|
|Lifetime||Have their lifetime.||Owner’s lifetime.|
|Child Object||Child objects all are independent.||Child objects belong to a single parent.|
Example of Composition
The folder could contain many files, while each File has exactly one Folder parent. If a folder is deleted, all contained files are removed as well. In a composite aggregation, an object may be a part of only one composite at a time.
Example of Aggregation
For example, your car consists of wheels, engine, gearbox, steering, and the main body, etc. It is an assembly, and the other parts are its constituents.
Here, car to the wheel is one Aggregation, car to the engine is another aggregation, car to gearbox another, and so on.
This type of pairing helps you to define the multiplicity of the constituent part within the assembly as its outcome. The number of objects can also be depicted.
A car needs a wheel to function correctly. However, we cannot say the same with a car. The same logic can be applied to bike, bicycle, or any other vehicle but not a particular car.
Here, the wheel object is meaningful even without the car object. It is known as an aggregation relationship.
Difference between Aggregation and Composition
Here is the main difference between Aggregation and Composition:
|Aggregation is one type of association between two objects that is also describing the “have a” relationship.||Composition is a specific type of Aggregation which implies ownership.|
|Aggregation is indicated using a straight line with an empty arrowhead at one end.||The composition is indicated using a straight line with a filled arrowhead at any one of the ends.|
|Aggregation is a part of an association relationship.||The composition is a part of an association relationship.|
|Aggregation is considered as a weak type of association.||The composition is considered as a strong type of association.|
|In an aggregation relationship, objects that are associated with each other can remain in the scope of a system without each other.||In a composition relationship, objects that are associated with each other cannot remain in the scope without each other.|
|In Aggregation, linked objects are not dependent upon the other object.||In composition, objects are highly dependent upon each other.|
|In Aggregation, deleting a single element does not affect another associated element.||In composition, deleting a single element affects another associated element.|
|Example: A car needs a wheel, but it doesn’t always require the same wheel. A car can function adequately with another wheel as well.||Example: A file is placed inside the folder. If one deletes the folder, then the File associated with that given folder is also deleted.|
|In Aggregation, parent
relationship with the child entity.
|In composition, the parent entity owns the child entity.|
|It is denoted by a filled diamond||It is denoted by an empty diamond|
|Child does not have a lifetime.||Child can have a lifetime.|
|It is represented by a hollow diamond next to the assembly class.||It is represented by a solid diamond next to the assembly class.|
|The deletion of assembly never affects its parts.||In the case of owning a class, the object is deleted. It also affects the containing class object.|