1. 介绍
代理模式给某一个对象提供一个代理对象,并由代理对象控制对原对象的引用。通俗的来讲代理模式就是我们生活中常见的中介。
2. 应用场景
日志记录,在一个方法处理前后增加自己的功能
3. 代码实现
3.1 静态代理
3.1.1 代理接口
public interface Moveable {
void go();
}
3.1.2 接口实现类
public class Bike implements Moveable {
@Override
public void go() {
System.out.println(" bike 蹬蹬蹬");
}
}
public class Car implements Moveable {
@Override
public void go() {
System.out.println("car 咻咻咻......");
}
}
3.1.3 代理类
public class LogProxy implements Moveable {
private Moveable m;
public LogProxy(Moveable m) {
this.m = m;
}
@Override
public void go() {
System.out.println("开始记录日志");
m.go();
System.out.println("结束记录日志");
}
}
public class TimeProxy implements Moveable {
private Moveable m;
public TimeProxy(Moveable m) {
this.m = m;
}
@Override
public void go() {
System.out.println("开始发动时间");
m.go();
System.out.println("发动结束");
}
}
3.1.4 调用入口
public class Main {
public static void main(String[] args) {
Moveable a = new TimeProxy(new LogProxy(new Car()));
a.go();
System.out.println("===========================================");
Moveable b = new TimeProxy(new LogProxy(new Train()));
b.go();
}
}
3.2 jdk动态代理
必须继承接口
3.2.1 接口类
public interface Moveable {
void go();
}
3.2.2 接口实现类
public class Bike implements Moveable {
@Override
public void go() {
System.out.println(" bike 蹬蹬蹬");
}
}
public class Car implements Moveable {
@Override
public void go() {
System.out.println("car 咻咻咻......");
}
}
3.2.3 代理类
public class TimeHandler implements InvocationHandler {
private Moveable m;
public TimeHandler(Moveable m) {
this.m = m;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("开始发动时间");
Object obj = method.invoke(m, args);
System.out.println("发送结束");
return obj;
}
}
3.2.4 调用入口
public class Main {
public static void main(String[] args) {
System.setProperty("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");
Moveable car = new Car();
//reflection 通过二进制字节码分析类的属性和方法
Moveable m = (Moveable)Proxy.newProxyInstance(Moveable.class.getClassLoader(),
new Class[]{Moveable.class}, //tank.class.getInterfaces()
new TimeHandler(car)
);
m.go();
}
}
3.3 cglib代理
pom文件
<!-- https://mvnrepository.com/artifact/cglib/cglib -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.3.0</version>
</dependency>
3.3.1 实现类
可以不继承接口
public class Bike implements Moveable {
@Override
public void go() {
System.out.println(" bike 蹬蹬蹬");
}
}
3.3.2 代理类
public class TimeProxy implements MethodInterceptor {
@Override
public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
System.out.println("开始发动");
Object result = methodProxy.invokeSuper(o, args);
System.out.println("发送结束");
return result;
}
}
3.3.4 调用入口
public class TimeProxy implements MethodInterceptor {
@Override
public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
System.out.println("开始发动");
Object result = methodProxy.invokeSuper(o, args);
System.out.println("发送结束");
return result;
}
}
来源:oschina
链接:https://my.oschina.net/u/4160362/blog/4295231