1.静态代理
接口 Moveable
package com.hwj.Daili;
/**
* @Auther: hwj
* @Date: 2018/8/24 14:44
* @Description:
*/
public interface Moveable {
void move();
}
Car实现了Moveale接口
package com.hwj.Daili;
import java.util.Random;
/**
* @Auther: hwj
* @Date: 2018/8/24 14:44
* @Description:
*/
public class Car implements Moveable{
@Override
public void move() {
try {
Thread.sleep(new Random().nextInt(1000));
System.out.println("行驶中");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
时间代理类:
package com.hwj.Daili;
/**
* @Auther: hwj
* @Date: 2018/8/24 14:55
* @Description:
*/
public class CarTimeProxy implements Moveable{
private Moveable moveable;
public CarTimeProxy(Moveable moveable){
super();
this.moveable=moveable;
}
@Override
public void move() {
long start=System.currentTimeMillis();
System.out.println("开始行驶");
moveable.move();
long end=System.currentTimeMillis();
System.out.println("行驶时间"+(end-start)+"毫秒");
}
}
测试类:
package com.hwj.Daili;
/**
* @Auther: hwj
* @Date: 2018/8/24 14:48
* @Description:
*/
public class Test {
public static void main(String[] args) {
Car car=new Car();
CarTimeProxy car2=new CarTimeProxy(car);
car2.move();
}
}
运行结果:
开始行驶
行驶中
行驶时间573毫秒
如果在时间代理类中加一个日志代理类:
package com.hwj.Daili;
/**
* @Auther: hwj
* @Date: 2018/8/24 15:13
* @Description:
*/
public class CarLogProxy implements Moveable{
private Moveable moveable;
public CarLogProxy(Moveable moveable){
super();
this.moveable=moveable;
}
@Override
public void move() {
long start=System.currentTimeMillis();
System.out.println("日志开始");
moveable.move();
long end=System.currentTimeMillis();
System.out.println("日志结束");
}
}
测试类:
package com.hwj.Daili;
/**
* @Auther: hwj
* @Date: 2018/8/24 14:48
* @Description:
*/
public class Test {
public static void main(String[] args) {
Car car=new Car();
CarTimeProxy car2=new CarTimeProxy(car);
CarLogProxy carLogProxy=new CarLogProxy(car2);
carLogProxy.move();
}
}
运行结果:
日志开始
开始行驶
行驶中
行驶时间507毫秒
日志结束
2.动态代理
时间代理类:
package com.hwj.jdkproxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
/**
* @Auther: hwj
* @Date: 2018/8/24 15:25
* @Description:
*/
public class TimeHandler implements InvocationHandler {
private Object target;
public TimeHandler(Object target){
this.target=target;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
/**
*@描述
*@参数 [proxy被代理的对象, method 被代理对象的方法, args 方法参数]
*@返回值 java.lang.Object
*@创建人 hwj
*@创建时间 2018/8/24
*/
long start=System.currentTimeMillis();
System.out.println("开始行驶");
method.invoke(target);
long end=System.currentTimeMillis();
System.out.println("行驶时间"+(end-start)+"毫秒");
return null;
}
}
测试:
package com.hwj.jdkproxy;
import com.hwj.Daili.Car;
import com.hwj.Daili.Moveable;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
/**
* @Auther: hwj
* @Date: 2018/8/24 15:31
* @Description:
*/
public class TestTwo {
public static void main(String[] args) {
Car car=new Car();
InvocationHandler h=new TimeHandler(car);
Moveable mv= (Moveable) Proxy.newProxyInstance(car.getClass().getClassLoader(), car.getClass().getInterfaces(),h);
mv.move();
}
}
结果:
开始行驶
行驶中
行驶时间83毫秒
在时间代理类中加一个日志代理类:
package com.hwj.jdkproxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
/**
* @Auther: hwj
* @Date: 2018/8/24 15:46
* @Description:
*/
public class LogHandler implements InvocationHandler {
private Object target;
public LogHandler(Object target){
this.target=target;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
long start=System.currentTimeMillis();
System.out.println("日志开始");
method.invoke(target);
long end=System.currentTimeMillis();
System.out.println("日志结束");
return null;
}
}
测试:
package com.hwj.jdkproxy;
import com.hwj.Daili.Car;
import com.hwj.Daili.Moveable;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
/**
* @Auther: hwj
* @Date: 2018/8/24 15:31
* @Description:
*/
public class TestTwo {
public static void main(String[] args) {
Car car=new Car();
InvocationHandler h=new TimeHandler(car);
Moveable mv= (Moveable) Proxy.newProxyInstance(car.getClass().getClassLoader(), car.getClass().getInterfaces(),h);
InvocationHandler h2=new LogHandler(mv);
Moveable m2=(Moveable) Proxy.newProxyInstance(car.getClass().getClassLoader(), car.getClass().getInterfaces(),h2);
m2.move();
}
}
结果:
日志开始
开始行驶
行驶中
行驶时间908毫秒
日志结束