Created
February 1, 2011 23:51
-
-
Save jaz303/806983 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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