Do pointer/reference data members indicate an association or aggregaation relationship in UML?

自作多情 提交于 2019-12-08 06:36:15

问题


I'm a bit confused about which one it is.

You could say it 'uses' the object via the pointer - therefore association. But it also sounds like aggregation - 'has an' object but when this object dies that doesn't mean the referenced object dies.

Which one makes most sense in UML?


回答1:


It can indicate association, aggregation and event composition. The difference between the three is semantic and not static or implementation specific.

  1. If the pointed class has a lifecycle dependency with the owning class (a part of a whole that cannot exist without it, like entry classes in a hashmap) then it is a composition.
  2. If the pointed class is a part of the owning class (semantic relation, i.e. a database connection that is shared between controllers in an application) then it is an aggregation.
  3. If the pointer class is used for another thing (caching, intermediate values in some calculation, etc) then it is probably a simple association.

But UML leaves a LOT of space for different interpretations, so you will never get a "correct" answer here.




回答2:


First, an "aggregation relationship" is actually a (binary) association where one end has an aggregation kind other than "none". Then aggregations are just associations with refined semantics.

Aggregations can be composite (full diamond) or shared (hollow diamond). A composite aggregation means that "the composite object has responsibility for the existence and storage of the composed objects" (then the referenced object dies when the composite is deleted, but a part may be removed before the composite is deleted).

A shared aggregation, on the other hand, does not tie the lifecycle of the aggregated object (the UML specification says that "precise semantics of shared aggregation varies by application area and modeler" and "the precise lifecycle semantics of aggregation is a semantic variation point"). They are something in the middle between an unadorned association and a composite one.



来源:https://stackoverflow.com/questions/15285996/do-pointer-reference-data-members-indicate-an-association-or-aggregaation-relati

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!