Aggregation in object orientation is a kind of association which relates two objects by ‘part-of whole’ relationship that’s why it is possible only in a binary association. Aggregation is a ‘has-a’ relationship between two class like a car has a wheel, an engine has a gearbox and so on.
Discovering aggregation in a class model is just a matter of judgement. In this section, we will discuss what is aggregation in object orientation and how we can figure out the aggregation in the class model.
Content: Aggregation in Object Orientation
- What is Aggregation?
- Comparison with Association
- Comparison with Composition
- Propagation of Operation
- Key Takeaways
What is Aggregation?
Aggregation is a kind of association that is used to establish a relationship between the assembly class and one constituent part class. Aggregation is a binary association where one end of the association is aggregate and the other end is constituent.
Aggregation is the ‘has-a’ relationship. An aggregate object is derived from the several lesser constituent parts where each constituent is a part of the aggregation. We can refer an aggregate object as an extended object which is operated as a single unit though it is derived from the several constituent objects.
The aggregation is denoted by the straight line with the empty arrow by the side of assembly class.
Let us understand this above theory with the help of an example. Let’s say we have an assembly class Car and the constituent classes Wheel, Engine, Seat.
For a Car has wheels, an engine, seats. Here the class Car is an assembly class and the others are the constituent part classes. Each individual pairing i.e. Car to Wheel is one aggregation relation, Car to Engine is an aggregation relation and Car to Seats is an aggregation relation.
This concludes that aggregation is a binary association. Let us discuss some interesting properties of aggregation.
- Aggregation is transitive which means if A belongs to B and B belongs to C then we can say that A belongs to C. For example, the gearbox is a part of engine and engine is a part of car then gearbox is the part of the car.
- Aggregation is antisymmetric i.e. when we say that A is part of B it doesn’t mean that b is also a part of A. For example, a car has an engine but we can not say that engine will also behave like a car.
It can also be put up like this, aggregation is unidirectional which means only one end of the association can be marked as aggregation.
Comparison with Association
Association simply defines the relationship between two objects and classes in the class model. We have discussed association in brief in our previous content, you can overview the topic. If two objects are related using the part-whole relationship then the relation is aggregation.
If two objects are bounded by relation and even are independent of each other then it is an association. To decide whether the relation between the classes must be aggregation or association you must ask the following questions to the classes in the model.
- Whether a class is a part of another class?
- Whether an operation performed on the whole class gets automatically applies to the constituent classes?
- Do the attribute values from the whole class propagate to the constituent classes?
- Whether there is an asymmetry to the association?
When you discover the answers for these questions you will be able to realize the use of aggregation. Aggregation include part-of relationship, discovering aggregate object incorporating the constituent parts. While modelling one must not compel the association to be modelled as aggregation it is a matter of judgement.
If you exercise the judgement carefully and distinguish between aggregation and association then it is not a problem to figure out aggregation and association separately.
Comparison with Composition
The aggregation and composition both are the part-of relationship and when explored in deep we observe that the aggregation is weak ‘part-of’ relationship and composition is a strong and restrictive ‘part-of’ relationship.
The composition is similar to aggregation with two restrictions. The first restriction is that the constituent part class must be related at most one assembly class. The second restriction is that once the constituent part class is related to the assembly class its existence depends upon the assembly class lifetime.
Thus, the composition is an ownership of the constituent part by the assembly class. Well, this eases the programming as the deletion of the assembly class object invokes the deletion of constituent objects also.
Like in the example given below, just note that the composite object of company class would be derived from constituent objects of division and department constituent part classes. Now, if the object of the company class is deleted then constituent objects of division and department class will also be affected.
While modelling the composition is denoted same as aggregation i.e. a straight line with the arrowhead but the difference is that the aggregation has empty arrowhead and composition has solid filled arrowhead to the side of assembly class.
Whereas the deletion of an assembly class object does not affect the object of the constituent part class. For example, if the object of the car class is deleted it does not affect the existence of wheel class object.
Propagation of Operation
Propagation of operation means whenever the operation is performed on the object of assembly class it triggers the operations of constituent part classes objects. This propagation of operation from starting object to the parts objects signals aggregation.
- Aggregation is a kind of association i.e. relationship between the objects and classes.
- Aggregation is the ‘has-a’ relationship.
- Aggregation can only occur in binary association.
- Multiplicity can be specified in aggregation by defining the individual pairing between a constituent class and assembly class.
- An aggregate object is derived from several constituent objects.
- Aggregation is transitive and asymmetric.
- If the two objects are bounded by a ‘part-of‘ relationship then it is aggregation.
- The aggregation is denoted by the straight line with an empty arrowhead to the side of assembly class.
- Deletion of an assembly class object does not affect the objects of the constituent part class.
- Propagation of operations from the assembly class object to the constituent class objects denotes aggregation.
So, this is all about the aggregation in object orientation which indicates the relationship between two objects in a class model where the relationship is a ‘part-of’ relationship.