公用接口和实现
public interface Calculator {
public Integer add(Integer num1, Integer num2);
public Integer minus(Integer num1, Integer num2);
}
public class CalculatorImpl implements Calculator {
@Override
public Integer add(Integer num1, Integer num2) {
int ret = num1 + num2;
System.out.println("in calculatorImpl, res: " + ret);
return ret;
}
@Override
public Integer minus(Integer num1, Integer num2) {
int ret = num1 - num2;
System.out.println("int calculatorImpl, res: " + ret);
return ret;
}
}
静态代理
public class StaticCalculatorProxy implements Calculator {
Calculator obj;
public StaticCalculatorProxy(Calculator obj) {
this.obj = obj;
}
@Override
public Integer add(Integer num1, Integer num2) {
System.out.println("in StaticCalculatorProxy, before invocation");
Integer ret = obj.add(num1, num2);
System.out.println("in StaticCalculatorProxy, after invocation");
return ret;
}
@Override
public Integer minus(Integer num1, Integer num2) {
System.out.println("in StaticCalculatorProxy, before invocation");
Integer ret = obj.minus(num1, num2);
System.out.println("in StaticCalculatorProxy, after invocation");
return ret;
}
}
测试代码:
public static void staticProxy() {
CalculatorImpl calculator = new CalculatorImpl();
StaticCalculatorProxy calculatorProxy = new StaticCalculatorProxy(calculator);
calculatorProxy.add(1, 2);
calculatorProxy.minus(2, 1);
}
动态代理
public class CalculatorHandler implements InvocationHandler {
private Object obj; //被代理类
public CalculatorHandler(Object obj) {
this.obj = obj;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("in calculatorhandler, before invocation");
Object ret = method.invoke(obj, args); //执行被代理类方法
System.out.println("in calculationhandler, after invocation");
return ret;
}
}
测试代码:
public static void dynamicProxy() {
CalculatorImpl calculatorImpl = new CalculatorImpl();//被代理类
CalculatorHandler calculatorHandler = new CalculatorHandler(calculatorImpl);
Calculator calculator = (Calculator) Proxy.newProxyInstance(
calculatorImpl.getClass().getClassLoader(),
calculatorImpl.getClass().getInterfaces(), calculatorHandler);
calculator.add(1, 2);
calculator.minus(1, 2);
}
动态代理的好处
无论calculator中包含多少函数,动态代理只需实现一次,实际工程中,System.out.println(“in calculatorhandler, before invocation”)可能是加缓存,打日志等操作。
代理模式和装饰器模式的区别
- 装饰器模式关注于在一个对象上动态的添加方法,然而代理模式关注于控制对对象的访问;
- 使用代理模式,代理和真实对象之间的的关系通常在编译时就已经确定了,而装饰者能够在运行时递归地被构造;
代理器模式在插件化中的应用
Android插件化原理解析——Hook机制之动态代理
动态加载APK原理分享