Can more than one objects aggregate or acquaint or instantiate the same object?

假装没事ソ 提交于 2019-12-20 07:32:23

问题


Design Pattern by Gamma et al said

Consider the distinction between object aggregation and acquaintance and how differently they manifest themselves at compile- and run-times.

Aggregation implies that one object owns or is responsible for another object. Generally we speak of an object having or being part of another object. Aggregation implies that an aggregate object and its owner have identical lifetimes.

Acquaintance implies that an object merely knows of another object. Sometimes acquaintance is called "association" or the "using" relationship. Acquainted objects may request operations of each other, but they aren't responsible for each other. Acquaintance is a weaker relationship than aggregation and suggests much looser coupling between objects.

Another useful thing to show is which classes instantiate which others. We call this the "creates" relationship. The arrow points to the class that's instantiated. In Figure c,

Can more than one objects aggregate the same object? If object A aggregate object B, can another object say C also aggregate B?

Can more than one objects acquaint the same object? If object A acquaint object B, can another object say C also acquaint B?

Can more than one objects instantiate the same object?


回答1:


To my understanding and the common definitions I know, Composition is an association where the lifetime of the associated objects is tied to the lifetime of the owner. Aggregation describes an association where the lifetime of the aggregated objects is independent from the lifetime of the owner. Acquaintance is the same as association.

According to this definition an object can be aggregated by multiple owners in an aggregate association. If the association is of type composition then sharing is by definition not intended.

Association or acquaintance are more generic description of a relationship between objects. So it depends on the details of the relationship if a single object can have multiple owners. But when being that generic, it is generally possible.

Wikipedia Object composition and aggregation

But I think there exists another definition which was introduced by Martin Fowler. It's the one you were quoting. To me this is the less known definition since the one that I rendered is defined in the UML rules which are widely spread and have become a de facto standard. According to M. Fowler's definition aggregation is equal to composition whereas acquaintance matches the UML definition of aggregation.

So the answer to your questions is, when looking through the eyes of M. Fowler: the rules for composition apply when talking about Fowler's aggregation which means multiple owners can't aggregate the same objects and in terms of acquaintance the rules of the in the UML defined aggregation association apply meaning that multiple owners can acquaint the same objects independently (from their lifetime).0

And of course you can create instances of an object as often as you want or as memory is available and where you want. The object or the class is only the definition whereas the instance (as a result of the instantiation) is the actual allocated memory area to store data of the object (e.g. fields). The class definition describes the layout of this allocated memory area.



来源:https://stackoverflow.com/questions/56478736/can-more-than-one-objects-aggregate-or-acquaint-or-instantiate-the-same-object

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