In an UML class diagram, an association is a stronger relationship than a dependency,
association and dependency can be implemented as follows:
Association --> A has-a B object (as a member variable)
public class A {
private B b;
A(B b){
this.b= b;
}
public void myMethod() {
b.callMethod();
}
}
Dependency --> A references B (as a method parameter or return type)
public class A {
public void myMethod(B b) {
b.callMethod();
}
}
In the above example calling b.callMethod()
can be achieve using either association or dependency
I want to know when to use one of the approaches:
- one object has the other object as a field(association)
- object accepts another object as a method parameter(dependency)???
p.s - Any example would be more than welcome :)