Aggregation and composition are the types of Association. The primary difference between aggregation and composition is that aggregation implicit a relationship where the child can exist independently of the parent. On the other hand, composition insinuates a relationship where the child cannot exist independent of the parent.
Object-Oriented programming is a common paradigm which is used in software development. The object is an instance of a class which are created by classes. The objects collaborate with each other through Links. Links are the physical and conceptual connection exists between objects. A collection of links having similar structure and semantics is described as an association.
Content: Aggregation Vs Composition
|Basis for comparison||Aggregation||Composition|
|Basic||In aggregation there exhibit a relationship where a child can exist independently of the parent.||In composition the cannot exist independently of the parent.|
|Type of relationship||"has a"||"part of"|
|Association type||Weak association||Strong association|
|UML design symbol||Represented by a hollow diamond next to assembly class.||Represented by a solid diamond next to assembly class.|
|Function||The deletion of assembly doesn't affect its parts.||If the owning class object is deleted, it could significantly affect the containing class object.|
Definition of Aggregation
Aggregation can be defined as a relationship with an assembly class to constituent class. The relationship between two objects can be represented by drawing a line segment in UML (Unified Modelling Language). Aggregation is also used to specify the multiplicity of objects such as one-to-one, one-to-many and many-to-many. When an object of class X is associated with a single object of class Y, in that case there exist one-to-one relationship. Similarly, if a single object of a class is associated with multiple objects of a class, then this relationship is referred to as one-to-many.
For example, a car consists of wheels, engine, gearbox, steering, main body, etc. A car is an assembly, and the other parts are its constituents. Car to wheels is one aggregation, car to engines is another aggregation, and so on. Each individual pairing is defined to specify the multiplicity of each constituent part within the assembly as an outcome the number of objects can also be depicted.
The aggregation is a type of association which describes a “has a” type of relationship between the objects. For instance, a car “has a” gearbox, and a car “has an” engine. For the one-to-many relationship, an example is a car “has” many wheels.
Definition of Composition
The composition is also a type of association but a more restrictive form. It is represented in UML by a tiny solid diamond adjacent to the assembly class. In this type of association, the constituent part can be linked to at most one assembly. Additionally, when a constituent part collaborates with assembly, it will have the concurrent lifetime with the assembly.
Thus, composition signifies ownership of the constituent part of the whole. This would ultimately increase the convenience for the programming. The composition can trigger the deletion of the constituent object by the deletion of an assembly object.
The composition describes a “part of” relationship. For example, a leaf is a part of a tree, if the tree is destroyed, then leaves are must be destroyed.
Key Differences Between Aggregation and Composition
- Aggregation indicates a relationship where a child and parent entities can exist independently. As against, in composition, the child entity is dependent on the parent.
- In aggregation there exist a “has a” relationship whereas in composition there is a “part of” relationship between the assembly and constituent class objects.
- Aggregation is weak association while the composition is a strong association because it has more constraints.
- In UML design the aggregation is described by a hollow diamond. In contrast, the composition is pictured by a solid diamond.
In aggregation, the constituent parts have the independent existence and can be shared or reassigned to aggregates or its assembly. On the other hand, in composition, the constituent part belongs to exactly one assembly or aggregate. Furthermore, the part is created when the aggregate is formed, and the part is destroyed when the aggregate is destroyed.