Is the set of IAction
s finite, unchanging, and manageable? If so, your Observer is more like a Visitor and requires an overload for every one of the known actions.
If the set of IAction
s is not all of those things, but the caller knows what kind of IAction
is being passed in statically, your IObserver
interface as defined is correct. That is, an implementer of the interface (such as Server
) should dynamically react to the specific IAction
class, using any of a number of extremely ugly choices in Java to do so.
public interface IObserver {
void < T extends IAction > onAction( T action );
}
public class Server implements IObserver {
@Override public void < T extends IAction > onAction( T action ) {
// ugliness, e.g. if-ladders based on instanceof
}
}
I don't think that buys you very much.
If the caller does not know which IAction
is going to be used at runtime, then your only choice is
public interface IObserver {
void onAction( IAction action );
}
public class Server implements IObserver {
@Override public void onAction( IAction action ) {
// ugliness, e.g. if-ladders based on instanceof
}
}
whose implementation will look the same as in the previous example.