課程
/后端開發
/Java
/模式的秘密---代理模式
作業怎么實現有同時記錄時間和日子?
2016-07-24
源自:模式的秘密---代理模式 2-4
正在回答
package?com.jdkproxy; import?java.lang.reflect.InvocationHandler; import?java.lang.reflect.Method; /* ?*?日志 ?*/ public?class?LogHandler?implements?InvocationHandler?{ private?Object?target; public?LogHandler?(Object?target){ super(); this.target?=?target; } public?Object?getTarget()?{ return?target; } public?void?setTarget(Object?target)?{ this.target?=?target; } @Override public?Object?invoke(Object?proxy,?Method?method,?Object[]?args) throws?Throwable?{ System.out.println("日志記錄開始。。。"); method.invoke(target); System.out.println("日志記錄結束。。。"); return?null; } }
package?com.jdkproxy; import?java.lang.reflect.InvocationHandler; import?java.lang.reflect.Proxy; import?com.proxy.Car; import?com.proxy.Moveable; public?class?Text?{ /** ?*?JDK動態代理測試類 ?*? ?*?動態代理實現步驟 ?*?1.創建一個實現InvocationHandler接口的類,它必須實現invoke方法 ?*?2.創建被代理的類和接口 ?*?3.調用Proxy的靜態方法,創建一個代理類newProxyInstance() ?*?4.通過代理調用方法 ?*? ?*/ public?static?void?main(String[]?args)?{ Car?car?=?new?Car(); InvocationHandler?h?=?new?TimeHandler(car); Class<?>?cls=?car.getClass(); /* ?*?loader?類加載器 ?*?interfaces?實現接口 ?*?h?InvocationHandler ?*/ Moveable?m?=?(Moveable)Proxy.newProxyInstance(cls.getClassLoader(),?cls.getInterfaces(),?h); //同時實現時間和日志的動態代理 InvocationHandler?l?=?new?LogHandler(m); Moveable?m1?=?(Moveable)Proxy.newProxyInstance(cls.getClassLoader(),?cls.getInterfaces(),?l); m1.move(); } }
package com.jdkproxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
/*
?* 日志
?*/
public class LogHandler implements InvocationHandler {
private Object target;
public LogHandler (Object target){
super();
this.target = target;
}
public Object getTarget() {
return target;
public void setTarget(Object target) {
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
System.out.println("日志記錄開始。。。");
method.invoke(target);
System.out.println("日志記錄結束。。。");
return null;
public?class?Test?{ public?static?void?main(String[]?args)?{ Car?car?=?new?Car(); //timeHandler是添加了time的car代理 InvocationHandler?timeHandler=??new?TimeProxy(car); Class<?>?cls?=?car.getClass(); Move?timemove?=?(Move)?Proxy.newProxyInstance(cls.getClassLoader(),?cls.getInterfaces(),?timeHandler); //注意此處timemove代替car,logHandler則為添加了time和log的car代理 InvocationHandler?logHandler?=??new?LogProxy(timemove); /** ?*?loader:類加載器, ?*?interfaces:實現接口 ?*?h:InvocationHandler ?*/ Move?logmove?=?(Move)?Proxy.newProxyInstance(cls.getClassLoader(),?cls.getInterfaces(),?logHandler); logmove.move(); } }
public?static?void?main(String[]?args)?{ Car?car?=?new?Car(); InvocationHandler?h?=??new?JDKProxy(car); Class<?>?cls?=?car.getClass(); Move?m?=?(Move)?Proxy.newProxyInstance(cls.getClassLoader(),?cls.getInterfaces(),?h); InvocationHandler?h1?=??new?LogProxy(m); /** ?*?loader:類加載器, ?*?interfaces:實現接口 ?*?h:InvocationHandler ?*/ Move?m1?=?(Move)?Proxy.newProxyInstance(cls.getClassLoader(),?cls.getInterfaces(),?h1); m1.move(); }
把相關信息都加一個里面
舉報
本節課程將帶你領略Java編程語言中代理模式的奧妙
1 回答動態代理作業
3 回答如何在jdk動態代理的中實現多個代理?(時間代理,日志代理)
3 回答jdk動態代理的問題
3 回答關于JDK動態代理的問題
1 回答在第二章jdk動態代理中 要實現多事物的處理處理,測試類要如何編寫?
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2016-12-07
2016-12-07
package com.jdkproxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
/*
?* 日志
?*/
public class LogHandler implements InvocationHandler {
private Object target;
public LogHandler (Object target){
super();
this.target = target;
}
public Object getTarget() {
return target;
}
public void setTarget(Object target) {
this.target = target;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
System.out.println("日志記錄開始。。。");
method.invoke(target);
System.out.println("日志記錄結束。。。");
return null;
}
}
2016-08-11
2016-08-11
2016-07-25
把相關信息都加一個里面