关联、聚合和组合之间的区别是什么? 请从实施的角度加以说明。


当前回答

关联、聚合、组合

关联、聚合、组合都是有关系的。

聚合和组合是关联的子集,它们更准确地描述了关系

与聚合无关的关系。一个对象可以通过构造函数、方法、setter传递和保存在类中…

成分依赖关系。对象由所有者对象创建

关联是sybtyping的另一种选择

其他回答

对于Foo和Bar这两个对象,可以定义关系

关联——我与一个对象有一种关系。Foo使用Bar

public class Foo {         
    private Bar bar;
};

注意:请参阅Fowler的定义-关键是Bar在语义上与Foo相关,而不仅仅是一个依赖项(如int或字符串)。

组合——我拥有一个对象,我对它的生命周期负责。当Foo死了,Bar也死了

public class Foo {
    private Bar bar = new Bar(); 
}

聚合——我有一个从别人那里借来的对象。当Foo死了,Bar可以活下去。

public class Foo { 
    private Bar bar; 
    Foo(Bar bar) { 
       this.bar = bar; 
    }
}

这些答案的问题在于,它们只说了一半:它们解释了聚合和组合是关联的形式,但没有说一个关联是否可能不是这两种形式。

基于对SO和一些UML文档的一些简要阅读,我收集到类关联有4种主要的具体形式:

A是由A组成的;没有A, B就不存在,就像家里的房间一样 聚合:A有A B;B可以没有A而存在,就像教室里的学生一样 依赖:A使用B;A和B之间没有生命周期依赖关系,比如方法调用参数、返回值或方法调用期间创建的临时对象 泛化:A是A

当两个实体之间的关系不是其中之一时,它可以被称为一般意义上的“关联”,并以其他方式进一步描述(注意,原型等)。

我的猜测是,“通用关联”主要用于两种情况:

when the specifics of a relationship are still being worked out; such relationship in a diagram should be converted as soon as possible to what it actually is/will be (one of the other 4). when a relationship doesn't match any of those 4 predetermined by UML; the "generic" association still gives you a way of representing a relationship that is "not one of the other ones", so that you aren't stuck using an incorrect relationship with a note "this is not actually aggregation, it's just that UML doesn't have any other symbol we could use"

    Simple rules:
    A "owns" B = Composition : B has no meaning or purpose in the system 
    without A
    A "uses" B = Aggregation : B exists independently (conceptually) from A
    A "belongs/Have" B= Association; And B exists just have a relation
    Example 1:

    A Company is an aggregation of Employees.
    A Company is a composition of Accounts. When a Company ceases to do 
    business its Accounts cease to exist but its People continue to exist. 
    Employees have association relationship with each other.

    Example 2: (very simplified)
    A Text Editor owns a Buffer (composition). A Text Editor uses a File 
    (aggregation). When the Text Editor is closed,
    the Buffer is destroyed but the File itself is not destroyed.

正如其他人所说,关联是对象之间的关系,聚合和组合是关联的类型。

从实现的角度来看,聚合是通过引用类成员来获得的。例如,如果类A聚合类B的一个对象,你会有这样的东西(在c++中):

class A {
    B & element;
  // or B * element;
};

聚合的语义是,当对象A被销毁时,它所存储的对象B仍然存在。当使用组合时,你有一个更强的关系,通常通过按值存储成员:

class A {
    B element;
};

在这里,当一个A对象被销毁时,它所包含的B对象也将被销毁。最简单的方法是按值存储成员,但你也可以使用智能指针,或在析构函数中删除成员:

class A {
    std::auto_ptr<B> element;
};

class A {
    B * element;

    ~A() {
        delete B;
    }
};

重要的一点是,在组合中,容器对象拥有被包含的对象,而在聚合中,它引用它。

我知道这个问题被标记为c#,但概念是非常一般的问题,就像这里的这个重定向。所以我将在这里提供我的观点(从java的角度来看,我更舒服)。

当我们想到面向对象的本质时,我们总是想到对象、类(对象蓝图)以及它们之间的关系。对象之间通过方法相互关联和交互。换句话说,一个类的对象可以使用另一个类的对象提供的服务/方法。这种关系被称为关联。

聚合和组合是关联的子集,这意味着它们是关联的特定情况。

In both aggregation and composition object of one class "owns" object of another class. But there is a subtle difference. In Composition the object of class that is owned by the object of it's owning class cannot live on it's own(Also called "death relationship"). It will always live as a part of it's owning object where as in Aggregation the dependent object is standalone and can exist even if the object of owning class is dead. So in composition if owning object is garbage collected the owned object will also be which is not the case in aggregation.

困惑吗?

组合示例:以一辆汽车为例,它的发动机是这辆汽车特有的(意味着它不能用于任何其他汽车)。Car和SpecificEngine类之间的这种关系称为Composition。Car类的对象如果没有SpecificEngine类的对象就不能存在,而如果没有Car类,SpecificEngine的对象就没有意义。简单地说,Car类单独“拥有”SpecificEngine类。

聚合示例:现在考虑类Car和类Wheel。Car需要一个Wheel对象来运行。这意味着Car对象拥有Wheel对象,但我们不能说没有Car对象,Wheel对象就没有意义。它可以很好地用于自行车,卡车或不同的汽车对象。

总结一下

总而言之,关联是一个非常通用的术语,用于表示一个类使用另一个类提供的功能。如果一个父类对象拥有另一个子类对象,并且没有父类对象,这个子类对象就不能有意义地存在,我们就说它是复合。如果可以,则称为聚合。

详情请点击这里。 我是http://opensourceforgeeks.blogspot.in的作者,并在上面添加了相关帖子的链接以获取更多信息。