胡萝卜卜姓胡
2018-12-24 21:41
CarSimp car = new CarSimp();
InvocationHandler handlerTime = new TimeHandler(car);
Class classCar = car.getClass();
Moveable mTime = (Moveable) Proxy.newProxyInstance(classCar.getClassLoader(), classCar.getInterfaces(), handlerTime);
mTime.move();
//上面的只是实现了时间的处理 如何也实现 日志处理?
InvocationHandler handlerLog = new LogHandler(handlerTime);
Moveable mLog = (Moveable) Proxy.newProxyInstance(classCar.getClassLoader(),classCar.getInterfaces(),handlerLog);
mTime是对car的时间代理对象,再生成mLog即mTime的日志代理对象即可。
Car car=new Car(); InvocationHandler timeHandler =new TimeHandler(car); Moveable m=(Moveable)Proxy.newProxyInstance(car.getClass().getClassLoader(),car.getClass().getInterfaces(),timeHandler); InvocationHandler logHandler=new LogHandler(m); Moveable m1=(Moveable)Proxy.newProxyInstance(m.getClass().getClassLoader(),m.getClass().getInterfaces(),logHandler); m1.move();
模式的秘密---代理模式
54914 学习 · 98 问题
相似问题