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

后端 未结 1 1728
小蘑菇
小蘑菇 2021-01-25 23:39

Design Pattern by Gamma et al said

Consider the distinction between object aggregation and acquaintance and how differently they manifest themselves at co

相关标签:
1条回答
  • 2021-01-26 00:27

    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.

    0 讨论(0)
提交回复
热议问题