ObjectDispatcher { public Object dispatch(Object... params) throws IllegalArgumentException, InvocationTargetException, NoSuchMethodException { if (params.length != 1 + methodSignature.length) { throw new IllegalArgumentException("Expected " + methodSignature.length + " parameters"); } Class<?> type = params[0].getClass(); Target target = findTarget(type); return target.process(params); } ObjectDispatcher(Collection<Object> delegates);  ObjectDispatcher(Collection<Object> delegates,
            Class<?>[] methodSignature);  ObjectDispatcher(Collection<Object> delegates,
            Class<?>[] methodSignature,
            Pattern methodPattern);  ObjectDispatcher(Collection<Object> delegates,
            Class<?>[] methodSignature,
            Pattern methodPattern,
            SuperTypeTraverser superTypeTraverser); Object dispatch(Object... params); void warmup(Class<?> type);  }
@Test public void testOverridingServices() throws Exception { ObjectDispatcher dispatcher = new ObjectDispatcher(Arrays.asList(new MyService())); ObjectDispatcher customizedDispatcher = new ObjectDispatcher(Arrays.asList( new MyCustomizedService(), new MyService())); ObjectDispatcher customizedDispatcherIncorrectOrder = new ObjectDispatcher(Arrays.asList( new MyService(), new MyCustomizedService())); assertEquals(2, dispatcher.dispatch(1)); assertEquals(3, customizedDispatcher.dispatch(1)); assertEquals(2, customizedDispatcherIncorrectOrder.dispatch(1)); }
@Test public void test() throws Exception { ObjectDispatcher dispatcher = new ObjectDispatcher( Arrays.asList(new MyService(), new MyErrorHandler())); assertEquals("*Hello*", dispatcher.dispatch("Hello")); assertEquals(2, dispatcher.dispatch(1)); assertEquals(null, dispatcher.dispatch(1.0)); try { dispatcher.dispatch(new Date()); fail("Expected exception"); } catch (InvocationTargetException e) {} }
@Test public void test2Params() throws Exception { ObjectDispatcher dispatcher = new ObjectDispatcher( Arrays.asList(new MyService2(), new MyErrorHandler2()), new Class<?>[] { String.class }); assertEquals("*Hello*", dispatcher.dispatch("Hello", "Param2")); assertEquals(2, dispatcher.dispatch(1, "Param2")); assertEquals(null, dispatcher.dispatch(1.0, "Param2")); try { dispatcher.dispatch(new Date(), "Param2"); fail("Expected exception"); } catch (InvocationTargetException e) {} }