动态代理-基于接口实现
静态代理模式:

动态代理模式:



接口和类设计(客户-房产中介-租户):

Rent:
public interface Rent { public void rent(); }
Host:
1 public class Host implements Rent{ 2 @Override 3 public void rent() { 4 System.out.println("出租房子"); 5 } 6 }
ProxyInvocationHandler:
1 /* 2 InvocationHandler是由代理实例的调用处理程序实现的接口 。 3 每个代理实例都有一个关联的调用处理程序。 4 当在代理实例上调用方法时,方法调用将被编码并分派到其调用处理程序的invoke方法。 5 */ 6 // 用这个类来自动生成代理类 7 public class ProxyInvocationHandler implements InvocationHandler { 8 9 // 被代理的接口 10 private Rent rent; 11 12 public void setRent(Rent rent) { 13 this.rent = rent; 14 } 15 16 // Foo f = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader(), 17 // new Class<?>[] { Foo.class }, 18 // handler); 19 // 为某个接口创建代理 20 public Object getProxy(){ 21 return Proxy.newProxyInstance(this.getClass().getClassLoader(),rent.getClass().getInterfaces(),this); 22 } 23 24 // 处理代理实例,并返回接口 25 @Override 26 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 27 28 seehouse(); 29 Object result = method.invoke(rent, args); 30 fare(); 31 return result; 32 } 33 // 扩展的业务方法 34 public void seehouse(){ 35 System.out.println("看房子"); 36 } 37 38 public void fare(){ 39 System.out.println("收取中介费"); 40 } 41 42 43 }
client:
1 public class Client { 2 public static void main(String[] args) { 3 // 真实角色 4 Host host = new Host(); 5 6 // 代理角色,现在没有 7 ProxyInvocationHandler pih = new ProxyInvocationHandler(); 8 // 通过调用程序处理角色来处理我们要调用的接口对象 9 pih.setRent(host); 10 11 Rent proxy = (Rent) pih.getProxy(); // 这里的proxy就是动态生成的类 12 proxy.rent(); 13 14 } 15 }
结果:

简化:
接口:UserService
1 public interface UserService { 2 public void add(); 3 public void delete(); 4 public void query(); 5 public void update(); 6 }
真实对象类:UserServiceImpl
1 public class UserServiceImpl implements UserService{ 2 @Override 3 public void add() { 4 System.out.println("增加用户"); 5 } 6 7 @Override 8 public void delete() { 9 System.out.println("删除用户"); 10 } 11 12 @Override 13 public void query() { 14 System.out.println("查询用户"); 15 } 16 17 @Override 18 public void update() { 19 System.out.println("更新用户"); 20 } 21 }
代理类生成程序类: ProxyInvocationHandler
1 public class ProxyInvocationHandler implements InvocationHandler { 2 3 // 被代理的接口 4 private Object target; 5 6 public void setTarget(Object target) { 7 this.target = target; 8 } 9 10 // 为某个接口创建代理 11 public Object getProxy(){ 12 return Proxy.newProxyInstance(this.getClass().getClassLoader(),target.getClass().getInterfaces(),this); 13 } 14 15 // 处理代理实例,并返回接口 16 @Override 17 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 18 log(method.getName()); 19 Object result = method.invoke(target, args); 20 return result; 21 } 22 23 // 增加日志方法 24 public void log(String msg){ 25 System.out.println("执行了"+msg+"方法"); 26 } 27 28 }
客户类:
1 public class Client { 2 public static void main(String[] args) { 3 // 真实对象 4 UserServiceImpl userService = new UserServiceImpl(); 5 // 代理程序 6 ProxyInvocationHandler pih = new ProxyInvocationHandler(); 7 pih.setTarget(userService); // 设置要代理的类 8 9 UserService proxy = (UserService) pih.getProxy(); // 动态生成代理类 10 // proxy.add(); 11 proxy.delete(); 12 } 13 }
结果:


浙公网安备 33010602011771号