127

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

Wim Coenen
  • 66,094
  • 13
  • 157
  • 251
Charlie
  • 1,398
  • 2
  • 10
  • 8

9 Answers9

116

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.

Uvuvwevwevwe
  • 971
  • 14
  • 30
Randolpho
  • 55,384
  • 17
  • 145
  • 179
  • 1
    This is closest to the way I generally decide the issue. If the other class contributes in a substantive way to the state or behaviour of my class then it's an association. Thus strategy classes will be associations even if they have no internal state of their own. If the other class merely provides a service to my class then it is a dependency. – Terrible Tadpole Aug 12 '19 at 05:45
  • @ As per this [answer](https://stackoverflow.com/a/9584259/13611002), a Singleton relationship could be either dependency or aggregation( But I think maybe it could also be association as well). As per your answer, I think a Singleton relationship should only be dependency. – John Apr 19 '22 at 12:55
70

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.

Ahmad Abdelghany
  • 11,983
  • 5
  • 41
  • 36
  • 1
    @Naruto_Uzumaki Aggregation is a whole-part relation. A Playlist & Song for example. Please check my other answer for a wider differentiation between Association, Dependency and Aggregation http://stackoverflow.com/a/34069760/1998422 – Ahmad Abdelghany Jul 09 '16 at 12:14
  • From _UML Distilled_ book by _Martin Fowler_ : "With classes, dependencies exist for various reasons : One class sends a message to another; one class has another as part of its data; one class mentions another as a parameter to an operation" – Ahmad Abdelghany Jul 09 '16 at 12:17
60

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.

...

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
  • 8
    Why answer, when Martin does it so much better for you?! +1 – Randolpho Aug 05 '09 at 02:32
  • 6
    It's not crystal clear for me yet, but one thing I did understand is that dependencies are somewhat 'weaker' than associations. It seems that associations are a subset of dependencies, albeit in my view at least, dependency is a *stronger* word than association. That may well have been the source of the confusion. – Felipe Aug 04 '11 at 05:20
  • That article says it well. In fact it aligns with my thoughts. So pulling in some points from it here: **(1)** You don't want to show every dependency on a UML diagram - there are far too many. You need to be very selective and show only those that are important to whatever it is you are communicating. **(2)** If there is an association between two classes, there is also a dependency. The association implies it, as does a generalization. **So obvious to infer dependency is somewhat superset relationship of other UML relationships** – Mahesha999 Mar 26 '14 at 07:50
  • 2
    Your explanation is too far from the real world examples, so it didn't give clear understanding to even software engineers. – softninja Mar 26 '18 at 08:42
  • @ softninja: you mean you didn't understand. Everyone else seems to find it acceptable. Oh and thanks for the downvote. – Mitch Wheat Mar 26 '18 at 13:36
  • I understand this answer as: with association, you can ask the parent object for the associated object (either through a getter, or some getter-like method). with dependency, you can't ask for such object, but if you make changes to it, your 'parent' object may break. – Amade Jul 29 '18 at 13:06
24

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.
j0k
  • 22,600
  • 28
  • 79
  • 90
Shrikant Mali
  • 241
  • 2
  • 2
  • "Association is like when you declare a string as an attribute in your class. then your code is associated with the string class." If that is the case, then what is the difference between association and composition? – Dean P May 02 '19 at 21:05
19

Dependency - A change in a class affects the change in it's dependent class. Example- Circle is dependent on Shape (an interface). If you change Shape , it affects Circle too. So, Circle has a dependency on Shape.

Association- means there is a certain relationship between 2 objects

(one-one, one-many,many-many)

Association is of 2 types-

  1. Composition
  2. Aggregation

    1) Composition- stronger Association or relationship between 2 objects. You are creating an object of a class B inside another class A

 public class A {
       B b;
       public void setB(){
         this.b= new B();
        }
     }

If we delete class A , B won't exist( B object is created inside A only).

Another example -Body & Liver .Liver can't exist outside Body.

2) Aggregation - weaker type of Association between 2 objects.

public class A {       
             B b;
             public void setB(B b_ref){
                 this.b= b_ref;   
                /* object B is passed as an argument of a method */
              }
   }

Even if you delete class A, B will exist outside(B is created outside and passed to Class A)

Another example of this- Man & Car . Man has a Car but Man & Car exist independently.

Deen John
  • 3,522
  • 4
  • 29
  • 32
10

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.

Raf
  • 570
  • 6
  • 13
  • 1
    I like this definition. Association is: I (the class that references another class) just hold a reference to an object, I don't use it and the members of that class are not interesting to me. Dependency is: I use some members, so if the referenced class changes it could have an impact on me. If I got it right than that was easy to understand! – robsch Sep 09 '14 at 09:43
  • 4
    First question that came to mind when I read your comment: in the case of association - why would one hold a reference to an object and not use it? Do you mean like the reference is only a field, only to be returned if a client wants to know about the reference? – H.Rabiee Dec 13 '17 at 17:06
3

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.

programmernovice
  • 3,841
  • 11
  • 43
  • 63
3

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

Islam Salah
  • 953
  • 11
  • 22
-1

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.

stopanko
  • 306
  • 3
  • 7
  • Can you please indicate where you got this information from? I don't think there is a rule in the UML specs that says that one side of an association doesn't use the methods of the other side. In general an association is a stronger relationship than a dependency. – Geert Bellekens Sep 09 '20 at 10:45
  • @GeertBellekens As I understand Dependency needs to show that changes in a supplier class will require changes in a client class. In programming this is only when you are using an interface of supplier(or Show me another reason). From your point of view there is no difference in this arrows. They are not pointing to the code implementation and just conceptual. – stopanko Sep 10 '20 at 13:02
  • I'm afraid that is your personal understanding, but not how it is described in the UML specs. From UML 2.5 § 7.8.4.1: _A Dependency is a Relationship that signifies that a single model Element or a set of model Elements requires other model Elements for their specification or implementation. This means that the complete semantics of the clientElement(s) are either semantically or structurally dependent on the definition of the supplier Element(s)._ – Geert Bellekens Sep 10 '20 at 13:42