Callback and tight coupling

I’m refactoring some parts of my code, one of them has callback function. I’m not really sure of the relevance of callback there, because it introduces tight-coupling.

Let me give you an example:

public class SimpleExample {
OtherClass oc;
Callback callback;
...

public void register(Callback c) {
    this.callback = c;
}

public void doStuff() {
    callback.push(oc);
}
...

}

The interface:

public interface Callback {
public void push(OtherClass oc);
}

The implementation of the interface:

public CallbackImpl implements Callback {
HashMap<OtherClass, String> map;
...
public void push(OtherClass oc) {
    String s = map.get(oc);
    System.out.println(s);
}
...
}

The main problem i see is that my interface use OtherClass as a function parameter. It is worth to mention that OtherClass is not an interface. So my first meaning was to refactor this class with an interface. Is it a good choice ?


Source: oop

Leave a Reply