Does passing an object as a parameter result in the same level of coupling as having it stored in a field?

119 Views Asked by At
class ConcreteObserver implements Observer {
    String text;

    public void actualize(Subject subject) {
        this.text = subject.getState();
    }
}

In other words, does the code above make my Subject coupled to Observer in the same way as if I did this?

class ConcreteObserver implements Observer {
    String text;
    Subject subject;

    public void actualize() {
        this.text = subject.getState();
    }
}
1

There are 1 best solutions below

0
Олександр Демура On

all of the following relationships are counted as coupling:

  • association
  • aggregation
  • dependency
  • generalization

so ConcreteObserver coupled to the Subject in both cases. (and nothing can be said about stated in the question Subject's coupling to Observer)

the second case decouples parent Observer from Subject, but IMO explicit dependency is better than faking the scope with implicit.