实例化与聚合和相识有何不同? [英] How is instantiation different from aggregation and from acquaintance?

查看:90
本文介绍了实例化与聚合和相识有何不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实例化与聚合和相识有何不同? (我已经了解了聚合和相识之间的区别,请参见引用

How is instantiation different from aggregation and from acquaintance? (I have undertood the difference between aggregation and acquaintance, see the quote from the book here)

实例化和聚合似乎彼此相似。

Instantiation and aggregation seem similar to each other.

在我看来,实例化和聚合都不允许多个对象实例化或聚合同一对象。

我可能还想考虑其他方面

There might also be other aspects which I miss to consider

我已经问过,但是我对类之间的实例化关系还不是很清楚。

I have asked What does "a class instantiates another" mean?, but I am still not very clear about instantiation relation between classes.

推荐答案

实例化对象是创建对象实例的过程,由编译器执行。您可以根据需要进行实例化的次数,也可以在内存可用时进行实例化,因为实例化意味着分配内存。实例(作为实例化的结果)是实际分配的存储区,用于存储对象的所有数据(例如字段,变量)。类定义描述了此分配的内存区域的布局。另一方面,聚集以及相识描述了两个或更多对象的关系以及它们的生命周期如何相互链接。结识或聚集的关系是所有权。聚合是指当所有者实例被销毁(从内存中删除)时,拥有(聚合)实例也将被销毁。相识意味着当所有者实例被破坏时,所拥有的实例将继续存在于内存中。实例化通过分配实例被销毁时释放的内存来创建实例。

Instantiation of an object is the process of creating instance of an object and is executed by the compiler. You can instantiate as often as you want or as memory is available, since instantiation means allocation of memory. The instance (as a result of the instantiation) is the actual allocated memory area to store all data of the object (e.g. fields, variables). The class definition describes the layout of this allocated memory area. On the other hand aggregation, as well as acquaintance, describes the relation of two or more objects and how their life cycles are linked to each other. Relation in case of acquaintance or aggregation means ownership. Aggregation means when the owner instance is destructed (removed from memory) then the owned (aggregated) instance will be destructed as well. Acquaintance means when the owner instance is destructed, then the owned instance will continue to exist in memory. Instantiation creates an instance by allocating the memory that is later released when the instance gets destructed.

我们来看一所房子。也许我们在这里找到一些类比,可以帮助您更好地理解它:

Let's look at a house. Maybe we find some analogy here that can help you to understand it better:

类或对象就像一个由建筑师创建的施工计划(architec =开发人员)。该计划告诉建造者(编译器)如何建造房屋。成品房子是施工计划的体现,实例是对象(类)的表现。因此,实例仅意味着对象已创建,存在并处于活动状态。

the class or object is like a construction plan, that was created by an architect (architect = developer). This plan tells the builder (compiler) how to build the house. The finished house is the manifestation of the construction plan, and the instance is the manifestation of an object (the class). So instance only means an object is created, existent and alive.

房屋的所有实体(如楼梯和房间)也是根据建筑师的计划自行建造(实例化)的大小和位置的对象。这意味着房屋是由其他对象实例组成的对象实例。所有这些实例共存以创建建筑物,但是当仔细观察时,我们可以将这种共存描述为一种关系:房屋或房屋。我们甚至可以更仔细地研究这种关系,并问自己,当我们看房子时,这种关系的影响是什么,是否还存在其他类型的关系。建筑师本人马丁·福勒(Martin Fowler)发现并描述了两种不同的类型:

All the entities of a house like stairs and rooms are also objects on their own that were build (instantiated) in size and location according to the architect's plan. This means a house is an object instance that consists of other object instances. All those instances coexist to create the building, but when having a closer look we can describe this coexistence as a relationship: The house has rooms or it owns rooms. We can even look more closer at this relationship and ask our self what the effects of this relationship are and if also other kind of relationships do exist, when we are looking at the house. Martin Fowler, an architect himself, found and described two different kinds:

聚集是指当所有者(建筑物实例)被破坏时建筑物实例的房间实例被销毁的情况。毁了。一个房间实例实际上仅存在于一个建筑物实例(所有者或对象实例)中。

Aggregation is when the room instances of a building instance are destroyed when the owner (the building instance) is destroyed. A room instance actually exist only in one building instance (owner or object instance).

相识是指建筑物被毁时房客还活着的时候。他将搬到另一栋大楼租一间公寓。租户可以在不同的房屋中租用多个地方。租户是与房屋相关的另一个实体或对象实例,而租户的DNA是对象或类,创建者的施工计划告诉他该实例的外观以及实例化后他将具有的属性(出生)。

Acquaintance is when the tenant is still alive when the building is destroyed. He would move to another building to rent a flat. The tenant can rent multiple places in different houses. A tenant is another entity or object instance that is related to the house, whereas the tenant's DNA is the object or class, it's the creator's construction plan that tells him how the instance has to look like and what attributes he will have once instantiated (born).

这篇关于实例化与聚合和相识有何不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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