Difference between association and dependency?

2019-01-10 03:33发布

In a UML class diagram, what is the difference between an association relationship and a dependency relationship?

From what I know, an association is a stronger relationship than a dependency, but I'm not sure how it is stronger.

Any example would be more than welcome :)

标签: uml
7条回答
smile是对你的礼貌
2楼-- · 2019-01-10 03:35

Dependency is like when you define a method that takes a String(in Java, C#, as string is a object in them) as a parameter, then your class is dependent on String class.

Association is like when you declare a string as an attribute in your class. then your code is associated with the string class.

String name = null //: is a association.
查看更多
时光不老,我们不散
3楼-- · 2019-01-10 03:38

An association almost always implies that one object has the other object as a field/property/attribute (terminology differs). A dependency typically (but not always) implies that an object accepts another object as a method parameter, instantiates, or uses another object. A dependency is very much implied by an association.

查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-01-10 03:41

What is the difference between dependency and association?:

In general, you use an association to represent something like a field in a class. The link is always there, in that you can always ask an order for its customer. It need not actually be a field, if you are modeling from a more interface perspective, it can just indicate the presence of a method that will return the order's customer.

To quote from the 3rd edition of UML Distilled (now just out) "a dependency exists between two elements if changes to the definition of one element (the supplier) may cause changes to the other (the client)". This is a very vague and general relationship, which is why the UML has a host of stereotypes for different forms of dependency. In code terms, such things as naming a parameter type and creating an object in a temporary variable imply a dependency.

...

查看更多
Lonely孤独者°
5楼-- · 2019-01-10 03:45

In OOP terms:

Association --> A has-a C object (as a member variable)

Dependency --> A references B (as a method parameter or return type)

public class A {
    private C c;
    public void myMethod(B b) {
        b.callMethod();
    }
}

There is also a more detailed answer.

查看更多
The star\"
6楼-- · 2019-01-10 03:51

A dependency is very general and lowering complexity is about diminishing dependencies as much as possible.

An association is a strong (static) dependency. Aggregation and Composition are even stronger.

查看更多
仙女界的扛把子
7楼-- · 2019-01-10 03:57

Here: "Association vs. Dependency vs. Aggregation vs. Composition", you have a great vade mecum with uml class diagrams and code snippets. The author gives us a list of relationships: Association, Dependency, Aggregation, Composition in one place.

查看更多
登录 后发表回答