问题描述
假设我有一个包含许多公共方法的类:
Say I have a class with many of public methods:
public class MyClass {
public void method1() {}
public void method2() {}
(...)
public void methodN() {}
}
现在我想创建一个 wrapper 类,它将所有方法委托给包装的实例(delegate):
Now I would like to create a wrapper class which would delegate all the methods to wrapped instance (delegate):
public class WrapperClass extends MyClass {
private final MyClass delegate;
public WrapperClass(MyClass delegate) {
this.delagate = delegate;
}
public void method1() { delegate.method1(); }
public void method2() { delegate.method2(); }
(...)
public void methodN() { delegate.methodN(); }
}
现在,如果 MyClass 有很多方法,我将需要覆盖它们中的每一个,它们或多或少是相同的代码,只是委托".我想知道是否有可能做一些魔术来自动调用 Java 中的方法(所以 Wrapper 类需要说嘿,如果你在我身上调用方法,只需转到 delegate 对象并调用这个方法就可以了).
Now if MyClass has a lot of methods I would need to override each of them which is more or less the same code which just "delegates". I was wondering if it is possible to do some magic to automatically call a method in Java (so the Wrapper class would need to say "Hey if you call a method on me just go to delegate object and call this method on it).
顺便说一句:我不能使用继承,因为委托不在我的控制之下.我只是从其他地方获取它的实例(另一种情况是 MyClass 是最终的).
BTW: I can not use inheritance because the delegate is not under my control.I just get its instance from elsewhere (another case would be if MyClass was final).
注意:我不想生成 IDE.我知道我可以在 IntelliJ/Eclipse 的帮助下做到这一点,但我很好奇这是否可以在代码中完成.
NOTE: I do not want IDE generation. I know I can do it with help of IntelliJ/Eclipse, but I'm curious if this can be done in code.
任何建议如何实现这样的事情?(注意:我可能可以用一些脚本语言来做到这一点,比如 php,我可以使用 php 魔术函数来拦截调用).
Any suggestions how to achieve something like this? (NOTE: I would probably be able to do it in some scripting languages like php where I could use php magic functions to intercept the call).
推荐答案
或许java的动态Proxy
可以帮到你.它仅在您因此使用接口时才有效.在这种情况下,我将调用接口 MyInterface
并设置一个默认实现:
Perhaps the dynamic Proxy
of java can help you. It only works if you consequently use interfaces. In this case, I will call the interface MyInterface
and set up a default implementation:
public class MyClass implements MyInterface {
@Override
public void method1() {
System.out.println("foo1");
}
@Override
public void method2() {
System.out.println("foo2");
}
@Override
public void methodN() {
System.out.println("fooN");
}
public static void main(String[] args) {
MyClass wrapped = new MyClass();
wrapped.method1();
wrapped.method2();
MyInterface wrapper = WrapperClass.wrap(wrapped);
wrapper.method1();
wrapper.method2();
}
}
包装类实现如下所示:
public class WrapperClass extends MyClass implements MyInterface, InvocationHandler {
private final MyClass delegate;
public WrapperClass(MyClass delegate) {
this.delegate = delegate;
}
public static MyInterface wrap(MyClass wrapped) {
return (MyInterface) Proxy.newProxyInstance(MyClass.class.getClassLoader(), new Class[] { MyInterface.class }, new WrapperClass(wrapped));
}
//you may skip this definition, it is only for demonstration
public void method1() {
System.out.println("bar");
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Method m = findMethod(this.getClass(), method);
if (m != null) {
return m.invoke(this, args);
}
m = findMethod(delegate.getClass(), method);
if (m != null) {
return m.invoke(delegate, args);
}
return null;
}
private Method findMethod(Class<?> clazz, Method method) throws Throwable {
try {
return clazz.getDeclaredMethod(method.getName(), method.getParameterTypes());
} catch (NoSuchMethodException e) {
return null;
}
}
}
注意这个类:
- 扩展
MyClass
,以继承默认实现(任何其他都可以) - 实现
Invocationhandler
,允许代理进行反射 - 可选地实现
MyInterface
(以满足装饰器模式)
- extends
MyClass
, to inherit a default implementation (any other would do) - implements
Invocationhandler
, to allow the proxy to do reflection - optionally implement
MyInterface
(to satisfy the decorator pattern)
此解决方案允许您覆盖特殊方法,但委托所有其他方法.这甚至适用于 Wrapper 类的子类.
This solution allows you to override special methods, but to delegate all others. This will even work with sub classes of Wrapper class.
请注意,方法 findMethod
尚未捕获特殊情况.
Note that the method findMethod
does not yet capture the special cases.
这篇关于自动委托 java 类的所有方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!