Thursday, 1 September 2016

Aggregation and composition

By,
Rahul

Aggregation and composition

 It is a form of association.
 It denotes a whole –part relationship between two classes. Here the whole part is called as
 ‘aggregate’ and the parts are called as ‘attributes’. It is possible to navigate between the
 aggregate and the attribute.
 It is denoted as follows:
 It can be interpreted as a form of physical containment where the attibute(s) is a part of the aggregate and can not survive without the aggregate. Thus the time lines of the attributes is within those of the aggregate and never independent.
Consider,
       

 It can be interpreted as a form of physical containment where the attibute(s) is a part of the aggregate and can not survive without the aggregate. Thus the time lines of the attributes is within those of the aggregate and never independent.
Consider,





 
In the above example a Car contains all the other class objects like the Wheel, Seats,Engine.
Without the presence of the whole i.e Car these attributes have no independent existence.Such type of associations can be also termed as Composition. The ‘aggregate ‘owns’ the ‘attributes’.
 Aggregation can also denote a ‘containment by reference’ association. Here the aggregate does not contain the attributes but only refers to those attributes.

   

Here ShareHolder is one class which contains zero or many references of Shares class. Here Shares class objects do not reside within the ShareHolder class and still these two classes share a whole/part relationship. This is an example of containment by reference.This is a type of ‘has –a ‘ relationship.

Comparison of Aggregation & Composition :



Aggregation
Composition
Class A contains Class B , Or Class A has instance of Class B.
Class A owns Class B.
Example:
Manager has N Employees for a project. 
Team has Players.
Example:
Order consists of LineItems / Order owns
LineItems
Body consists of Arm, Head, Legs.
BankAccount consists of Balance and
TransactionHistory.
An aggregation is used when life of object is independent of container object But still container object contains/has aggregated object.
Eg. Team has players, If team dissolve, Player will still exists.
A composition is used where each part may belong to only one whole at a time.
Eg. A line item is part of an order so A line item cannot exist without an order.
Aggregations are represented by the line with diamond.
Compositions are represented by the line with filled diamond.
Life of the aggregated objects are independent of each other, But one object is playing the role of Owner of the other object.
Life of the composite object is dependent
on the existence of container object,
Existence of composite object is not
meaningful without its container object.





No comments:

Post a Comment