一个以上的对象可以聚合或认识或实例化同一个对象吗? [英] Can more than one objects aggregate or acquaint or instantiate the same object?

查看:93
本文介绍了一个以上的对象可以聚合或认识或实例化同一个对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Gamma等人的设计模式

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.

另一个值得展示的事情是哪个类实例化了其他类。我们称此为
创建关系。箭头指向实例化的类。在图c中,

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,

多个对象可以聚合同一对象吗?
如果对象A聚合了对象B,那么另一个对象也可以说C也聚合了B吗?

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

可以有多个对象认识同一个对象吗?
如果对象A认识对象B,另一个对象也可以说C也认识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?

推荐答案

根据我的理解和我所知道的常见定义, Composition 是一种关联关系,其中关联对象的生命周期受到限制直到拥有者的一生。 Aggregation 描述了一种关联,其中聚合对象的生存期独立于所有者的生存期。 相识关联相同。

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.

根据此定义,一个对象可以由多个所有者聚合。 aggregate 关联。如果关联的类型为 composition ,则按定义不打算共享。

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 相识是对象之间关系的更一般的描述。因此,如果一个对象可以有多个所有者,则取决于关系的细节。但是,如果使用这种通用名称,通常是可能的。

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对象的组成和聚合

但是我认为,存在另一个由Martin Fowler引入的定义。这是您所引用的。对我而言,这是鲜为人知的定义,因为我提供的定义是在UML规则中定义的,而UML规则已广泛传播并已成为事实上的标准。根据M. Fowler的定义, aggregation 等于 composition ,而 agraintance 匹配UML定义 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.

因此,您的问题的答案是,从福勒先生的眼睛看:组成的规则适用于谈论福勒的 aggregation ,这意味着多个所有者不能按照相识 UML定义的 aggregation 关联的应用意味着多个所有者可以独立地(从生命周期开始)熟悉相同的对象。0

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.

这篇关于一个以上的对象可以聚合或认识或实例化同一个对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆