组合与聚合:包含对象的所有权在其他包含对象中重用

Composition vs. Aggregation: Ownership of the contained object being reused in other containing objects

我正在阅读 post Inheritance and Composition (Is-a vs Has-a relationship) in Java,我对以下陈述感到困惑。

It's also worth noting that in this case, the containing Computer object has ownership of the contained objects if and only if the objects can't be reused within another Computer object. If they can, we'd be using aggregation, rather than composition, where ownership isn't implied.

这是引用的代码片段

public class Computer{
    private Processor processor;
    private Memory memory;
    private SoundCard soundCard;
    //... more code
}

在另一个计算机对象中重复使用是什么意思?这是否意味着当我有两个 Computer 实例时,它们不应该共享那些组合对象(处理器、内存、声卡)?

有人可以举一个例子,在上面的陈述之后,使用聚合而不是组合吗?

问题来了,私有字段是如何实例化的?

Does it mean that when I have two instantiations of Computer that they shouldn't be sharing those composed objects...

是的。

如果一个 Computer 对象实例化它自己的 Processor 以便没有其他对象引用那个 Processor 实例,那么它 "has ownership of" 它的 Processor, 引述称其为 聚合.

将该场景与通过 getter 方法或构造函数参数接收其 ProcessorComputer 对象进行对比。在这种情况下,Processor 由其他某个对象实例化,并且 Computer 不拥有其 Processor 的唯一所有权,因为它没有对 [=11] 的唯一引用=] 对象。引用将此关系称为 composition.

在实践中,聚合和组合之间的区别并不重要。组合和继承之间的区别更为重要。