動(dòng)態(tài)代理的代碼
public interface SaleComputer {
?? ?public String sale(double money);
?? ?
?? ?public void show();
}
package cn.itcast.proxy;
/*
?* 真實(shí)類(lèi)
?*/
public class Lenovo implements SaleComputer {
?? ?@Override
?? ?public String sale(double money) {
?? ??? ?System.out.println("花了"+money+"元買(mǎi)了一臺(tái)聯(lián)想電腦");
?? ??? ?return "聯(lián)想電腦";
?? ?}
?? ?@Override
?? ?public void show() {
?? ??? ?System.out.println("展示電腦...");
?? ??? ?
?? ?}
?? ?
}
package cn.itcast.proxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class ProxyTest {
?? ?public static void main(String[] args) {
?? ??? ?//1.創(chuàng)建真實(shí)對(duì)象
?? ??? ?Lenovo lenovo=new Lenovo();
?? ??? ?/*
?? ??? ? * 三個(gè)參數(shù):
?? ??? ? * ?? ??? ??? ?1.類(lèi)加載器:真實(shí)對(duì)象.getClass().getClassLoader()
?? ??? ? * ?? ??? ??? ?2.接口數(shù)組:真實(shí)對(duì)象.getClass().getInterfaces()???? 保證代理對(duì)象和真實(shí)對(duì)象實(shí)現(xiàn)相同的接口
?? ??? ? * ?? ??? ??? ?3.處理器;new InvocationHandler(){}??? 匿名內(nèi)部類(lèi)的寫(xiě)法
?? ??? ? * ?? ??? ??? ??? ?里面是我們核心業(yè)務(wù)邏輯的處理
?? ??? ? */?? ??? ??? ?
?? ??? ?
?? ??? ?//2.動(dòng)態(tài)代理增強(qiáng)lenovo對(duì)象
?? ??? ?//最終返回的就是代理對(duì)象
?? ??? ?//因?yàn)閷?shí)現(xiàn)的是相同的接口可以強(qiáng)轉(zhuǎn)成接口類(lèi)型
?? ??? ?//大類(lèi)型轉(zhuǎn)小類(lèi)型要強(qiáng)轉(zhuǎn)
?? ??? ?SaleComputer proxy_lenovo =(SaleComputer) Proxy.newProxyInstance(lenovo.getClass().getClassLoader(), lenovo.getClass().getInterfaces(), new InvocationHandler() {
?? ??? ??? ?/*
?? ??? ??? ? * 代理邏輯進(jìn)行編寫(xiě)的方法:代理對(duì)象調(diào)用的所有方法都會(huì)觸發(fā)該方法的執(zhí)行
?? ??? ??? ? * ?? ??? ?參數(shù):
?? ??? ??? ? * ?? ??? ??? ?1.proxy:代理對(duì)象?? 這里指的就是:proxy_lenovo
?? ??? ??? ? * ?? ??? ??? ?2.method:代理對(duì)象調(diào)用的方法封裝為對(duì)象
?? ??? ??? ? * ?? ??? ??? ?3.atgs:代理對(duì)象調(diào)用方法傳遞的實(shí)際參數(shù)
?? ??? ??? ? *
?? ??? ??? ? */
?? ??? ??? ?@Override
?? ??? ??? ?public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//?? ??? ??? ??? ?System.out.println("該方法執(zhí)行了");
//?? ??? ??? ??? ?System.out.println(method.getName());
//?? ??? ??? ??? ?System.out.println(args[0]);
?? ??? ??? ??? ?//判斷是否是sale方法
?? ??? ??? ??? ?if(method.getName().equals("sale")) {
?? ??? ??? ??? ??? ?//1.增強(qiáng)參數(shù)
?? ??? ??? ??? ??? ?double money=(double)args[0];
?? ??? ??? ??? ??? ?money=money*0.85;
?? ??? ??? ??? ??? ?System.out.println("專(zhuān)車(chē)接你");
?? ??? ??? ??? ??? ?//使用真實(shí)對(duì)象調(diào)用該方法
?? ??? ??? ??? ??? ?String obj = (String)method.invoke(lenovo, money);
?? ??? ??? ??? ??? ?System.out.println("免費(fèi)送貨");
?? ??? ??? ??? ??? ?//2.增強(qiáng)返回值
?? ??? ??? ??? ??? ?
?? ??? ??? ??? ??? ?return obj+"_鼠標(biāo)墊";
?? ??? ??? ??? ??? ?
?? ??? ??? ??? ?}else {
?? ??? ??? ??? ??? ?Object obj = method.invoke(lenovo, args);
?? ??? ??? ??? ??? ?return obj;
?? ??? ??? ??? ?}
?? ??? ??? ??? ?
?? ??? ??? ?}
?? ??? ?});
?? ??? ?
?? ??? ?//2.調(diào)用方法
?? ??? ?String computer = proxy_lenovo.sale(8000);
?? ??? ?System.out.println(computer);
?? ?}
}
標(biāo)簽: