The use of an object by other objects is very common in POO. This behavior is classified as relation, whose behavior will be determined by specific classifications. Association, aggregation and composition are names used to classify the type of relations between objects and each of them will be explained below.
Objects that work by association use one another. This type of relation is the most common. Each one of them works independently, but associate with another one in order to perform a certain task. A driver of a car, for example. The driver and the car exist independently, but combine to make the car function.
In the aggregation, the behavior is slightly different. In this case, an object adds another object, such as a parent object. Thus, the parent object begins to utilize the features of the aggregate object. Still, both objects can exist independently. For example, a lid adds a pan, but the lid and the pan may still exist without depending on the existence of one another.
In the composition, the behavior changes completely. An object becomes dependent on the other to exist. The parent object is responsible for the creation and destruction of child object, which shall be responsible for the instance of its parts. In this example we can mention the wheels of the bike – the bike can only exist if the wheels exist.
These classifications help the interpretation of various diagrams, thereby allowing the reading and understanding to be facilitated.
In the class diagram, for example, those relations are marked in form of UML, thus allowing the reader to understand in few minutes all the logic that was used in the creation and implementation of objects. In the case, representation by lines indicates the type of relationship between objects:
- Straight line: indicates Association;
- Line with cutting of empty lozenge: indicates Agregation;
- Line with cutting of filled lozenge: indicates Composition.