用于关联、聚合、组合的 C# 代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12604031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# code for association, aggregation, composition
提问by user20358
I am trying to confirm my understanding of what the code would look like for association, aggregation & composition. So here goes..
我试图确认我对关联、聚合和组合的代码的理解。所以在这里..
Aggregation: Has-a. It hasan existing object of another type
聚合:Has-a。它有另一个类型的现有对象
public class Aggregation
{
SomeUtilityClass objSC
public void doSomething(SomeUtilityClass obj)
{
objSC = obj;
}
}
Composition: Is composed of another object
组成:由另一个对象组成
public class Composition
{
SomeUtilityClass objSC = new SomeUtilityClass();
public void doSomething()
{
objSC.someMethod();
}
}
Association: I have two views on this.
公会:对此我有两种看法。
- 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(); } }
- 当一个类与另一个相关联时。因此,以上都是关联的例子。
关联是一种较弱的聚合形式,其中类不保留对其接收的对象的引用。
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 hereand hereand 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.
我的理解正确吗?我在这里和这里阅读了相互矛盾的文章,所以我真的不确定要遵循哪一篇。我的理解好像和第一个链接是一致的。我觉得第二个链接是错误的,或者也许我没有正确理解它。
What do you think?
你怎么认为?
采纳答案by Tudor
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:
聚合和组合之间的区别非常模糊,AFAIK 与容器销毁后“子”对象的逻辑存在有关。因此,在聚合的情况下,容器内的对象在容器对象被销毁后仍然可以存在,而在组合设计的情况下,它们也被销毁。一些类比:
- A
Carobject containing fourWheelobjects. 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 anotherCarobject). More realistically, a reader object wrapping an input stream will also close the inner stream when it gets closed itself. This is composition. - A
Personobject contains (owns) aRadioobject. If thePersondies, theRadiomay be inherited by anotherPersoni.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.
- 一个
Car包含四个Wheel对象的对象。通常,如果您销毁汽车(通过调用某种方法将其清理干净),您也应该在此过程中销毁车轮,因为它们存在于汽车外部毫无意义(除非您将它们移到另一个Car物体上)。更现实的是,包装输入流的读取器对象也会在内部流自身关闭时关闭它。这是组成。 - 一个
Person对象包含(拥有)一个Radio对象。如果Person死了,它Radio可能会被另一个人继承,Person即它在没有原始所有者的情况下存在是有意义的。更现实的是,当列表本身被释放时,一个包含对象的列表并不要求所有的对象都被释放。这就是聚合。
Edit: After reading your links I'd be inclined to go with the first one, since it gives an explanation similar to mine.
编辑:阅读您的链接后,我倾向于使用第一个,因为它给出了与我类似的解释。
Also associationis merely invoking a method (sending a message) to another object via a reference to that object.
此外,关联只是通过对另一个对象的引用调用一个方法(发送消息)到另一个对象。
回答by Andritchi Alexei
For two objects Fooand Barwe have:
对于两个对象Foo,Bar我们有:
Dependency:
依赖:
class Foo
{
...
fooMethod(Bar bar){}
...
}
Association:
协会:
class Foo
{
private Bar bar;
...
}
Composition:(When Foodies so does Bar)
组成:(当Foo死亡也是如此Bar)
class Foo
{
...
private Bar bar = new Bar();
...
}
Aggregation:(When Foodies, Barmay live on)
聚集:(当Foo模具,Bar可以住)
class Foo
{
private List<Bar> bars;
}

