Dyl*_*n P 5 java reflection observer-pattern
我有很多使用setListener方法注册的监听器,而不是addListener.因此,为了允许多个侦听器注册到一个对象,我必须使用多路复用器.这很好,但现在我必须为我拥有的每个监听器接口创建一个多路复用器.所以我的问题是:是否可以根据以下代码的需要实现Mux.create()?
AppleListener appleListener1 = new AppleProcessorA();
AppleListener appleListener2 = new AppleProcessorB();
AppleListener appleListenerMux = Mux.create(appleListener1, appleListener2);
Apple apple = new Apple();
apple.setListener(appleListenerMux);
OrangeListener orangeListener1 = new OrangeProcessorA();
OrangeListener orangeListener2 = new OrangeProcessorB();
OrangeListener orangeListenerMux = Mux.create(orangeListener1, orangeListener2);
Orange apple = new Orange();
orange.setListener(orangeListenerMux);
class Mux {
public static <T> T create(T... outputs) { }
}
Run Code Online (Sandbox Code Playgroud)
我想这可能是使用反射.有没有理由使用反射会是一个坏主意?(表现浮现在脑海中)
可以使用动态代理.
最简单的方法是将所需的接口作为Mux.create()调用的第一个参数传递.否则,您将不得不使用反射来尝试从所提供的所有具体侦听器实例中猜出所需的接口(很难确定所有侦听器对象是否实现了多个共同的接口).
这是它的缺点:
public class Mux {
/**
* @param targetInterface
* the interface to create a proxy for
* @param instances
* the concrete instances to delegate to
* @return a proxy that'll delegate to all the arguments
*/
@SuppressWarnings("unchecked")
public static <T> T create(Class<T> targetInterface, final T... instances) {
ClassLoader classLoader = targetInterface.getClassLoader();
InvocationHandler handler = new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method m, Object[] args)
throws Throwable {
for (T instance : instances) {
m.invoke(instance, args);
}
return null;
}
};
return (T) Proxy.newProxyInstance(classLoader,
new Class<?>[] { targetInterface }, handler);
}
}
Run Code Online (Sandbox Code Playgroud)
您将使用哪种,例如,如下:
Apple apple = new Apple();
AppleListener l1 = new AppleListenerA();
AppleListener l2 = new AppleListenerB();
apple.setListener(Mux.create(AppleListener.class, l1, l2));
apple.doSomething(); // will notify all listeners
Run Code Online (Sandbox Code Playgroud)
这可以通过简单地创建一个Proxy强制转换为目标类型的动态来实现T.该代理使用InvocationHandler仅仅将代理的所有方法调用委托给给定的具体实例.
请注意,虽然一般我完成所有的参数和局部变量只要有可能,我才最终确定下来T... instances在这种情况下,突出的是,如果事实instances是不是最终的,那么匿名内部类中引用它不会被允许(你会得到一个"不能引用在不同方法中定义的内部类中的非final变量args").
另请注意,上面假设实际的方法调用不返回任何有意义(或有用)的值,因此handler也返回null所有方法调用.如果要收集返回值并以有意义的方式返回它们,则需要添加更多代码.
或者,可以检查所有给定的instances以确定它们都实现的公共接口,并将所有这些接口传递给newProxyInstance().这使得Mux.create()使用起来更加方便,失去对其行为的一些控制.
/**
* @param instances
* the arguments
* @return a proxy that'll delegate to all the arguments
*/
@SuppressWarnings("unchecked")
public static <T> T create(final T... instances) {
// Inspect common interfaces
final Set<Class<?>> commonInterfaces = new HashSet<Class<?>>();
commonInterfaces.addAll(Arrays.asList(instances[0].getClass()
.getInterfaces()));
// Or skip instances[0]
for (final T instance : instances) {
commonInterfaces.retainAll(Arrays.asList(instance.getClass()
.getInterfaces()));
}
// Or use ClassLoader.getSystemClassLoader();
final ClassLoader classLoader = instances[0].getClass().getClassLoader();
// magic
final InvocationHandler handler = new InvocationHandler() {
@Override
public Object invoke(final Object proxy, final Method m, final Object[] args)
throws Throwable {
for (final T instance : instances) {
m.invoke(instance, args);
}
return null;
}
};
final Class<?>[] targetInterfaces = commonInterfaces
.toArray(new Class<?>[commonInterfaces.size()]);
return (T) Proxy.newProxyInstance(classLoader, targetInterfaces,
handler);
}
Run Code Online (Sandbox Code Playgroud)