import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class DynmicProxyMode {
public static void main(String[] args) {
final HelloWorld impl = new HelloWorldImpl();
HelloWorld helloWolrdProxy = (HelloWorld)Proxy.newProxyInstance(
HelloWorld.class.getClassLoader(),
new Class[] { HelloWorld.class }, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method,
Object[] args) throws Throwable {
System.out.println("前");
method.invoke(impl, args);
System.out.println("后");
return null;
}
});
helloWolrdProxy.a("a");
System.out.println("============");
helloWolrdProxy.b("b");
}
}
interface HelloWorld {
public abstract void a(String s);
public abstract void b(String s);
}
class HelloWorldImpl implements HelloWorld {
@Override
public void a(String s) {
System.out.println(s);
}
@Override
public void b(String s) {
System.out.println(s);
}
}
结构型模式---代理模式之动态代理自己实现
最新推荐文章于 2019-02-02 17:29:01 发布