代理模式

一.代理模式概念

代理(Proxy)是一種設(shè)計(jì)模式, 提供了對(duì)目標(biāo)對(duì)象另外的訪問方式;即通過代理訪問目標(biāo)對(duì)象。 這樣好處: 可以在目標(biāo)對(duì)象實(shí)現(xiàn)的基礎(chǔ)上,增強(qiáng)額外的功能操作。(擴(kuò)展目標(biāo)對(duì)象的功能)。

二.靜態(tài)代理

代理對(duì)象,要實(shí)現(xiàn)與目標(biāo)對(duì)象一樣的接口
1.可以做到在不修改目標(biāo)對(duì)象的功能前提下,對(duì)目標(biāo)對(duì)象功能擴(kuò)展。
2.缺點(diǎn):

  • a.因?yàn)榇韺?duì)象,需要與目標(biāo)對(duì)象實(shí)現(xiàn)一樣的接口。所以會(huì)有很多代理類,類太多。
  • b.一旦接口增加方法,目標(biāo)對(duì)象與代理對(duì)象都要維護(hù)。

接口類:StudentDao .java

package com.huan.a_static;

/**
 * Created by 馬歡歡 on 2017/6/25.
 */
public interface StudentDao {
    void delect();
}

目標(biāo)對(duì)象:StudentDaoImpl.java

package com.huan.a_static;

/**
 * Created by 馬歡歡 on 2017/6/25.
 */
public class StudentDaoImpl implements StudentDao {
    public void delect() {
        System.out.println("刪除數(shù)據(jù)成功");
    }
}

代理對(duì)象:StudentDaoProxy.java

package com.huan.a_static;

/**
 * Created by 馬歡歡 on 2017/6/25.
 */
public class StudentDaoProxy implements StudentDao {
    //接收保存目標(biāo)對(duì)象
    private StudentDao target;
    public StudentDaoProxy(StudentDao target){
        this.target=target;
    }
    public void delect() {
        System.out.println("開始事務(wù)");
        target.delect();//執(zhí)行目標(biāo)對(duì)象的方法
        System.out.println("結(jié)束事務(wù)");
    }
}

測(cè)試用例:

package com.huan.a_static;

import com.huan.BeanTest.UserDao;

/**
 * Created by 馬歡歡 on 2017/6/25.
 */
public class Test {
    public static void main(String[] args){
        //目標(biāo)對(duì)象
        StudentDao studentDao = new StudentDaoImpl();

        //代理
        StudentDao proxy = new StudentDaoProxy(studentDao);
        proxy.delect();
    }
}

三.動(dòng)態(tài)代理

1)代理對(duì)象,不需要實(shí)現(xiàn)接口;
2)代理對(duì)象的生成,是利用JDKAPI, 動(dòng)態(tài)的在內(nèi)存中構(gòu)建代理對(duì)象(需要我們指定創(chuàng)建 代理對(duì)象/目標(biāo)對(duì)象 實(shí)現(xiàn)的接口的類型);

  1. 動(dòng)態(tài)代理, JDK代理, 接口代理;
動(dòng)態(tài)代理總結(jié):

` 代理對(duì)象不需要實(shí)現(xiàn)接口,但是目標(biāo)對(duì)象一定要實(shí)現(xiàn)接口;否則不能用動(dòng)態(tài)代理!

接口類:StudentDao .java

package com.huan.b_dynameic;

/**
 * Created by 馬歡歡 on 2017/6/25.
 */
public interface StudentDao {
    void delect();
}

目標(biāo)對(duì)象:StudentDaoImpl.java

package com.huan.b_dynameic;

/**
 * Created by 馬歡歡 on 2017/6/25.
 */
public class StudentDaoImpl implements StudentDao {
    public void delect() {
        System.out.println("刪除數(shù)據(jù)成功");
    }
}

代理對(duì)象:StudentDaoProxy.java


package com.huan.b_dynameic;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * Created by 馬歡歡 on 2017/6/25.
 */
public class ProxyFactory  {
    //接收保存目標(biāo)對(duì)象
    private Object target;
    public ProxyFactory(Object target){
        this.target = target;
    }
    //給目標(biāo)對(duì)象生成代理對(duì)象
    public  Object getProxyFactory() {
        return Proxy.newProxyInstance(target.getClass().getClassLoader(),
                target.getClass().getInterfaces(),
                new InvocationHandler() {
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        System.out.println("開啟事務(wù)");
                        //執(zhí)行目標(biāo)對(duì)象
                        Object returnValue = method.invoke(target,args);
                        System.out.println("提交事務(wù)");
                        return returnValue;
                    }
                });

    }
}

測(cè)試用例:

package com.huan.b_dynameic;


/**
 * Created by 馬歡歡 on 2017/6/25.
 */
public class Test {
    public static void main(String[] args){
        //目標(biāo)對(duì)象
       StudentDao studentDao =new StudentDaoImpl();

        //代理
        StudentDao proxy = (StudentDao) new ProxyFactory(studentDao).getProxyFactory();
        //執(zhí)行方法
        proxy.delect();

    }
} 

四.Cglib代理

Cglib代理,也叫做子類代理。在內(nèi)存中構(gòu)建一個(gè)子類對(duì)象從而實(shí)現(xiàn)對(duì)目標(biāo)對(duì)象功能的擴(kuò)展。

Cglib子類代理:
  1. 需要引入cglib – jar文件, 但是spring的核心包中已經(jīng)包括了cglib功能,所以直接引入spring-core-3.2.5.jar即可。
    2)引入功能包后,就可以在內(nèi)存中動(dòng)態(tài)構(gòu)建子類
    3)代理的類不能為final, 否則報(bào)錯(cuò)。
    4) 目標(biāo)對(duì)象的方法如果為final/static, 那么就不會(huì)被攔截,即不會(huì)執(zhí)行目標(biāo)對(duì)象額外的業(yè)務(wù)方法。

目標(biāo)對(duì)象:StudentDaoImpl.java

package com.huan.c_cglib;

/**
 * Created by 馬歡歡 on 2017/6/25.
 */
public class StudentDaoImpl  {
    public void delect() {
        System.out.println("刪除數(shù)據(jù)成功");
    }
}

Cglib子類對(duì)象 ProxyFactory .java

package com.huan.c_cglib;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;

/**
 * Created by 馬歡歡 on 2017/6/25.
 */
public class ProxyFactory implements MethodInterceptor {

    /**
     * Cglib子類對(duì)象
     * 對(duì)UserDao 在內(nèi)存中構(gòu)建一個(gè)子類對(duì)象
     */
    //接收保存目標(biāo)對(duì)象
    private Object target;
    public ProxyFactory(Object target){
        this.target = target;
    }

    //給目標(biāo)對(duì)象創(chuàng)建代理對(duì)象
    public Object getProxyInstance(){
        //工具類
        Enhancer en = new Enhancer();
        //設(shè)置父類
        en.setSuperclass(target.getClass());
        //設(shè)置回調(diào)函數(shù)
        en.setCallback(this);
        // 創(chuàng)建子類:代理對(duì)象對(duì)象
        return en.create();
    }

    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("開始事務(wù)---");
        method.invoke(target,objects);
        System.out.println("結(jié)束事務(wù)----");
        return null;
    }
}


測(cè)試用例:

package com.huan.c_cglib;


/**
 * Created by 馬歡歡 on 2017/6/25.
 */
public class Test {
    public static void main(String[] args){
       StudentDaoImpl studentDao = new StudentDaoImpl();
       StudentDaoImpl proxy = (StudentDaoImpl) new ProxyFactory(studentDao).getProxyInstance();
       proxy.delect();

    }
}

上一篇:Spring--IOC容器——對(duì)象依賴關(guān)系(注解)

文集:Spring框架學(xué)習(xí)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容