一、代理模式 1.代理模式定义:为其他对象提供一种代理以控制对这个对象的访问。
2.代理模式UML
3.代理模式的示例
interface ProxyBase{ public void f(); public void g(); public void h(); } //代理程序 class Proxy implements ProxyBase{ private ProxyBase implementation; public Proxy(){ //目标程序 implementation = new ProxyImplementation(); } public void f(){ implementation.f(); } public void g(){ implementation.g(); } public void h(){ implementation.h(); } } //目标程序 class ProxyImplementation implements ProxyBase{ public void f(){ System.out.println("ProxyImplementation.f()"); } public void g(){ System.out.println("ProxyImplementation.g()"); } public void h(){ System.out.println("ProxyImplementation.h()"); } } //客户端程序调用代理 public class ProxyDemo{ public static void main(String[] args){ //客户端调用代理程序 Proxy p = new Proxy(); p.f(); p.g(); p.h(); } }4.jdk动态代理示例 动态代理:动态代理中的代理类是由java.lang.reflect.Proxy类在运行期时根据接口定义,采用Java反射功能动态生成的。和java.lang.reflect.InvocationHandler结合,可以加强现有类的方法实现。动态带来自定义Handler实现InvocationHandler接口,自定义Handler实例化时,将代理的实现类传入自定义Handler对象中。自定义Handler需要实现invoke方法,该方法可以使用Java反射调用实现类的实现的方法,同时当然可以实现其他功能,例如在调用实现类方法前后加入Log,实现安全认证等。而Proxy类根据Handler和需要代理的接口动态生成一个接口实现类的对象。当用户调用这个动态生成的实现类时,实际上是调用了自定义Handler的invoke方法。
//代理接口 interface Foo{ public void f(String s); public void g(int i); public void h(int i, String s); } //接口实现类,即被代理类 class FooImpl implements Foo{ public void f(String s){ System.out.println("FooImpl.f(), s=" + s); } public void g(int i) { System.out.println("FooImpl.g(), i=" + i); } public void h(int i, String s) { System.out.println("FooImpl.h(), i=" + i + ", s=" + s); } } //动态代理处理类 class ProxyHandler implements InvocationHandler{ //代理实现类 private Object delegate; public ProxyHandler (Object obj) { delegate = obj; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException{ System.out.println("Before mothod:" + method); method.invoke(this.delegate, args); System.out.println("After mothod:" + method); return null; } } public class DynamicProxyDemo{ public static void main(String[] args){ Foo foo = new FooImpl(); ProxyHandler handler = new ProxyHandler(foo); //产生动态代理 Foo proxy = (Foo)Proxy.newProxyInstance(Foo.class.getClassLoader(), new Class[]{Foo.class}, handler); proxy.f("f"); proxy.g(1); proxy.h(2,"h"); } }