用于关联、聚合、组合的 C# 代码 [英] C# code for association, aggregation, composition

查看:33
本文介绍了用于关联、聚合、组合的 C# 代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图确认我对关联、聚合和代码的理解作品.就这样吧.

I am trying to confirm my understanding of what the code would look like for association, aggregation & composition. So here goes.

聚合:Has-a.它另一个类型的现有对象

Aggregation: Has-a. It has an existing object of another type

public class Aggregation
{
    SomeUtilityClass objSC
    public void doSomething(SomeUtilityClass obj)
    {
      objSC = obj;
    }
}

Composition:由另一个对象组成

public class Composition
{
    SomeUtilityClass objSC = new SomeUtilityClass();
    public void doSomething()
    {
        objSC.someMethod();
    }
}

协会:我对此有两种看法.

  1. 当一个类与另一个相关联时.因此,以上都是关联的例子.

  1. When one class is associated with another. Hence both the above are examples of association.

关联是一种较弱的聚合形式,其中类不保留对其接收的对象的引用.

Association is a weaker form of Aggregation where the class doesn't keep a reference to the object it receives.

public class Association
{
    //SomeUtilityClass objSC   /*NO local reference maintained */
    public void doSomething(SomeUtilityClass obj)
    {
       obj.DoSomething();
    }
}

我的理解正确吗?我在这里这里,所以我真的不确定要遵循哪个.我的理解好像和第一个链接是一致的.我觉得第二个链接是错误的,或者我可能没有正确理解它.

Is my understanding correct? I have read conflicting articles here and here and so I am really not sure which one to follow. My understanding seems to be in line with the first link. I feel the second link is wrong, or maybe perhaps I haven't understood it properly.

你怎么看?

推荐答案

聚合和组合之间的区别非常模糊,AFAIK 与容器销毁后子"对象的逻辑存在有关.因此,在聚合的情况下,容器内的对象在容器对象被销毁后仍然可以存在,而在组合设计的情况下要求它们也被销毁.一些类比:

The difference between aggregation and composition is pretty fuzzy and AFAIK relates to the logical existence of the "child" objects after the container is destroyed. Hence, in the case of aggregation the objects inside the container can still exist after the container object is destroyed, while in the case of composition design demands that they also get destroyed. Some analogies:

  • 一个 Car 对象,包含四个 Wheel 对象.通常,如果您销毁汽车(通过调用某种方法将其清理干净),您也应该在此过程中销毁车轮,因为它们存在于汽车外部毫无意义(除非您将它们移动到另一个 Car 对象).更现实的是,包装输入流的读取器对象也会在内部流自身关闭时关闭它.这是组合.
  • Person 对象包含(拥有)一个 Radio 对象.如果 Person 死了,Radio 可能会被另一个 Person 继承,也就是说,它在没有原始所有者的情况下存在是有意义的.更现实的是,当列表本身被释放时,一个包含对象的列表并不要求所有的对象都被释放.这是聚合.
  • A Car object containing four Wheel objects. Normally if you destroy the car (by calling some method to clean it up) you should also destroy the wheels in the process, since it makes little sense for them to exist outside the car (unless you move them to another Car object). More realistically, a reader object wrapping an input stream will also close the inner stream when it gets closed itself. This is composition.
  • A Person object contains (owns) a Radio object. If the Person dies, the Radio may be inherited by another Person i.e. it makes sense for it to exist without the original owner. More realistically, a list holding objects does not demand that all objects get disposed when the list itself is disposed. This is aggregation.

编辑:阅读您的链接后,我倾向于使用第一个,因为它给出的解释与我的相似.

Edit: After reading your links I'd be inclined to go with the first one, since it gives an explanation similar to mine.

此外,关联只是通过对另一个对象的引用调用一个方法(发送消息)到另一个对象.

Also association is merely invoking a method (sending a message) to another object via a reference to that object.

这篇关于用于关联、聚合、组合的 C# 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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