When we have only one relationship between objects, that is called Association. Aggregation and Composition both are specialized form of Association.
Composition is again specialize form of Aggregation.
Association is a relationship where all object have their own life-cycle and there is no owner. Let’s take an example of Teacher and Student. Multiple students can associate with single teacher and single student can associate with multiple teachers but there is no ownership between the objects and both have their own life-cycle. Both can create and delete independently.
Aggregation is a specialize form of Association where all object have their own life-cycle but there is ownership and child object cannot belongs to another parent object. Let’s take an example of Department and teacher. A single teacher cannot belongs to multiple departments, but if we delete the department teacher object will not destroy. We can think about “has-a” relationship.
Composition is again specialize form of Aggregation and we can call this as a “death” relationship. It is a strong type of Aggregation. Child object does not have their life-cycle and if parent object deletes all child object will also be deleted. Let’s take again an example of relationship between House and rooms. House can contain multiple rooms there is no independent life of room and any room cannot belongs to two different house if we delete the house room will automatically delete. Let’s take another example relationship between Questions and options. Single questions can have multiple options and option can not belong to multiple questions. If we delete questions options will automatically delete.
Here is the list of differences between Composition and Aggregation in point format, for quick review.The key difference between them comes from the point that in case of Composition, One object is OWNER of other object, while in case of aggregation, one object is just a USER or another object.
1) If A and B two classes are related to each other such that, B ceased to exist, when A is destroyed, then association between two object is known as Composition. Example is Car and Engine. While if A and B are associated with each other, such that B can exist without being associated with A, then this association in known as Aggregation.
2) In case of Composition A owns B e.g. Person is owner of his Hand, Mind and Heart, while in case of Aggregation, A uses B e.g. Organization uses People as employee.
3) In UML diagram Association is denoted by normal arrow head, while Composition is represented by filled diamond arrow head, and Aggregation is represented by empty diamond arrow head, As shown in below and attached diagram in third paragraph.
Association A---->B
Composition A-----B
Aggregation A-----<>B
4) Aggregation is a lighter form of Composition, where sub-part object can meaningfully exits without main objects.
5) In Java, you can use final keyword to represent Composition. Since in Composition, Owner object expect part object to be available and functions, by making it final, your provide guarantee that, when Owner will be created, this part object will exist. This is actually a Java idiom to represent strong form of association i.e. composition between two objects.
6) Another interesting word, which comes handy to understand difference between Composition and Aggregation in software design is "part-of" and "has". If one object is part-of another object e.g. Engine is part of Car, then association or relationship between them is Composition. On the other hand if one object just has another object e.g. Car has driver than it's Aggregation.
That's all on difference between Association, Composition and Aggregation in UML, Java and Object oriented design. Since object oriented analysis is more about defining relationship between object, it's important to know what kind of relationship exists between them, composition and aggregation is a two way of representing relationship between two objects.
1) If A and B two classes are related to each other such that, B ceased to exist, when A is destroyed, then association between two object is known as Composition. Example is Car and Engine. While if A and B are associated with each other, such that B can exist without being associated with A, then this association in known as Aggregation.
2) In case of Composition A owns B e.g. Person is owner of his Hand, Mind and Heart, while in case of Aggregation, A uses B e.g. Organization uses People as employee.
3) In UML diagram Association is denoted by normal arrow head, while Composition is represented by filled diamond arrow head, and Aggregation is represented by empty diamond arrow head, As shown in below and attached diagram in third paragraph.
Association A---->B
Composition A-----
Aggregation A-----<>B
4) Aggregation is a lighter form of Composition, where sub-part object can meaningfully exits without main objects.
5) In Java, you can use final keyword to represent Composition. Since in Composition, Owner object expect part object to be available and functions, by making it final, your provide guarantee that, when Owner will be created, this part object will exist. This is actually a Java idiom to represent strong form of association i.e. composition between two objects.
6) Another interesting word, which comes handy to understand difference between Composition and Aggregation in software design is "part-of" and "has". If one object is part-of another object e.g. Engine is part of Car, then association or relationship between them is Composition. On the other hand if one object just has another object e.g. Car has driver than it's Aggregation.
That's all on difference between Association, Composition and Aggregation in UML, Java and Object oriented design. Since object oriented analysis is more about defining relationship between object, it's important to know what kind of relationship exists between them, composition and aggregation is a two way of representing relationship between two objects.
No comments:
Post a Comment