UML中關聯(association)和依賴(dependency)的區別

這個問題不僅我們這些小嘍羅會迷惑,很多大拿們也沒有統一的認識,爭論常有。
在UML2.0 規範中,有些以前歸爲一方的現在被歸爲另一方。

下面是從網上搜集的一些看法:
---------
1.
---------
Dependency Relationship

Draw a dependency relationship between two classes, or between a class and an interface, to show that the client class depends on the supplier class/interface to provide certain services, such as:

?The client class accesses a value (constant or variable) defined in the supplier class/interface.

?Operations of the client class invoke operations of the supplier class/interface.

?Operations of the client class have signatures whose return class or arguments are instances of the supplier class/interface.

A dependency relationship is a dotted line with an arrowhead at one end:The arrowhead points to the supplier class.



Association Relationship

An association provides a pathway for communication. The communication can be between use cases and actors, between two classes or between a class and an interface. Associations are the most general of all relationships and consequentially the most semantically weak. If two objects are usually considered independently, the relationship is an association

---------
2.
---------
Martin Fowler

If you have an association from Class A to Class B then that means every instance of class A has some kind of link to class B. Now exactly what people mean by "some kind of link" varies, it may be a conceptual link, a method of the form getA or a field inside class A. But the usual notion is that the link exists at all times (although if the lower bound is 0 it may be empty). This link can be used by any method of A and, if exposed, by other classes too.

With a parameter the connection between A and B only exists within the scope of the method that took the parameter. No other method can use the connection. As such that, for most people I come across, means that it is not an association.

The dependency means that if you change the interface of B then you may have to change A. An association usually implies a dependency, but not vice-versa, as the parameter example suggests.

---------
3.
---------
Robert C. Martin
However, association is not free of semantics. An association is a data relationship. i.e. the implementation must use some sort of data variable to implement it. Typically this is done with some kind of member variable or instance variable that refers to the associate. In C++ we might create a pointer variable, in Java a reference variable. Or we might use some kind of string that represents the associate in a dictionary somewhere.

In order for a message to be send between two objects, an association must exist between their two classes; and that association must be navigable in the direction that the message was sent.

---------
4.
---------
quote:
>What is the difference between Association and Dependency?

>My understanding so far was:

>If an object of class A has a reference to an object of class B in its
>attribute structure, it is association.

Robert C. Martin's answer:

Er, well, uh -- hmmm.

That's as good a definition as any I suppose. There are so many different colloquialisms and slangs for UML that I guess it hardly matters any more.

There was a time when an association was the channel between classes over which messages were sent. No message, no association. But that rule has been relaxed by so much conventional usage that I don't think it can hold any more.

Nowadays we seem to use association and aggregation interchangeably to represent a data field (except, of course, that everybody has their own private definition for what the white diamond means.) Dependency if very commonly used to represent an argument passed to a function. This was not the original intent of UML, but it's the way things have turned out.

There was a time when dependency meant that a class knew about another, but did not send messages. Data structures had dependencies on their members because they didn't send them messages, whereas classes had associations with their members because they did send them messages. But that nice separation has gotten so badly muddled that it has become useless.

Perhaps you can read from my tone that I'm pretty disillusioned with UML. Too bad. It could have been a standard. Now, I think its just going to be a confusing compendium of confounding conventions.

---------
5.
---------
//
From UML 2.0 Superstructure Final Adopted Specification:
"An association describes a set of tuples whose values refer to typed instances." (p. 97)
"A dependency is a relationship that signifies that a single or a set of model elements requires other model elements for their specification or implementation." (p. 124)

---------
6.
---------
依賴是比關聯弱的關係,關聯代表一種結構化的關係,體現在生成的代碼中,以java爲例:
若類A單向關聯指向類B,則在類A中存在一個屬性B b。
若類A依賴類B,則不會有這個屬性,類B的實例可能存在於某個方法調用的參數中,或某個方法的局部變量中。

---------
7.
---------
關聯有雙向與單向之分,類A與類B雙向關聯可以是A與B相互可以作爲對方的一個attribute,單向的話,就指其中一個類作爲另一個類中的 attribute;依賴就只有單向的,不存在attribute的問題,例如類A依賴類B,表示在類A中有三種類B的使用方法:一是類B是全局的,二是類B在類A中實例化,三是類B作爲參數被傳遞

---------
8.
---------
關聯是一種結構關係,表現爲一個對象能夠獲得另一個對象的實例引用並調用它的服務(即使用它);依賴是一種使用關係,表現爲一個對象僅僅是調用了另一個對象的服務

歡迎大家拍磚...
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章