I'm using a listener pattern where a class A
listens for events from various classes B
, C
, D
with the help of a listener interface I
Essentially the structure looks like:
interface I {
void generalCallback();
}
class A implements I {
@Override
public void generalCallback() {
/// Do some stuff
}
void initCommon(Common common) {
common.setListener(this);
}
}
class Common {
I i;
void setListener(I i) {
this.i = i;
}
}
class B extends Common {
void doStuff() {
i.generalCallback();
}
}
class C extends Common {
void doSomeOtherStuff() {
i.generalCallback();
}
}
class D extends Common {
void doSomeGeneralStuff() {
i.generalCallback();
}
}
Now, for some reasons, I want to inform A
about a specific event of D
. So is it okay to add one more method to the interface which would now be specific to a particular client(D
here) rather than general.
Updated Code
interface I {
...
void callbackFromD();
}
class A implements I {
...
@Override
public void callbackFromD() { }
}
class D extends Common {
...
void doSpecificStuff() {
i.callbackFromD();
}
}
So my questions are :
- Is this a good approach to solve this problem?
- Or should I create a new interface just for 1 callback from
D
? - What happens when I have the requirement for specific callbacks from other classes
B
andC
as well?