UML - 协会或聚集(简单code段) [英] UML - association or aggregation (simple code snippets)

查看:188
本文介绍了UML - 协会或聚集(简单code段)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要把我逼疯了多少书违背自己。

I drives me crazy how many books contradicts themselves.

Class A {} class B {void UseA(A a)} //some say this is an association,
no reference is held but communication is possible
Class A {} class B {A a;} //some say this is
    aggregration, a reference is held

但很多人说拿着一提的是还只是一个关联和聚合,他们使用列表 - 恕我直言,这是一样的,它仍然参照

But many say that holding a reference is still just an association and for aggregation they use a list - IMHO this is the same, it it still a reference.

我很困惑,我想了解的问题。

I am very confused, I would like to understand the problem.

例如。在这里: http://aviadezra.blogspot.cz/2009/05 /uml-association-aggregation-composition.html - 什么是强关联和聚合的区别,在这两种情况下,笔者使用的字段存储的参考。

E.g. here: http://aviadezra.blogspot.cz/2009/05/uml-association-aggregation-composition.html - what is the difference between Strong Association and Aggregation, in both cases the author uses a field to store the reference..

又如:
这被认为是协

Another example: This is said to be Association:

和这被说成是Aggregration:

And this is said to be Aggregration:

public class Professor {
  // ...
}

public class Department {
  private List<Professor> professorList;
  // ..

}

再次有什么区别?它是在这两种情况下的参考

Again, what is the difference? It is a reference in both cases

推荐答案

这个问题一直是,而且会,问了很多次在许多不同的变种,因为很多人,包括许多高知名度的开发商,感到困惑的意义的这些术语,已在UML被定义。既然问题已经被问了很多次,也有人回答了很多次。见,例如<一href=\"http://stackoverflow.com/questions/734891/aggregation-versus-composition/27889087#27889087\">this回答。我会试着总结UML定义。

This question has been, and will be, asked many times in many different variants, because many people, including many high-profile developers, are confused about the meaning of these terms, which have been defined in the UML. Since the question has been asked many times, it has also been answered many times. See, e.g. this answer. I'll try to summarize the UML definitions.

两个类之间的关联不是通过方法参数建立的,而是通过引用属性(类属性),其范围/类型都是关联的类。如果一个方法参数的类型是一个类,这并不能建立一个协会,而是一个的依赖的关系。

An association between two classes is not established via a method parameter, but rather via reference properties (class attributes), the range/type of which are the associated classes. If the type of a method parameter is a class, this does not establish an association, but a dependency relationship.

这是必须理解协会第一的逻辑概念,看他们是如何codeD之前。对象类型之间的关联进行分类的那些类型的对象之间的关系。例如,联想委员会都具有一个 - ClubMember -as主席,这是可视化作为一条连接线如下图所示类图,可能的关系FinanceCommittee-有-PeterMiller,作为主席,分类RecruitmentCommittee-有-SusanSmith-作为主席和AdvisoryCommittee-HAS-SarahAnderson,作为主席,那里的对象PeterMiller,SusanSmith和SarahAnderson是类型 ClubMember 和对象FinanceCommittee,RecruitmentCommittee和AdvisoryCommittee的类型均为委员会

It's essential to understand the logical concept of associations first, before looking at how they are coded. An association between object types classifies relationships between objects of those types. For instance, the association Committee-has-ClubMember-as-chair, which is visualized as a connection line in the class diagram shown below, may classify the relationships FinanceCommittee-has-PeterMiller-as-chair, RecruitmentCommittee-has-SusanSmith-as-chair and AdvisoryCommittee-has-SarahAnderson-as-chair, where the objects PeterMiller, SusanSmith and SarahAnderson are of type ClubMember, and the objects FinanceCommittee, RecruitmentCommittee and AdvisoryCommittee are of type Committee.

这是一直使用EN作为参考性质,其范围/类型是关联的类方法codeD。举例来说,像这样

An association is always encoded by means of reference properties, the range/type of which is the associated class. For instance, like so

class Committee { ClubMember chair; String name;}

在UML,聚合和组合被定义为部分整体关系进行分类的本意协会的特殊形式。在聚合的情况下,相对于组合物,一个整体的零件可以与其它wholes共享。这在聚集,其中一门课程可以属于多个学位课程以下例子可以说明。

In the UML, aggregation and composition are defined as special forms of associations with the intended meaning of classifying part-whole-relationships. In the case of aggregation, as opposed to composition, the parts of a whole can be shared with other wholes. This is illustrated in the following example of an aggregation, where a course can belong to many degree programs.

的组合物的定义特征是具有独占(或不可共享)部分。的组合物,可能会与整体与部分暗示,当整个被摧毁,它的所有部件都与它破坏之间的生命周期的依赖。然而,这仅适用于组合物某些情况下,而不是其他人,因此,它不是一个限定特征。其中部件(组件)可从整体(复合)并因此生存它的破坏被分离的组合物的一个例子,如下:

The defining characteristic of a composition is to have exclusive (or non-shareable) parts. A composition may come with a life-cycle dependency between the whole and its parts implying that when a whole is destroyed, all of its parts are destroyed with it. However, this only applies to some cases of composition, and not to others, and it is therefore not a defining characteristic. An example of a composition where the parts (components) can be detached from the whole (composite) and therefore survive its destruction, is the following:

这篇关于UML - 协会或聚集(简单code段)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆