前面做了一個(gè)Waiter的動(dòng)態(tài)代理的練習(xí),這篇繼續(xù)來寫一個(gè)練習(xí),一個(gè)動(dòng)態(tài)代理工廠的例子,這個(gè)例子在以后學(xué)習(xí)Spring的時(shí)候會(huì)有幫助。這個(gè)動(dòng)態(tài)工廠的主要作用就是包裝了下目標(biāo)對(duì)象和前置增強(qiáng)和后置增強(qiáng)。
成都創(chuàng)新互聯(lián)是一家專業(yè)的成都網(wǎng)站建設(shè)公司,我們專注成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站、網(wǎng)絡(luò)營(yíng)銷、企業(yè)網(wǎng)站建設(shè),外鏈,廣告投放平臺(tái)為企業(yè)客戶提供一站式建站解決方案,能帶給客戶新的互聯(lián)網(wǎng)理念。從網(wǎng)站結(jié)構(gòu)的規(guī)劃UI設(shè)計(jì)到用戶體驗(yàn)提高,創(chuàng)新互聯(lián)力求做到盡善盡美。
工廠類的使用步驟
我們要設(shè)計(jì)一個(gè)動(dòng)態(tài)代理工廠類,以下是使用這個(gè)工程類的步驟。
來源:凱哥Java(kaigejava)
1.?創(chuàng)建代理工廠
2.?給工廠設(shè)置三樣?xùn)|西
1)?目標(biāo)對(duì)象,調(diào)用方法setTargetObject()
2)?前置增強(qiáng),調(diào)用setBeforeAdvice(該接口的具體實(shí)現(xiàn))
3)?后置增強(qiáng),調(diào)用setAfterAdvice(該接口的具體實(shí)現(xiàn))
3.?調(diào)用createProxy方法得到代理對(duì)象
執(zhí)行代理對(duì)象方法時(shí),按照以下順序執(zhí)行。
1)?執(zhí)行BeforeAdvice的before方法
2)?執(zhí)行目標(biāo)對(duì)象的目標(biāo)方法
3)?執(zhí)行AfterAdvice的after方法
前提準(zhǔn)備
新建一個(gè)包,把前面的Waiter.java 和ManWaiter.java拷貝過來。
創(chuàng)建動(dòng)態(tài)代理工廠類
這里我們的代理工廠類,先給出目標(biāo)對(duì)象,前置增強(qiáng)和后置增強(qiáng),然后創(chuàng)建set和get方法。
下面是前置增強(qiáng)接口代碼
package?demo1;
/**
*?????前置增強(qiáng)??
*?@author?anthony
*
*/
public?interface?BeforeAdvice?{
????
????public?void?before();
}
下面是后置增強(qiáng)接口代碼
package?demo1;
/**
*????后置增強(qiáng)
*?@author?anthony
*
*/
public?interface?AfterAdvice?{
????public?void?after();
}
這里面,我們暫時(shí)不知道程序員會(huì)在前置增強(qiáng)和后置增強(qiáng)寫什么具體代碼,所以這里我們利用接口來實(shí)現(xiàn),程序員到時(shí)候?qū)懘a的時(shí)候,前置增強(qiáng)必須要實(shí)現(xiàn)前置增強(qiáng)接口中的before方法,后置增強(qiáng)必須實(shí)現(xiàn)后置增強(qiáng)接口中的after方法。
下面是代理工廠類的代碼
package?demo1;
/**
*?????這個(gè)類用來生成代理對(duì)象
*?????需要所有參數(shù)
*?????*目標(biāo)對(duì)象
*?????*增強(qiáng)
*?@author?anthony
*
*/
public?class?ProxyFactory?{
????
????private?Object?targetObject;?//?目標(biāo)對(duì)象
????private?BeforeAdvice?beforeAdvice;?//?前置增強(qiáng)
????private?AfterAdvice?afterAdvice;?//?后置增強(qiáng)
????
????
????public?Object?getTargetObject() {
????????return?targetObject;
????}
????public?void?setTargetObject(Object?targetObject) {
????????this.targetObject?=?targetObject;
????}
????public?BeforeAdvice?getBeforeAdvice() {
????????return?beforeAdvice;
????}
????public?void?setBeforeAdvice(BeforeAdvice?beforeAdvice) {
????????this.beforeAdvice?=?beforeAdvice;
????}
????public?AfterAdvice?getAfterAdvice() {
????????return?afterAdvice;
????}
????public?void?setAfterAdvice(AfterAdvice?afterAdvice) {
????????this.afterAdvice?=?afterAdvice;
????}
}
這里三個(gè)對(duì)象都給生成set和get方法,方便調(diào)用。
創(chuàng)建代理對(duì)象
前面代碼都不是重點(diǎn),我們代理工廠重點(diǎn)部分來了,就是如何創(chuàng)建代理對(duì)象。這部分是在工廠類中,實(shí)現(xiàn)createProxy()方法,是本篇的重點(diǎn)內(nèi)容,代碼如下。
package?demo1;
import?java.lang.reflect.InvocationHandler;
import?java.lang.reflect.Method;
import?java.lang.reflect.Proxy;
import?org.omg.CORBA.portable.InvokeHandler;
/**
*?????這個(gè)類用來生成代理對(duì)象
*?????需要所有參數(shù)
*?????*目標(biāo)對(duì)象
*?????*增強(qiáng)
*?@author?anthony
*
*/
public?class?ProxyFactory?{
????
????private?Object?targetObject;?//?目標(biāo)對(duì)象
????private?BeforeAdvice?beforeAdvice;?//?前置增強(qiáng)
????private?AfterAdvice?afterAdvice;?//?后置增強(qiáng)
????
????/**
?????*????生成代理對(duì)象
?????*?@return
?????*/
????public?Object?createProxy() {
????????// 1.?給出三大參數(shù)
????????ClassLoader?loader?=?this.getClass().getClassLoader();
????????Class[]?interfaces?=?targetObject.getClass().getInterfaces();
????????InvocationHandler?h?=?new?InvocationHandler() {
????????????public?Object?invoke(Object?proxy, Method?method, Object[]?args)?throws?Throwable {
????????????????//?執(zhí)行前置增強(qiáng)
????????????????if(beforeAdvice?!=?null) {
????????????????????beforeAdvice.before();
????????????????}
????????????????//?執(zhí)行目標(biāo)對(duì)象目標(biāo)方法
????????????????Object?result?=?method.invoke(targetObject,?args);
????????????????//?執(zhí)行后置增強(qiáng)
????????????????if(afterAdvice?!=?null) {
????????????????????afterAdvice.after();
????????????????}
????????????????return?result;
????????????}
????????};
????????
????????// 2.?得到代理對(duì)象
????????Object?proxyObject?=?Proxy.newProxyInstance(loader,?interfaces,?h);
????????return?proxyObject;
????}
????
????public?Object?getTargetObject() {
????????return?targetObject;
????}
????public?void?setTargetObject(Object?targetObject) {
????????this.targetObject?=?targetObject;
????}
????public?BeforeAdvice?getBeforeAdvice() {
????????return?beforeAdvice;
????}
????public?void?setBeforeAdvice(BeforeAdvice?beforeAdvice) {
????????this.beforeAdvice?=?beforeAdvice;
????}
????public?AfterAdvice?getAfterAdvice() {
????????return?afterAdvice;
????}
????public?void?setAfterAdvice(AfterAdvice?afterAdvice) {
????????this.afterAdvice?=?afterAdvice;
????}
}
上面代碼就是在createProxy()方法的時(shí)候,把目標(biāo)對(duì)象和增強(qiáng)給做了以下組裝。接下來,看看如何代碼使用這個(gè)工廠類。
測(cè)試工廠類的使用
新建一個(gè)Demo1.java文件,寫代碼來使用ProxyFactory這個(gè)動(dòng)態(tài)代理工廠類。
package?demo1;
import?org.junit.Test;
public?class?Demo1 {
????
????@Test
????public?void?fun1() {
????????// 1.?創(chuàng)建工廠類對(duì)象
????????ProxyFactory?factory?=?new?ProxyFactory();
????????//?2.設(shè)置目標(biāo)對(duì)象
????????factory.setTargetObject(new?ManWaiter());
????????//?3.設(shè)置前置增強(qiáng)
????????factory.setBeforeAdvice(new?BeforeAdvice() {
????????????public?void?before() {
????????????????System.out.println("你好,歡迎光臨!");
????????????}
????????});
????????//?4.設(shè)置后置增強(qiáng)
????????factory.setAfterAdvice(new?AfterAdvice() {
????????????public?void?after() {
????????????????System.out.println("再見,歡迎下次再來!");
????????????}
????????});
????????
????????// 5.?獲取代理對(duì)象,?執(zhí)行增強(qiáng)代碼
????????Waiter?waiter?= (Waiter)factory.createProxy();
????????waiter.serve();
????}
}
運(yùn)行單元測(cè)試,得到如下輸出。
聯(lián)系凱哥:凱哥Java(kaigejava)
凱哥個(gè)人博客:www.kaigejava.com
個(gè)人博客上可以發(fā)即時(shí)消息給凱哥哦~
你好,歡迎光臨!
服務(wù)中...
再見,歡迎下次再來!