2

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

Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186
Susantha7
  • 898
  • 1
  • 20
  • 38

2 Answers2

1

You model an association only of you intend to introduce an owned property in either of the connected classes. If that is not the case and you only reference the other class in a parameter or so you will use a dependency. In the course of the modeling process you start with usually a simple association or a dependency ad hoc. This is because you feel the binding force between both. A plain (eventually directed) association is the first indication for the property. In a later design stage you will name the role and add multiplicity (thereby removing the direction arrow since the direction is now clarified by the role name usage). Finally you place the dot to show that the role name should be implemented as owned property. Dependencies are more an optional thing like "also look here". It just helps to navigate to the right position but you will not create any property for it.

qwerty_so
  • 35,448
  • 8
  • 62
  • 86
  • if connected class( `class B`) used in multiple methods in another class (in `class A`) then it is ok to introduce association rather than dependency ? – Susantha7 Nov 02 '18 at 18:41
  • if connected class( `class B`) used in one methods in another class (in `class A`) then it is ok to introduce dependency rather than association ? – Susantha7 Nov 02 '18 at 18:41
  • i want to know how to decide class (`class B`) use as a association or dependency within another class (`class A`) ,what are the reasons have to consider for that – Susantha7 Nov 02 '18 at 18:43
  • I told you when to use an association. Right in the first sentence of my answer. What don't you understand with that? – qwerty_so Nov 02 '18 at 18:46
  • OK thanks it sounds like there is no hard rule for decide use class b as a association or decency depending on my need i can decide which use to when .. – Susantha7 Nov 02 '18 at 19:01
  • 1
    There is no rule except you take the statement in my first sentence. Theoretically you can just use dependency. – qwerty_so Nov 02 '18 at 19:12
0

Lets see example with Printer:

public class Printer {

   private Paper paper;
   private Inks inks;

   Printer(Paper paper, Inks inks){
      this.paper = paper;
      this.inks = inks;
   }

   public PrintedFile print(PDFFile pdfFile){
     // print PDFFile to paper
   }
}

Association (Printer has...) (aggregation/composition): 1) Paper 2) Inks

Dependecy: 1) PDF File (Pdf file is not a part of printer, printer just uses it...)

Resume: If object A should HAS object B as an inner part of itself, it is association. If object A just uses object B, and object B is NOT a part of object A, it is dependency.

Che_D
  • 46
  • 2