关联(association)和依赖(dependency)的区别

《uml用户手册》(中文版)第10章,第97页:
“在语义上,包括泛化、关联和实现在内的所有关系都是某种依赖关系。”

 

第5章,46页:“其实聚合是一种特殊的关联。”

因此,

  • aggregation是一种association。
  • association是一种dependency。


association和dependency的区别: (《uml用户手册》)"给定一个连接两个类的关联,可以从一个类的对象导航到另一个类的对象,反之亦然。" 关联,必然是可以连到
一个完整的对象实例,而非MyClass::Foo()之类的单个操作。因此,association就只能实现为成员变量,或者A::getB(),如果是A::op(B b)就只能是dependency了

 

aggregation和association的区别:《UML Distilled 3rd ed.》上说,aggregation没什么实际语义。(“So the UML included aggregation, but with hardly any semantics. ”) 

而且,在建模时建议不要使用aggregation,因为不同的人对aggregation这一模糊概念常有不同的理解。("Aggregation is strictly meaningless; as a result, I recommend that you ignore it in your own diagrams. If you see it in other people's diagrams, you'll need to dig deeper to find out what they mean by it. Different authors and teams use it for very different purposes.")

那么,为什么UML还包含了aggregation呢?为什么UML meta model里没有明说association继承自dependency呢?请看下面的摘录:

UML Reference Manual:

The distinction between aggregation and association is often a matter of taste
rather than a difference in semantics. Keep in mind that aggregation is association.
Aggregation conveys the thought that the aggregate is inherently the sum of its
parts. In fact, the only real semantics that it adds to association is the constraint
that chains of aggregate links may not form cycles, which is often important to
know, however. Other constraints, such as existence dependency, are specified by
the multiplicity, not the aggregation marker. In spite of the few semantics attached
to aggregation, everybody thinks it is necessary (for different reasons). Think of it
as a modeling placebo

(placebo: 安慰剂, 某些没有内在补救作用的东西,仅仅用来安慰他人)

UML Reference Manual:

Note that association and generalization fit within the general definition of dependency, but they have their own model representation and notation and are not
usually considered to be dependencies.

UML User Guide:

Semantically, all relationships, including generalization, association, and realization,
are kinds of dependencies. Generalization, association, and realization have enough
important semantics about them that they warrant treatment as distinct kinds of
relationships in the UML. The stereotypes listed above represent shades of
dependencies, each of which has its own semantics, but each of which is not so
semantically distant from plain dependencies to warrant treatment as distinct kinds of
relationships. This is a judgment call on the part of the UML, but experience shows
that this approach strikes a balance between highlighting the important kinds of
relationships you’ll encounter and not overwhelming the modeler with too many
choices. You won’t go wrong if you model generalization, association, and realization
first, then view all other relationships as kinds of dependencies.
摘自http://old.blog.edu.cn/user2/zzulgq/archives/2006/1282712.shtml

 

 

重要的区别是:association意味着成员变量,而dependency是依赖的关系,以非成员变量的方式依赖着

共同的特点是:伸出箭头的类拥有被箭头指向类的知识(或者是其成员,或者用到了它)

摘自http://www.procedurelife.com/technical/JAVA/content/jchaa.html

 

 

其实,完全不必在乎于他们在哪里创建,在建模的这一阶段,重要的是理清他们的逻辑关系,如果两个类的关系是结构上的,他们就属于association范畴,而那些在函数调用时引用到的临时变量,不管他们在哪里创建他们都只是一种依赖,就像我们可能使用工厂(建筑类)的“执行建筑职能”这一方法制造某样产品(商品类),我们不太可能认为商品与工厂在结构上有什么联系吧。   
  另外,aggragation其实本身就是一种association,只是多了一种“整体/部分”的语义而已,如果两个类满足association,而我们又从逻辑关系中判断他们有整体/部分的关系,那他们就是一种aggregation.   

摘自http://topic.csdn.net/t/20031203/15/2522821.html

posted @ 2010-04-18 15:31  心梦帆影  阅读(1688)  评论(0编辑  收藏  举报