Reputation: 1508
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 :)
Upvotes: 138
Views: 132869
Reputation: 983
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
Upvotes: 3
Reputation: 336
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.
Upvotes: -1
Reputation: 56391
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.
Upvotes: 126
Reputation: 570
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.
Upvotes: 10
Reputation: 13208
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.
Upvotes: 77
Reputation: 3750
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-
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.
Upvotes: 19
Reputation: 300539
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.
...
Upvotes: 61
Reputation: 251
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.
Upvotes: 25
Reputation: 3941
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.
Upvotes: 3