标签:style blog http java 使用 os io strong
周末放假,小孙睡到12点才告别周公醒来,顿时饥肠辘辘。舍长小王正准备去食堂买饭,作为一个好舍长小王主动要帮小孙带饭。小孙点了米饭、宫保鸡丁、芬达。小孙起床洗漱,然后静待舍长。小孙心理寻思道舍长果然是好舍长啊。下面我们先把这个故事抽象一下,画作类图。这个类图即代理模式。
/** * 宿舍成员接口 * 开发时间:2014-8-12 下午9:17:19 */ public interface DormitoryMember { //买主食 public boolean buyStapleFood(String stapleFoodName); //买菜 public boolean buyDish(String dishName); //买饮料 public boolean buyDrink(String drinkName); }
//代理类 public class DormitoryMemberProxy implements DormitoryMember { //持有一个被代理的对象 private DormitoryMember dormitoryMember; //构造时传入被代理对象 public DormitoryMemberProxy(DormitoryMember dormitoryMember){ this.dormitoryMember=dormitoryMember; } //代理买主食 public boolean buyStapleFood(String stapleFoodName) { return dormitoryMember.buyStapleFood(stapleFoodName); } //代理买菜 public boolean buyDish(String dishName) { return dormitoryMember.buyDish(dishName); } //代理买饮料 public boolean buyDrink(String drinkName) { return dormitoryMember.buyDrink(drinkName); } }
//实际的类 public class DormitoryMemberImpl implements DormitoryMember { //买主食 public boolean buyStapleFood(String stapleFoodName) { try{ System.out.println("买到主食" + stapleFoodName); }catch(Exception e){ return false; } return true; } //买菜 public boolean buyDish(String dishName) { try{ System.out.println("买到菜:" + dishName); }catch(Exception e){ return false; } return true; } //买饮料 public boolean buyDrink(String drinkName) { try{ System.out.println("买到饮料:" + drinkName); }catch(Exception e){ return false; } return true; } }
//代理类 public class DormitoryMemberProxy implements DormitoryMember { //持有一个被代理的对象 private DormitoryMember dormitoryMember; //构造时传入被代理对象 public DormitoryMemberProxy(DormitoryMember dormitoryMember){ this.dormitoryMember=dormitoryMember; } //代理买主食 public boolean buyStapleFood(String stapleFoodName) { boolean buySuccess=dormitoryMember.buyStapleFood(stapleFoodName); if( buySuccess=false){ System.out.println("给小孙打电话"); return false; }else{ return true; } } //代理买菜 public boolean buyDish(String dishName) { boolean buySuccess=dormitoryMember.buyDish(dishName); if( buySuccess=false){ System.out.println("给小孙打电话"); return false; }else{ return true; } } //代理买饮料 public boolean buyDrink(String drinkName) { boolean buySuccess=dormitoryMember.buyDrink(drinkName);; if( buySuccess=false){ System.out.println("给小孙打电话"); return false; }else{ return true; } } }
再看问题二,对于问题二我们必须将代理类和被代理类解耦,通常我们解决这类问题就是用反射。这也不例外,这里java提供的反射机制可以在运行时产生代理类,即动态代理。这篇博客已经太长了,分开来吧。先把动态代理的代码贴上。其中实际类和接口没有变化!
import java.lang.reflect.Proxy; //代理工厂类 public class ProxyFactory { //实际类 private Object target; public ProxyFactory(Object target){ this.target=target; } //根据传入的实际类生成代理 public Object getInstance(){ //实例化一个继承了InvocationHandler接口的预处理对象 ProxyHandler handler=new ProxyHandler(target); //反射得到代理类 return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), handler);//handler即集中处理在动态代理类对象上的方法调用 } }
import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; public class ProxyHandler implements InvocationHandler { private Object target; public ProxyHandler(Object target){ this.target=target; } //所有代理方法的公共处理方法 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Object result =null; result=method.invoke(target, args); //要买的东西断货时 if(result.toString().equals("false")){ System.out.println("给小孙打电话"); return result; }else{ return result; } } } public class Client { public static void main(String[] args){ //真正得到的代理类在客户端强转 DormitoryMember dormitoryMember=(DormitoryMember)new ProxyFactory(new DormitoryMemberImpl()).getInstance(); dormitoryMember.buyStapleFood("米饭"); } }
代理模式深入(一)——静态到动态,布布扣,bubuko.com
标签:style blog http java 使用 os io strong
原文地址:http://blog.csdn.net/zhuojiajin/article/details/38531539