问题
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.
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.
回答1:
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. In other words: you pick the one that helps you solving your problem!
回答2:
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.
回答3:
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.
回答4:
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.
来源:https://stackoverflow.com/questions/42397555/composition-in-uml