Java 关联与聚合

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

Association vs. Aggregation

javaoopuml

提问by maks

I have reviewed a lot of information about these things, but can't understand what is the difference between them? In Fowler's UML Distilled says that Aggreagation is strictly meaningless, so author recommends not to use it in diagrams. Explain, please, when I should use each of them and how it will influence on java code.

我查阅了很多关于这些东西的资料,但不明白它们之间有什么区别?在 Fowler 的 UML Distilled 中,Aggregation 是严格没有意义的,因此作者建议不要在图表中使用它。请解释一下我什么时候应该使用它们中的每一个以及它将如何影响 java 代码。

采纳答案by khachik

This is a very arguable question. As Martin explains in the answer, the Order aggregates the Product. And this can be considered true. Grady Booch in his "Object-Oriented Analysis and Design" brings a similar example for association - a sale is associated with products in that sale, and vice versa. And a sale doesn't aggregate products. So all examples should be domain-specific, because from another point of view the association may become more specific. Another example is the composition of Documents using Paragraphs.

这是一个很有争议的问题。正如 Martin在回答中解释的那样,订单聚合了产品。这可以被认为是正确的。Grady Booch 在他的“面向对象的分析和设计”中提供了一个类似的关联示例——销售与该销售中的产品相关联,反之亦然。并且销售不会聚合产品。所以所有的例子都应该是特定领域的,因为从另一个角度来看,关联可能会变得更加具体。另一个例子是使用段落组合文档。

So everything in this field strongly depends on the context. This is the OOP.

所以这个领域的一切都在很大程度上取决于上下文。这就是面向对象。

You can try to apply your knowledge to a particular project you are going to design. I would recommend you to read Grady Booch's book, if you haven't done it yet. Lots of books have been written since it, but it is still the Bible of OO*.

您可以尝试将您的知识应用于您要设计的特定项目。我建议你阅读 Grady Booch 的书,如果你还没有读过的话。从那以后已经写了很多书,但它仍然是 OO* 的圣经。

回答by duffymo

There seems to be some debate on which word is which.

关于哪个词是哪个词似乎存在一些争论。

It has to do with parent-child relationships between objects and what happens to children when you delete the parent.

它与对象之间的父子关系以及删除父对象时子对象会发生什么有关。

One scenario says that children have no life outside that of their parent, so they should be deleted when the parent is deleted. Think "DELETE CASCADE" in foreign keys and relational databases.

一种情况是,孩子在父母之外没有生命,所以当父母被删除时,他们应该被删除。在外键和关系数据库中考虑“删除级联”。

The other scenario says that children should persist beyond their parents, so they should not be deleted when their parent is deleted.

另一种情况是孩子应该在他们的父母之外持久化,所以当他们的父母被删除时他们不应该被删除。

I'll leave it to others to argue which word describes each situation.

我将留给其他人来争论哪个词描述了每种情况。

回答by Martin Algesten

Association means two classes have some kind of relationship, could be anything really.

关联意味着两个类有某种关系,可以是任何东西。

Composition and aggregation are two kinds of associations. The easiest way to distinguish them is thinking about "how hard" the relationship is. Think about what happens when you delete the owner object.

组合和聚合是两种关联。区分它们的最简单方法是考虑这种关系“有多难”。想想删除所有者对象时会发生什么。

Aggregation, the aggregated object continues to live. (Think order <-> product, the product continues to live).

聚合,聚合后的对象继续存活。(认为​​订单 <-> 产品,产品继续存在)。

Composition, the aggregated object dies with the owner. (Think paragraphs <-> document, the paragraphs die with the document).

组合,聚合对象随所有者死亡。(认为​​段落 <-> 文档,段落随文档而死)。

An aggregation can be argued to be meaningless since there isn't really much difference between drawing a line with an non-filled arrow (association), and a line with a non-filled diamond (aggregation). The relations are very similar. Line with filled diamond (composition) is however very different.

聚合可以被认为是没有意义的,因为绘制带有非填充箭头的线(关联)和绘制带有非填充菱形的线(聚合)之间并没有太大区别。关系非常相似。然而,带有填充钻石(成分)的线条非常不同。

回答by Goran Jovic

Association is any relation between classes where instances of one class have a field reference to an instance of another class.

关联是类之间的任何关系,其中一个类的实例具有对另一个类的实例的字段引用。

Composition is a "stronger" relation, meaning that one instance (parent) "owns" another one (child).

组合是一种“更强”的关系,这意味着一个实例(父)“拥有”另一个(子)。

It is Aggregation that doesn't have any additional semantics other than being an association.

除了作为关联之外,聚合没有任何其他语义。

See more here: http://martinfowler.com/bliki/AggregationAndComposition.html

在此处查看更多信息:http: //martinfowler.com/bliki/AggregationAndComposition.html

EDIT: You may add some special semantics to aggregation symbol, like "May be owned by maximum one parent at a time, but may change parents, or be an orphan." However, such extensions are your own, and are not defined in UML, as far as I know.

编辑:您可以向聚合符号添加一些特殊语义,例如“一次最多可以由一个父代拥有,但可以更改父代,或者成为孤儿。” 但是,据我所知,此类扩展是您自己的,并没有在 UML 中定义。

回答by Kos

UML composition, aggregation and plain association are semantic concepts, not programming concepts. The meaning of them can be understood as follows:

UML 组合、聚合和普通关联是语义概念,而不是编程概念。它们的含义可以理解如下:

  • Composition: A consists of B; B is a part of A and hence cannot exist without A
  • Aggregation: A owns B, B belongs to A
  • Association: A uses B, A is related to B in a given way
  • 组成:A由B组成;B 是 A 的一部分,因此没有 A 就不能存在
  • 聚合:A拥有B,B属于A
  • 关联:A 使用 B,A 以给定方式与 B 相关

(Composition and Aggregation are special types of associations.)

(组合和聚合是特殊类型的关联。)

In Java, you may implement all of them in the same way. It's a conceptual difference.

在 Java 中,您可以以相同的方式实现所有这些。这是概念上的差异。

回答by pohchen

There are four kinds of Class relationships

Class关系有四种

  1. Association: uses a
    Ex:a Class Manuses a Class Pen( Pen is still there when man die )
  2. Aggregation: has a
    Ex:a Class Manhas a Class Car( Car is still there when Man die )
  3. Composition: owns a
    Ex:a Class Manowns a Class Heart( When Man die, Heart die )
  4. Inheritance: is a
    Ex:a Class Manis a Class Human( Man is a Human )
  1. 关联: 使用一个
    Ex:a 类Man使用一个类Pen(人死时笔还在那里)
  2. 聚合: 有一个
    例子:一个类Man有一个类Car(人死时汽车还在那里)
  3. 组成: 拥有一个
    Ex:一个类Man拥有一个类Heart(当人死,心死)
  4. 继承: is a
    Ex:a Class Manis a Class Human( Man is a Human )

A relationship between classes of objects

对象类之间的关系

Inheritance>Composition>Aggregation>Association

继承>组合>聚合>关联