C#code的关联,聚合,组成 [英] C# code for association, aggregation, composition

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

问题描述

我想确认一下我的code会是什么样子的关联,聚合和放大器的理解;组成。所以这里去..

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

汇聚:有-A。它的的另一种类型的现有对象

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

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

组成:由另一个对象的

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

协会:我对这个两个视图。

Association: I have two views on this.


  1. 当一个类与另一个相关联。因此,上述两个都是联想的例子。

  2. 协会是聚集的弱形式,其中类不引用保持它接收的对象。

  1. When one class is associated with another. Hence both the above are examples of association.
  2. 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();
    }
}


我的理解是否正确?我看了冲突的文章这里和<一个href=\"http://www.$c$cproject.com/Articles/330447/Understanding-Association-Aggregation-and-Composit\">here所以,我真的不知道一个遵循的。我的理解似乎是在与第一链接线。我觉得第二个链接是错误的,或者也许我没有正确地理解它。

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.

你觉得呢?

推荐答案

聚集和组合物之间的区别是pretty模糊和容器被销毁之后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:


    包含四个轮式对象
  • A 对象。一般来说,如果你破坏了汽车(通过调用某些方法把它清理干净),你也应该销毁的过程中车轮,因为它没有什么意义了他们的车外存在(除非你将其移动到另一个汽车)对象。更为现实的,当它被关闭本身就是一个读者对象包装输入流也将关闭内在的流。这就是组成

  • A 对象包含(拥有)无线对象。如果死亡,无线可能被另一个被继承即它是有道理的,它没有原来的主人存在。更为现实的,拿着对象列表不要求列表本身处置时的所有对象得到处理。这就是汇聚

  • 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#code的关联,聚合,组成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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