欧美日操-欧美日韩91-欧美日韩99-欧美日韩ay在线观看-xxxx色-xxxx视频在线

設計模式 — 動態代理模式

2018-03-14 14:24:01 csdn  點擊量: 評論 (0)
動態代理動態代理0 簡介1 類圖2 示例3 源碼分析0 簡介代理模式有兩種形式:靜態代理、動態代理。1 類圖圖片來源網絡2 示例使

動態代理

 

 

 

0. 簡介

 

代理模式有兩種形式:靜態代理、動態代理。

1. 類圖

 

圖片來源網絡 
這里寫圖片描述

2. 示例

 

使用JDK中的Proxy類實現動態代理類的創建;

Proxy.newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler handler);
  • 1

一般的用法:

public void proxy() throws Exception {
    PlayProxy handler= new PlayProxy();
    IPlay  proxy= (IPlay) Proxy.newProxyInstance(IPlay.class.getClassLoader(), new Class[]{IPlay.class}, handler);
    // 這個方法返回值為null,這是由invoke()方法返回的。
    proxy.play("籃球");
}

interface IPlay {
    void play(String name);
}

class PlayProxy implements InvocationHandler {

    // 當IPlay.play()被調用時,invoke()也會被調用。
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("method=" + method + " , args=" + args[0]);
        // 在此處直接添加處理邏輯。
        return null;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

輸出結果:

method=public abstract void com.example.demo.IPlay.play(java.lang.String) , args=籃球


由上面可以看出,雖然動態代理生成了接口的代理對象,但是代理類中沒有實際的處理邏輯,而接口的方法也是沒有實際處理邏輯的,所以要添加處理邏輯,只能在PlayProxy.invoke()中添加,這就增加了代碼的耦合性。

注意: 跟靜態代理相比,動態代理要少寫一個代理類,因為該代理類可以通過Proxy.newProxyInstance() 方法獲得。 
這里涉及到三個類: 
1. IPlay 
2. StudentPlay 
3. PlayProxy

public void proxy() throws Exception {
    StudentPlay student = new StudentPlay();
    PlayProxy handler= new PlayProxy(student);
    IPlay proxy= (IPlay) Proxy.newProxyInstance(IPlay.class.getClassLoader(), new Class[]{IPlay.class}, handler);
    proxy.play("籃球");//代理類執行play()方法
}

interface IPlay {
    void play(String name);
}

class StudentPlay implements IPlay {
    @Override
    public void play(String name) {
        System.out.println("StudentPlay.play(),name=" + name);
    }
}

class PlayProxy<T> implements InvocationHandler {
    // 實際的執行對象
    T target;
    public PlayProxy(T target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("method=" + method + " , args=" + args[0]);
        // 這里實際調用的是target對象中對應的方法,即StudentPlay.play("籃球");
        Object result = method.invoke(target, args);
        return result;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

輸出結果:

method=public abstract void com.example.demo.IPlay.play(java.lang.String) , args=籃球 
StudentPlay.play(),name=籃球

3. 源碼分析

 

源碼基于JDK1.8

// java.lang.reflect.Proxy
public class Proxy implements java.io.Serializable {
    /** parameter types of a proxy class constructor */
    private static final Class<?>[] constructorParams = { InvocationHandler.class };

    public static Object newProxyInstance(ClassLoader loader,
                                          Class<?>[] interfaces,
                                          InvocationHandler h) throws IllegalArgumentException {
        Objects.requireNonNull(h);

        final Class<?>[] intfs = interfaces.clone();
        final SecurityManager sm = System.getSecurityManager();
        if (sm != null) {
            checkProxyAccess(Reflection.getCallerClass(), loader, intfs);
        }

        // 1.獲取一個對interfaces包裝后的代理class
        Class<?> cl = getProxyClass0(loader, intfs);

        /*
         * Invoke its constructor with the designated invocation handler.
         */
        try {
            if (sm != null) {
                checkNewProxyPermission(Reflection.getCallerClass(), cl);
            }
            // 2.將InvocationHandler.class作為代理class的構造參數
            final Constructor<?> cons = cl.getConstructor(constructorParams);
            final InvocationHandler ih = h;
            if (!Modifier.isPublic(cl.getModifiers())) {
                AccessController.doPrivileged(new PrivilegedAction<Void>() {
                    public Void run() {
                        cons.setAccessible(true);
                        return null;
                    }
                });
            }
            // 3.通過構造器創建代理class的實例對象Proxy,該Proxy對象內持有一個InvocationHandler實例。
            return cons.newInstance(new Object[]{h});
        } catch (Exception e) {
            // ...代碼省略...
        }
    }
}
大云網官方微信售電那點事兒

責任編輯:售電衡衡

免責聲明:本文僅代表作者個人觀點,與本站無關。其原創性以及文中陳述文字和內容未經本站證實,對本文以及其中全部或者部分內容、文字的真實性、完整性、及時性本站不作任何保證或承諾,請讀者僅作參考,并請自行核實相關內容。
我要收藏
個贊
?
主站蜘蛛池模板: 91免费看片 | 麻豆短视频app网站 麻豆短视频 | 欧美有码在线观看 | 成人免费一级毛片在线播放视频 | 欧美成人高清在线视频大全 | 91网站免费在线观看 | 久久黄视频 | 手机在线观看精品国产片 | 欧美一区二区三区gg高清影视 | 国产欧美日韩精品一区二 | 中文字幕 日本 | 香蕉久久高清国产精品免费 | 美日韩在线观看 | 免费5xx | 91免费版视频 | 国产网红在线观看 | 亚洲人成绝费网站色ww | 在线播放第一页 | 日韩簧片| 久久久噜噜噜久久 | 久久免费播放视频 | 欧美日韩不卡码一区二区三区 | 思久久 | www.天天操 | 成年女人在线观看片免费视频 | 成人在线免费视频播放 | 成人中文字幕在线观看 | 日韩视频精品在线 | 日本天堂视频 | 国内精品久久久久久久999下 | 日韩精品在线观看免费 | 青青自拍视频 | 国产精品女 | 成人亚洲网站www在线观看 | 特级毛片在线 | 亚洲成片观看四虎永久 | 青青草原伊人网 | 俺也来国产精品欧美在线观看 | 成年人网址在线观看 | xvideos国产在线视频 | 国产午夜精品理论片影院 |