Java 组合和聚合有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/813048/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:46:38  来源:igfitidea点击:

What is the difference between composition and aggregation?

javaooad

提问by ariso

What is the difference between composition and aggregation? can anybody give me a sample of this OOAD?

组合和聚合有什么区别?谁能给我一个这个 OOAD 的样本?

采纳答案by Ethan Heilman

Found here

在这里找到

"Both aggregation and composition are special kinds of associations. Aggregation is used to represent ownership or a whole/part relationship, and composition is used to represent an even stronger form of ownership. With composition, we get coincident lifetime of part with the whole. The composite object has sole responsibility for the disposition of its parts in terms of creation and destruction.

“聚合和组合都是特殊类型的关联。聚合用于表示所有权或整体/部分关系,而组合用于表示更强大的所有权形式。通过组合,我们可以获得部分与整体的一致生命周期。复合对象在创建和销毁方面全权负责其部分的处置。

Moreover, the multiplicity of the aggregate end may not exceed one; i.e., it is unshared. An object may be part of only one composite at a time. If the composite is destroyed, it must either destroy all its parts or else give responsibility for them to some other object. A composite object can be designed with the knowledge that no other object will destroy its parts.

而且,聚合端的多重性不得超过一;即,它是非共享的。一个对象一次只能是一个组合的一部分。如果复合体被摧毁,它必须要么摧毁它的所有部分,要么将它们的责任交给其他某个对象。可以设计一个复合对象,知道没有其他对象会破坏它的部分。

Composition can be used to model by-value aggregation, which is semantically equivalent to an attribute. In fact, composition was originally called aggregation-by-value in an earlier UML draft, with “normal” aggregation being thought of as aggregation-by-reference. The definitions have changed slightly, but the general ideas still apply. The distinction between aggregation and composition is more of a design concept and is not usually relevant during analysis." John Moore

组合可用于对按值聚合建模,这在语义上等同于属性。事实上,在早期的 UML 草案中,组合最初被称为按值聚合,“正常”聚合被认为是按引用聚合。定义略有变化,但总体思路仍然适用。聚合和组合之间的区别更多是一种设计概念,在分析过程中通常不相关。”约翰·摩尔

回答by pts

Relevant answer to your question here: http://ootips.org/uml-hasa.html

您的问题的相关答案在这里:http: //ootips.org/uml-hasa.html

In short: both composition and aggregation are acyclic has-a relationships consisting of a partand a whole. The difference is that with composition, the wholeis responsible for creating (and deleting) its parts.

简而言之:组合和聚合都是由部分整体组成的非循环 has-a 关系。不同之处在于,对于组合,整体负责创建(和删除)其部分s。

回答by chaos

Consider a student, the student's brain, and the school the student attends.

考虑一个学生、学生的大脑和学生就读的学校。

The brain is a part ofthe student. If the student is destroyed, so is the brain. This is composition.

大脑学生的一部分。如果学生被摧毁,大脑也会被摧毁。这是组成

The student has aschool. The student survives the school's destruction, and vice versa. This is aggregation.

学生有一所学校。学生在学校的毁灭中幸存下来,反之亦然。这就是聚合