UML CompositionWhat is an UML composition and what insights does it provide?
UML composition – a special case of association
A composition in the Unified Modeling Language (UML) is a special case of association that describes a relationship between a whole and its existential parts. Put simply, in a composition, a part can never be larger than the whole.
An example: A building consists of rooms. The building is the whole, the rooms are parts of the whole. Since it is a concrete building, its multiplicity is 1. The multiplicity of the rooms can go from 1..*, so there is at least one room (without space no building), but there can be (at least in theory) infinitely many rooms.
From this example some aspects of a UML composition – also called Composite Aggregation – can be derived:
- The existence of an object or an instance cannot be dependent on two other objects/instances at the same time. A room belongs to one building and not to several buildings at the same time. In other words: the whole determines the life cycle of the parts. If the whole (i.e. the building) is removed, the individual parts (i.e. the rooms) disappear as well. This is called existential dependency.
- An object that is part of a whole can also switch to another whole or simply to independence. Although this is difficult to imagine in the example of the building space, there is the Kaisersaal in Berlin, for example, which was integrated into today’s Sony Center as an earlier part of the Grand Hotel Esplanade. Due to the integration into the Sony Center, the Kaisersaal is again dependent on its existence.
A composition is visualized with a line between the model elements and a filled diamond on the side of the whole.
Aggregation is another special form of association.
When implementing clean code, it is recommended to use compositions instead of inheritance. FCoI – Favour Composition over Inheritance is the name of the corresponding principle.
Here you can find more information on the Unified Modeling Language.
Here you will find additional information from our Smartpedia section: