Java-Note-Pattern-动态代理个人心得
2018-12-27
Pattern
- 介绍: 通过实现
InvocationHandler接口, 动态的生成代理类. - 优点:
- 无需重写要修改的方法, 在普通代理代码变得很复杂的情况下, 动态代理可以减少代码量.
- 通过动态代理可以实现AOP(面向切面编程)
- 缺点: 基类必须实现接口, 没有普通代理方便.
- 要点:
- 代理类中有被代理类的成员变量
- 代理类必须初始化被代理类的实例
- 代理类中的
invoke(Object proxy, Method method, Object[] args)用来动态执行需要修改的方法以及在其之前的操作 - 要用
Proxy.newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h)来生成代理类的实例- 第二个参数
Class<?>[] interfaces表明基类必须实现接口且可以由多个基类和其接口
- 第二个参数
例子:
接口
1
2
3
4
5interface Interface {
void doSomething();
void somethingElse(String arg);
}基类
1
2
3
4
5
6
7
8
9
10
11class RealObject implements Interface {
public void doSomething() {
print("doSomething");
}
public void somethingElse(String arg) {
print("somethingElse "+arg);
}
}代理类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19class SimpleProxy implements Interface {
private Interface proxied;
public SimpleProxy(Interface proxied) {
this.proxied = proxied;
}
public void doSomething() {
print("SimpleProxy doSomething");
proxied.doSomething();
}
public void somethingElse(String arg) {
print("SimpleProxy somethingElse" + arg);
proxied.somethingElse(arg);
}
}测试类
1
2
3
4
5
6
7
8
9
10public class SimpleProxyDemo {
public static void consumer(Interface iface) {
iface.doSomething();
iface.somethingElse("bonobo");
}
public static void main(String[] args) {
consumer(new RealObject());
consumer(new SimpleProxy(new RealObject()));
}
}