Skip to content

Instantly share code, notes, and snippets.

@jaz303
Created February 1, 2011 23:51
Show Gist options
  • Select an option

  • Save jaz303/806983 to your computer and use it in GitHub Desktop.

Select an option

Save jaz303/806983 to your computer and use it in GitHub Desktop.
class Handler {
public void handle(InputEvent ie) {
ie.dispatch(this);
}
public void handleInput(InputEvent ie) {
System.out.println("ERROR - NO HANDLER DEFINED");
}
public void handleInput(MouseEvent me) {
System.out.println("MOUSE EVENT!");
}
public void handleInput(KeyboardEvent ke) {
System.out.println("KEYBOARD EVENT!");
}
}
class InputEvent {
public void dispatch(Handler h) { h.handleInput(this); }
}
class MouseEvent extends InputEvent {
public void dispatch(Handler h) { h.handleInput(this); }
}
class KeyboardEvent extends InputEvent {
public void dispatch(Handler h) { h.handleInput(this); }
}
class FooEvent extends InputEvent {
// no explicit handler defined
public void dispatch(Handler h) { h.handleInput(this); }
}
class Main {
public static void main(String[] args) {
Handler h = new Handler();
h.handle(new MouseEvent());
h.handle(new KeyboardEvent());
h.handle(new FooEvent());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment