欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring設(shè)計模式中代理模式詳細講解

 更新時間:2023年01月03日 14:03:13   作者:熱愛編程的小白白  
如何實現(xiàn)在不修改源碼的基礎(chǔ)上實現(xiàn)代碼功能的增強呢?spring為我們提供了代理模式。所謂的代理模式通俗來說就是一個中介,它給某一個對象提供一個代理對象,并由代理對象控制原對象的引用,從而實現(xiàn)在不修改源碼的基礎(chǔ)上實現(xiàn)代碼功能的增強

一、場景模擬

①聲明接口

聲明計算器接口Calculator,包含加減乘除的抽象方法

public interface Calculator {
    int add(int i, int j);
    int sub(int i, int j);
    int mul(int i, int j);
    int div(int i, int j);
}

②創(chuàng)建實現(xiàn)類

public class CalculatorPureImpl implements Calculator {
    @Override
    public int add(int i, int j) {
        int result = i + j;
        System.out.println("方法內(nèi)部 result = " + result);
        return result;
    }
    @Override
    public int sub(int i, int j) {
        int result = i - j;
        System.out.println("方法內(nèi)部 result = " + result);
        return result;
    }
    @Override
    public int mul(int i, int j) {
        int result = i * j;
        System.out.println("方法內(nèi)部 result = " + result);
        return result;
    }
    @Override
    public int div(int i, int j) {
        int result = i / j;
        System.out.println("方法內(nèi)部 result = " + result);
        return result;
    }
}

③創(chuàng)建帶日志功能的實現(xiàn)類

public class CalculatorPureImpl implements Calculator {
    @Override
    public int add(int i, int j) {
        System.out.println("[日志] add 方法開始了,參數(shù)是:" + i + "," + j);
        int result = i + j;
        System.out.println("方法內(nèi)部 result = " + result);
        System.out.println("[日志] add 方法結(jié)束了,結(jié)果是:" + result);
        return result;
    }
    @Override
    public int sub(int i, int j) {
        System.out.println("[日志] sub 方法開始了,參數(shù)是:" + i + "," + j);
        int result = i - j;
        System.out.println("方法內(nèi)部 result = " + result);
        System.out.println("[日志] sub 方法結(jié)束了,結(jié)果是:" + result);
        return result;
    }
    @Override
    public int mul(int i, int j) {
        System.out.println("[日志] mul 方法開始了,參數(shù)是:" + i + "," + j);
        int result = i * j;
        System.out.println("方法內(nèi)部 result = " + result);
        System.out.println("[日志] mul 方法結(jié)束了,結(jié)果是:" + result);
        return result;
    }
    @Override
    public int div(int i, int j) {
        System.out.println("[日志] div 方法開始了,參數(shù)是:" + i + "," + j);
        int result = i / j;
        System.out.println("方法內(nèi)部 result = " + result);
        System.out.println("[日志] div 方法結(jié)束了,結(jié)果是:" + result);
        return result;
    }
}

二、提出問題

①現(xiàn)有代碼缺陷

針對帶日志功能的實現(xiàn)類,我們發(fā)現(xiàn)有如下缺陷:

  • 對核心業(yè)務(wù)功能有干擾,導(dǎo)致程序員在開發(fā)核心業(yè)務(wù)功能時分散了精力
  • 附加功能分散在各個業(yè)務(wù)功能方法中,不利于統(tǒng)一維護

②解決思路

解決這兩個問題,核心就是:解耦。我們需要把附加功能從業(yè)務(wù)功能代碼中抽取出來。

③困難

解決問題的困難:要抽取的代碼在方法內(nèi)部,靠以前把子類中的重復(fù)代碼抽取到父類的方式?jīng)]法解決。 所以需要引入新的技術(shù)。

三、代理模式

①介紹 二十三種設(shè)計模式中的一種,屬于結(jié)構(gòu)型模式。它的作用就是通過提供一個代理類,讓我們在調(diào)用目標方法的時候,不再是直接對目標方法進行調(diào)用,而是通過代理類間接 調(diào)用。讓不屬于目標方法核心邏輯 的代碼從目標方法中剝離出來—— 解耦 。調(diào)用目標方法時先調(diào)用代理對象的方法,減少對目標方法的調(diào)用和打擾,同時讓附加功能能夠集中在一起也有利于統(tǒng)一維護。

使用代理后:

②生活中的代理

  • 廣告商找大明星拍廣告需要經(jīng)過經(jīng)紀人
  • 合作伙伴找大老板談合作要約見面時間需要經(jīng)過秘書
  • 房產(chǎn)中介是買賣雙方的代理

③相關(guān)術(shù)語

  • 代理:將非核心邏輯剝離出來以后,封裝這些非核心邏輯的類、對象、方法。
  • 目標:被代理“套用”了非核心邏輯代碼的類、對象、方法。

1.靜態(tài)代理

創(chuàng)建靜態(tài)代理類:

public class CalculatorStaticProxy implements Calculator {
    // 將被代理的目標對象聲明為成員變量
    private Calculator target;
    public CalculatorStaticProxy(Calculator target) {
        this.target = target;
    }
    @Override
    public int add(int i, int j) {
        System.out.println("[日志] add 方法開始了,參數(shù)是:" + i + "," + j);
        int result = target.add(i,j);
        System.out.println("[日志] add 方法結(jié)束了,結(jié)果是:" + result);
        return result;
    }
    @Override
    public int sub(int i, int j) {
        System.out.println("[日志] sub 方法開始了,參數(shù)是:" + i + "," + j);
        int result = target.sub(i,j);
        System.out.println("[日志] sub 方法結(jié)束了,結(jié)果是:" + result);
        return result;
    }
    @Override
    public int mul(int i, int j) {
        System.out.println("[日志] mul 方法開始了,參數(shù)是:" + i + "," + j);
        int result = target.mul(i,j);
        System.out.println("[日志] mul 方法結(jié)束了,結(jié)果是:" + result);
        return result;
    }
    @Override
    public int div(int i, int j) {
        System.out.println("[日志] div 方法開始了,參數(shù)是:" + i + "," + j);
        int result = target.div(i,j);
        System.out.println("[日志] div 方法結(jié)束了,結(jié)果是:" + result);
        return result;
    }
}

實現(xiàn)類:

public class CalculatorPureImpl implements Calculator {
    @Override
    public int add(int i, int j) {
        int result = i + j;
        System.out.println("方法內(nèi)部 result = " + result);
        return result;
    }
    @Override
    public int sub(int i, int j) {
        int result = i - j;
        System.out.println("方法內(nèi)部 result = " + result);
        return result;
    }
    @Override
    public int mul(int i, int j) {
        int result = i * j;
        System.out.println("方法內(nèi)部 result = " + result);
        return result;
    }
    @Override
    public int div(int i, int j) {
        int result = i / j;
        System.out.println("方法內(nèi)部 result = " + result);
        return result;
    }
}

測試:

    public void test(){
        CalculatorStaticProxy proxy = new CalculatorStaticProxy(new CalculatorPureImpl());
        proxy.add(3,4);
}

靜態(tài)代理確實實現(xiàn)了解耦,但是由于代碼都寫死了,完全不具備任何的靈活性。就拿日志功能來說,將來其他地方也需要附加日志,那還得再聲明更多個靜態(tài)代理類,那就產(chǎn)生了大量重復(fù)的代碼,日志功能還是分散的,沒有統(tǒng)一管理。

提出進一步的需求:將日志功能集中到一個代理類中,將來有任何日志需求,都通過這一個代理類來實現(xiàn)。這就需要使用動態(tài)代理技術(shù)了。

2.動態(tài)代理

生產(chǎn)代理對象的工廠類:

public class ProxyFactory {
    Object target;
    public ProxyFactory(Object target) {
        this.target = target;
    }
    public Object getProxy(){
/**
 * newProxyInstance():創(chuàng)建一個代理實例
 * 其中有三個參數(shù):
 * 1、classLoader:加載動態(tài)生成的代理類的類加載器
 * 2、interfaces:目標對象實現(xiàn)的所有接口的class對象所組成的數(shù)組
 * 3、invocationHandler:設(shè)置代理對象實現(xiàn)目標對象方法的過程,即代理類中如何重寫接口中的抽象方法
*/
        ClassLoader classLoader = target.getClass().getClassLoader();
        Class<?>[] interfaces = target.getClass().getInterfaces();
        InvocationHandler invocationHandler = new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                /**
                 * proxy:代理對象
                 * method:代理對象需要實現(xiàn)的方法,即其中需要重寫的方法
                 * args:method所對應(yīng)方法的參數(shù)
                 */
                Object result = null;
                try {
                    System.out.println("[動態(tài)代理][日志] "+method.getName()+",參數(shù):"+ Arrays.toString(args));
                    result= method.invoke(target, args);
                    System.out.println("[動態(tài)代理][日志] "+method.getName()+",結(jié)果:"+ result);
                }catch (Exception e){
                    e.printStackTrace();
                    System.out.println("[動態(tài)代理][日志] "+method.getName()+",異常:"+e.getMessage());
                }finally {
                    System.out.println("[動態(tài)代理][日志] "+method.getName()+",方法執(zhí)行完畢");
                }
                return result;
            }
        };
        return Proxy.newProxyInstance(classLoader,interfaces,invocationHandler);
    }
}

測試:

    public void test(){
        ProxyFactory proxyFactory = new ProxyFactory(new CalculatorPureImpl());
        Calculator proxy = (Calculator)proxyFactory.getProxy();
        proxy.add(3,99);
 
    }

到此這篇關(guān)于Spring設(shè)計模式中代理模式詳細講解的文章就介紹到這了,更多相關(guān)Spring代理模式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Mybatis中一條SQL使用兩個foreach的問題及解決

    Mybatis中一條SQL使用兩個foreach的問題及解決

    這篇文章主要介紹了Mybatis中一條SQL使用兩個foreach的問題及解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • Java MeteoInfo解析與繪圖代碼教程詳解

    Java MeteoInfo解析與繪圖代碼教程詳解

    這篇文章主要介紹了Java MeteoInfo解析與繪圖代碼教程,對于后端導(dǎo)出圖片的話,就需要添加色階了,這一文很簡單,就涉及色階,名稱,網(wǎng)格刻度線,感興趣的朋友一起看看吧
    2021-10-10
  • Java如何利用Socket進行數(shù)據(jù)讀寫

    Java如何利用Socket進行數(shù)據(jù)讀寫

    這篇文章主要介紹了Java如何利用Socket進行數(shù)據(jù)讀寫,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • java虛擬機鉤子關(guān)閉函數(shù)addShutdownHook的操作

    java虛擬機鉤子關(guān)閉函數(shù)addShutdownHook的操作

    這篇文章主要介紹了java虛擬機鉤子關(guān)閉函數(shù)addShutdownHook的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • 解決springboot遇到autowire注入為null的問題

    解決springboot遇到autowire注入為null的問題

    這篇文章主要介紹了解決springboot遇到autowire注入為null的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • Java鎖之可重入鎖介紹

    Java鎖之可重入鎖介紹

    這篇文章主要介紹了Java鎖之可重入鎖介紹,可重入鎖,也叫做遞歸鎖,指的是同一線程外層函數(shù)獲得鎖之后,內(nèi)層遞歸函數(shù)仍然有獲取該鎖的代碼,但不受影響,需要的朋友可以參考下
    2014-11-11
  • springboot @ConditionalOnMissingBean注解的作用詳解

    springboot @ConditionalOnMissingBean注解的作用詳解

    這篇文章主要介紹了springboot @ConditionalOnMissingBean注解的作用詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • Java并發(fā)編程之線程安全性

    Java并發(fā)編程之線程安全性

    這篇文章主要介紹了Java并發(fā)編程之線程安全性,文章基于Java的相關(guān)內(nèi)容詳細的展開詳細介紹,需要的小伙伴可以參考一下
    2022-04-04
  • java實現(xiàn)統(tǒng)一異常處理的示例

    java實現(xiàn)統(tǒng)一異常處理的示例

    一個全局異常處理類需要處理三類異常1.業(yè)務(wù)類異常,2.運行時異常 ,3.Error,本文給大家介紹java實現(xiàn)統(tǒng)一異常處理的示例,感興趣的朋友一起看看吧
    2021-06-06
  • java8中Stream的使用示例教程

    java8中Stream的使用示例教程

    Stream是Java8的一大亮點,是對容器對象功能的增強,下面這篇文章主要給大家介紹了關(guān)于java8中Stream使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-10-10

最新評論