Difference between association and dependency?

Viewed 106867

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 :)

9 Answers

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.

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.

...

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.

I was always checking this answer as it didn't stick in my mind. I found this one more helpful after reading the accepted answer UML Dependency vs Association

Association is when one object just has a link to another and don't use relational object methods. For ruby for example

class User
  has_one :profile
end

user = User.first
profile = user.profile
profile.sign_out

It means you can get a profile object from user but user don't use profile's methods inside himself(has no dependency on a Profile's interface).

Dependency means that User has link to another object and call that object's methods inside himself

class User
  has_one :profile

  def personal_info
    profile.info
  end
end

Here if Profile's info method will be changed or renamed our Dependent User class also need to be changed.

Related