Difference between association and dependency?

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.

...


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.


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.

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.

Tags:

Uml