简体   繁体   English

Aggregation和Association在实现上的区别

[英]The difference between Aggregation and Association in implementation

I have read a lot of theory regarding object relationshisps, and I am still having difficulty understanding how are association and aggregation seperated from implementaion point of view .我已经阅读了很多关于 object 关系的理论,但我仍然很难从实现的角度理解关联和聚合是如何分离的。 In both cases you will have object B as a data member in object A, where it is present there as a reference,(unlike with composition where it exists there by value).在这两种情况下,您都会将 object B 作为 object A 中的数据成员,在那里它作为参考存在(与按值存在的组合不同)。 So what is the difference between the two cases really?那么这两种情况有什么区别呢? I have read somewhere that some Java gurus consider aggregation a solely abstract concept, a "placebo" case that can not be told apart (from association) from implementation/syntax point of view, is that right or have I missed something?我在某处读到一些 Java 大师认为聚合是一个完全抽象的概念,一个“安慰剂”案例,从实现/语法的角度无法区分(从关联),是对的还是我错过了什么?

I agree that from the implementation point of view both association and aggregation look the same - like you mentioned, in both cases one of the objects is a data member in the other.我同意从实现的角度来看,关联和聚合看起来都是一样的——就像你提到的那样,在这两种情况下,其中一个对象是另一个对象中的数据成员。

The way I understand this is that the implementation difference that you are asking about does not happen at the level of the object, but rather at the level of the application design:我理解这一点的方式是,您所询问的实现差异不会发生在 object 级别,而是发生在应用程序设计级别:

  • If by implementation difference you understand the code itself (the way the object is placed within another), then there is no difference.如果通过实现差异您了解代码本身(object 放置在另一个中的方式),那么没有区别。

  • But if we extend the conversation to how the objects are used within the application, then we need to start looking at whether the objects are self sufficient or not, whether they can serve a unique, independent function or not.但是,如果我们将对话扩展到应用程序中如何使用对象,那么我们需要开始研究对象是否自给自足,它们是否可以提供独特的、独立的 function。 It is for you to decide whether this is still implementation由您决定这是否仍然是实施

Edit -> additional explanation added below:编辑->下面添加的附加说明:

I might have not been clear enough - what I meant was that in this case the implementation could be considered on two levels:我可能还不够清楚——我的意思是在这种情况下,可以在两个层面上考虑实现

  • the code that represents the object within the class (the field holding the reference to the object)代表 class 中的 object 的代码(包含对对象的引用的字段)

  • the wider code (how the object is used in other classes or how the dependencies between objects are represented)更广泛的代码(如何在其他类中使用 object 或如何表示对象之间的依赖关系)

Both of those could be understood as implementation , but on different levels of abstraction - the usage within the class is the same for both Aggregation and Composition , yet the way the object relationships are implemented across multiple classes would differ.两者都可以理解为implementation ,但是在不同的抽象级别上 - class 中的用法对于AggregationComposition是相同的,但是 object 关系在多个类中实现的方式会有所不同。

There is normally no difference in the implementation of an aggregation versus an association because their semantic difference is normally not relevant in the code of an app.聚合与关联的实现通常没有区别,因为它们的语义差异通常与应用程序的代码无关。

An aggregation is a special form of association with the intended meaning of a part-whole-relationship , where the parts of a whole can be shared with other wholes.聚合是一种特殊形式的关联,具有部分-整体-关系的预期含义,其中整体的部分可以与其他整体共享。 For instance, we can model an aggregation between the classes DegreeProgram and Course , as shown in the following diagram, since a course is part of a degree program and a course can be shared among two or more degree programs (eg an engineering degree could share a C programming course with a computer science degree).例如,我们可以 model 类之间的聚合DegreeProgramCourse ,如下图所示,因为课程是学位课程的一部分,并且课程可以在两个或多个学位课程之间共享(例如,工程学位可以共享具有计算机科学学位的 C 编程课程)。

在此处输入图像描述

Modeling the special relationship between DegreeProgram and Course in this way conveys some intended meaning, but does not have to be, and is typically not, reflected in the implementation code, which may look as follows:以这种方式对DegreeProgramCourse之间的特殊关系进行建模传达了一些预期的含义,但不一定,通常也不会反映在实现代码中,可能如下所示:

class DegreeProgram {    
  private List<Course> courses;
  ...
}

To be correct: the UML term aggregation (you probably are referring) is composite aggregation .正确地说:UML 术语聚合(您可能指的是)是composite aggregation P. 110 of UML 2.5 says: UML 2.5 的第 110 页说:

composite - Indicates that the Property is aggregated compositely, ie, the composite object has responsibility for the existence and storage of the composed objects (see the definition of parts in 11.2.3). composite - 表示属性是复合聚合的,即复合 object 负责组合对象的存在和存储(参见 11.2.3 中的部件定义)。

In case you mean a shared aggregation, see the same p.如果您的意思是共享聚合,请参见相同的 p。 110: 110:

shared - Indicates that the Property has shared aggregation semantics. shared - 表示该属性具有共享聚合语义。 Precise semantics of shared aggregation varies by application area and modeler.共享聚合的精确语义因应用领域和建模者而异。


tl;dr tl;博士

The difference is quite simple.区别很简单。 A composite (aggregate) object must be destroyed if the aggregating object says good bye.如果聚合 object 说再见,则必须销毁复合(聚合)object。 An associated oject does not care (or the referring object will not try to kill it).关联的对象不关心(或引用 object 不会尝试杀死它)。

For a shared aggregation: invent your own definition and publish it along with its usage.对于共享聚合:发明您自己的定义并将其连同其用法一起发布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM