diff --git a/.idea/workspace.xml b/.idea/workspace.xml index e99ca21..8e50812 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,9 +2,8 @@ - - - + + @@ -19,11 +18,45 @@ - + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -42,8 +75,8 @@ - - + + @@ -52,8 +85,18 @@ - - + + + + + + + + + + + + @@ -107,6 +150,8 @@ @@ -160,6 +205,13 @@ + + + + + + + @@ -195,7 +247,7 @@ - + @@ -231,6 +283,25 @@ + + + + + + - + + - - + + + @@ -269,12 +342,20 @@ - @@ -299,7 +380,7 @@ - + @@ -315,8 +396,14 @@ + + + - + + @@ -344,6 +431,14 @@ + + + + + + + + @@ -354,35 +449,63 @@ - - + + - - - - - - - - - - + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/out/production/JavaBase/com/fjy/proxy/StaticProxy/Client.class b/out/production/JavaBase/com/fjy/proxy/StaticProxy/Client.class index 475e7b3..73cba62 100644 Binary files a/out/production/JavaBase/com/fjy/proxy/StaticProxy/Client.class and b/out/production/JavaBase/com/fjy/proxy/StaticProxy/Client.class differ diff --git a/src/com/fjy/proxy/JDKProxy/TimeHandler.java b/src/com/fjy/proxy/JDKProxy/TimeHandler.java new file mode 100644 index 0000000..4c18d1b --- /dev/null +++ b/src/com/fjy/proxy/JDKProxy/TimeHandler.java @@ -0,0 +1,35 @@ +package com.fjy.proxy.JDKProxy; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; + +public class TimeHandler implements InvocationHandler { + + private Object target; + + public TimeHandler(Object target) { + super(); + this.target = target; + } + + /** + * + * @param proxy 被代理对象 + * @param method 被代理对象的方法 + * @param args 方法的参数 + * @return Object方法的返回值 + * @throws Throwable + */ + @Override + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { + long startTime = System.currentTimeMillis(); + System.out.println("汽车开始行驶..."); + + method.invoke(target); + + long endTime = System.currentTimeMillis(); + System.out.println("汽车结束行驶.... 汽车行驶时间:" + + (endTime - startTime) + "毫秒!"); + return null; + } +} diff --git a/src/com/fjy/proxy/JDKProxy/TimeHandlerTest.java b/src/com/fjy/proxy/JDKProxy/TimeHandlerTest.java new file mode 100644 index 0000000..7b2bbf3 --- /dev/null +++ b/src/com/fjy/proxy/JDKProxy/TimeHandlerTest.java @@ -0,0 +1,25 @@ +package com.fjy.proxy.JDKProxy; + +import com.fjy.proxy.StaticProxy.Car; +import com.fjy.proxy.StaticProxy.MoveAble; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Proxy; + +public class TimeHandlerTest { + //时间JDK动态测试类 + public static void main(String[] args) { + Car car = new Car(); + InvocationHandler handler = new TimeHandler(car); + Class carClass = car.getClass();//通过反射获取类类型 + + /*三个参数分别是:类加载器、实现接口、时间处理器 + newProxyInstance(ClassLoader loader,Class[] interfaces,InvocationHandler h) + 由于都实现了MoveAble接口,故将返回的动态代理对象强制类型转换为MoveAble接口对象 + */ + MoveAble moveAble = (MoveAble)Proxy.newProxyInstance(carClass.getClassLoader(), + carClass.getInterfaces(), handler); + + moveAble.run(); + } +} diff --git a/src/com/fjy/proxy/StaticProxy/Client.java b/src/com/fjy/proxy/StaticProxy/Client.java index 2ec46b7..26228ea 100644 --- a/src/com/fjy/proxy/StaticProxy/Client.java +++ b/src/com/fjy/proxy/StaticProxy/Client.java @@ -6,8 +6,8 @@ public class Client { * @param args */ public static void main(String[] args) { - RunByCarProxy(); - RunByCarAggregationProxy(); + //RunByCarProxy(); + //RunByCarAggregationProxy(); RunByModulProxy(); } //直接运行 @@ -37,7 +37,6 @@ public class Client { CarLogProxy carLogProxy = new CarLogProxy(car); CarTimeProxy carTimeProxy = new CarTimeProxy(carLogProxy); carTimeProxy.run(); - }