前言:代理模式
类图
静态代理
Subject——抽象角色
1 2 3 4 5 6 7 8 9
| package com.husky.demo.proxy;
public interface Subject { public void doSomething(); }
|
RealSubject——真实角色/委托类
1 2 3 4 5 6 7 8 9 10 11 12 13
| package com.husky.demo.proxy;
public class RealSubject implements Subject{
@Override public void doSomething() { System.out.println("execute RealSubject method"); } }
|
ProxySubject——代理角色/代理类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| package com.husky.demo.proxy;
public class ProxySubject implements Subject { private RealSubject realSubject; public ProxySubject() { realSubject = new RealSubject(); }
@Override public void doSomething() { System.out.println("execute ProxySubject method start"); realSubject.doSomething(); System.out.println("execute ProxySubject method end"); } }
|
调用运行
1 2 3 4 5 6 7 8 9 10 11 12
| package com.husky.demo.proxy;
public class main { public static void main(String[] args) { Subject subject = new ProxySubject(); subject.doSomething(); } }
|
代理模式可以对实现类进行管理,增加新的功能(装饰模式与代理模式类似,但装饰模式是增强具体类功能),如打印日志、校验权限、通知的功能,避免了修改具体实现类。满足开闭原则。最典型的就是AOP。
但是静态代理这个模式本身有个大问题,针对每一个具体类都需要有一个代理类,麻烦
jdk动态代理
动态代理类必须实现InvocationHandler接口,实现其invoke方法,并使用Proxy类
1 2 3 4 5 6
|
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable;
|
Proxy类用于动态创建一个代理对象,主要方法newProxyInstance用来得到一个代理对象
jdk动态代理类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| package com.husky.demo.proxy;
import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy;
public class DynamicProxyHandler implements InvocationHandler { private Object object; public Object newProxyInstance(Object object){ this.object = object;
return Proxy.newProxyInstance(object.getClass().getClassLoader(), object.getClass().getInterfaces(),this); }
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("execute DynamicProxyHandler method start"); Object result = method.invoke(object,args); System.out.println("execute DynamicProxyHandler method end"); return result; }
}
|
调用运行
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package com.husky.demo.proxy;
import java.lang.reflect.Proxy;
public class main { public static void main(String[] args) { DynamicProxyHandler dynamicProxyHandler = new DynamicProxyHandler(); Subject subject = (Subject) dynamicProxyHandler.newProxyInstance(new RealSubject()); subject.doSomething();
} }
|
Cglib动态代理
1 2 3 4 5
| <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>3.3.0</version> </dependency>
|
CglibProxy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
public class CglibProxy implements MethodInterceptor { private Object object; public Object newInstance(Object obj) { this.object = obj; Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(obj.getClass()); enhancer.setCallback(this); return enhancer.create(); } @Override public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable { System.out.println("execute DynamicProxyHandler method start"); Object result = method.invoke(object, args); System.out.println("execute DynamicProxyHandler method end"); return result; } }
|
调用运行
1 2 3 4 5 6 7 8 9 10 11 12
|
public class Main { public static void main(String[] args) { CglibProxy cglibProxy = new CglibProxy(); Subject subject = (Subject)cglibProxy.newInstance(new RealSubject()); subject.doSomething(); } }
|