Car car = new Car();
Class<?> cls = car.getClass();
InvocationHandler timeHandler = new TimeHandler(car);
InvocationHandler logHandler = new LogHandler(car);
Moveable moveable = (Moveable) Proxy.newProxyInstance(cls.getClassLoader(), cls.getInterfaces(), timeHandler);
Moveable m = (Moveable)Proxy.newProxyInstance(moveable.getClass().getClassLoader(), cls.getInterfaces(), logHandler);
m.move();
timeHandler和logHandler 是两个调用处理器,各自重写的invoke方法的业务逻辑不同。Car类实现了Moveable接口,这接口里只有一个move方法,现在怎么才能让move在执行时能够新增timeHandler 和logHandler 的方法呢?我现在写的这样不行
相关分类