public abstract class Handler { public void handle(Object message) { multipleDispatch(message); } private void multipleDispatch(Object message) { try { this.getClass().getMethod("handle", message.getClass()).invoke(this, message); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { ; } } }
Not a good idea for high-performance applications and possibly not a good idea for any app. When you send a message to a class that extends Handler then the correct method will get called if it is capable of handling it. If you use a specific type then the specific method is called like normal, otherwise this multiple dispatcher tries to call the correct method.
No comments:
Post a Comment