Tabla de Contenidos
Aggregation is a type of association between classes that can be performed in various object-oriented programming languages, such as Java. It is a process that allows code to be reused quickly and efficiently, making programs much easier to write and running more efficiently.
Generally speaking, aggregation is referencing a class within another class. This creates what is called a HAS-A relationship between the containing class and the containing class. These relationships are characterized by being unidirectional, since they imply that one of the classes contains the other; this prevents the opposite from happening.
For example, we can say that a company, which can be seen from a programming point of view as an object class, has a set of employees; these, in turn, can also be seen as objects with their own attributes. In this case, the containing class is the company, while the containing class is the employees. This relationship only makes sense in this direction, that is, from the company to the employee. The relationship in the opposite direction does not make sense, since it would be like saying that the employee contains the company. The fact that this does not make sense shows that it is a one-way relationship.
Aggregation in class diagrams
In the Unified Modeling Language (UML), which is a type of universal visual language created for designing complex software systems, class diagrams are used to describe the structure and characteristics of a system. In these diagrams each class and its relationships with the others are represented by means of geometric figures. In the case of aggregation, this relationship is represented by a hollow diamond on the side of the containing class, which is connected by means of a straight line segment to the containing class. For example:
This is an aggregation relationship indicating that the Well class contains the Duck class.
How is aggregation implemented in Java?
The implementation of aggregation in Java is relatively simple. It’s just a matter of creating two classes and then referencing the first class inside the second.
In addition to establishing a “have a” or “have to” relationship, Java aggregation is characterized by a weak relationship between the container object and the content. This means that the destruction of one does not imply the destruction of the other. To understand this type of relationship, the relationship between a teacher and his students can be taken as an example. In this case, the disappearance or death of the teacher does not result in the disappearance or death of her students.
The opposite case would be the relationship between a human being and his heart. There is no doubt that the heart is contained in the human being, but this relationship is different from that of aggregation. This is because, in this case, both depend on each other. The destruction of the human being also destroys the heart, since to function it needs the nutrients and oxygenation that the body provides. On the other hand, the destruction of the heart also destroys the human being, since without a heart we cannot live (excluding the cases of transplants or other methods of artificial life support).
Aggregation Example
The following code exemplifies the implementation of aggregation as a form of association between two classes using Java. The example is about a program to keep track of which employees of a company have their own car and which do not, as well as which have bicycles and which do not.
Since we are looking to establish a relationship that indicates that the employee “has” a car and/or that the employee “has” a bicycle, it is an aggregation relationship between the class to which the employee will belong (which we will call Employee, for obvious reasons) and two additional classes, one for the car and one for the bicycle.
The first thing to do in these cases is to create the respective classes. In this case, there are three classes: one for the car, one for the bicycle, and one for the employee. The following figure shows the code for creating these classes, which are named Car, Bicycle, and Employee.
In the above code you can also see that each class has certain attributes associated with it. In the case of the car, it has as attributes the model, the color and the license plates, all associated with the car. The same happens with the bicycle, which is associated with a license plate number and the model.
When we look at the Employee class, we notice that this class has the attributes monbreemp , which represents the employee’s name, and empid, which represents the employee’s identification number; it then references the Car and Bike classes, creating the aggregation association between these classes. Finally, an employee’s record is created along with his car and bicycle.
Note that in this example, referencing the Car and Bike classes within the Employee class makes it possible to access the bike and car attributes from within the Employee class.
The output of this program would be:
________Employee Details________
Employee Name: Xavier
Employee Identification Number: 210921
________Employee car details________
Car model: Corolla
Car Color: Green
Car license plate: XFZ-043
________Employee bike details________
Bike Model: MERIDA
Bicycle Plates: LAA-11B
References
- Aggregation in Java Programming . (2020). ICHI.PRO. https://ichi.pro/es/agregacion-en-programacion-java-144087060492693
- Barquinero, JMM (2019). Aggregation Vs Composition in class diagrams. UML. | SEAS Blog . SEAS blog. https://www.seas.es/blog/informatica/agregacion-vs-composicion-en-diagramas-de-clases-uml/
- Software Mentoring. (2019). OOP8 – Object Oriented Programming in Java – UML Diagrams & OOP – Exercises [Video]. Youtube. https://www.youtube.com/watch?v=-Gy7Rf_wOeY