Search code examples
javauml

Composition in UML


In UML diagrams when considering composition. Should we use it in logical or implementation sense. Examples for both terms:

  • Implementation - An airport will contain a reference to the country. In other words, a country is part of the Airport.
  • Logical - A country can have zero or many airports. In other words, an airport is part of the country.

UML example diagram

From diagram above, which case shows correct usage of composition? NOTE: if neither of these cases are good, please suggest other ways to show relationship between country and airport.


Solution

  • I think that this is not a composition in the strong "UML sense" of that word.

    From wikipedia:

    The relationship between the composite and the component is a strong “has a” relationship, as the composite object takes ownership of the component. This means the composite is responsible for the creation and destruction of the component parts.

    An Airport does not create countries (and in IT sense, a "country" object is also not responsible for providing/creating "airport" objects).

    In that sense, you are looking towards an association here; and I think the first one fits better (talking in "general"). But the core aspect is: your model has to express the specific requirements of your domain. In other words: both solutions are valid; it very much depends on the context which one to choose. So, pick the one that helps you solving your problem!