4

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.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
Dennis Grinch
  • 353
  • 3
  • 13
  • It's not `1 : 1` unless every country has exactly one airport. It's `1` country to `*` airports. I would guess that the diamond should be on the `Airport` side of the relationship (but wouldn't swear to it). – khelwood Feb 22 '17 at 16:55
  • 1
    An `<>` is a DataType, not a stereotyped class (though they look identical). In that sense you can not compose a class of an `<>`. This means that both diagrams are wrong. Where the 2nd is even "wronger" than the 1st. – qwerty_so Feb 22 '17 at 23:21

4 Answers4

7

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!

GhostCat
  • 137,827
  • 25
  • 176
  • 248
0

This would depend on the ultimate model you're trying to build.

If your model includes countries as first-class objects or entities then clearly a country contains airports. If Country is an attribute of an airport (more properly an attribute of the airport's location), then model it as such.

Unless you have a good reason to model countries as entities I'd go with the attribute, since borders can shift and airports can change countries.

In other words, this question doesn't have a definite answer, either can work depending on your ultimate goals.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
0

If I can suggest : cf. Section 9.5.3 of UML specification (march 2015) :

Composite : Indicates that the Property is aggregated compositely, i.e., the composite object has responsibility for the existence and storage of the composed objects (see the definition of parts in 11.2.3).

and

Composite aggregation is a strong form of aggregation that requires a part object be included in at most one composite object at a time. If a composite object is deleted, all of its part instances that are objects are deleted with it.

For me, the question of implementation and logical is more a question about db model. But maybe I am wrong.

And If I have to do a choice I will design the implementation case only.

granier
  • 1,739
  • 13
  • 25
0

You can say that airport is a part of a country or in other words is responsible for it. So from business perspective the second approach is correct. To indicate the "knowledge" about the country from airport perspective you can add an open arrow pointing in the country direction just before the diamond.

Regardless of the approach what exactly you want to model the first diagram is incorrect. With this diagram each airport will be responsible for one country so each airport will be in a different country.

Ister
  • 5,958
  • 17
  • 26