代理模式(學習筆記)

  1. 意圖

  提供對象的替代品或其佔位符代理控制着對於原對象的訪問容許在將請求提交給對象先後進行一些處理java

  2. 動機

   在面向對象的系統中,有些對象因爲某種緣由(好比對象建立的開銷很大,或者某些操做須要安全控制,或者須要進程外的訪問等),直接訪問對象會給使用者或者系統結構帶來不少麻煩      api

  3. 適用性

  • 延遲初始化(虛擬代理)。若是你有一個偶爾使用的重量級服務對象,一直保持該對象運行會消耗系統資源時,可以使用代理模式
  • 訪問控制(保護代理)。若是隻但願特定客戶端使用服務對象,這裏的對象能夠是操做系統中很是重要的部分,而客戶端則是各類已啓動的程序(包括惡意程序),此時可以使用代理模式
  • 本地執行遠程服務 (遠程代理)。適用於服務對象位於遠程服務器上的情形。代理經過網絡傳遞客戶端請求,負責處理全部與網絡相關的複雜細節
  • 記錄日誌請求(日誌記錄代理)。適用於當你須要保存對於服務對象的請求歷史記錄時。代理能夠在向服務傳遞請求前進行記錄
  • 緩存請求結果 (緩存代理)。適用於須要緩存客戶請求結果並對緩存生命週期進行管理時,特別是當返回結果的體積很是大時。代理可對重複請求所需的相同結果進行緩存,還可以使用請求參數做爲索引緩存的鍵值
  • 智能引用。可在沒有客戶端使用某個重量級對象時當即銷燬該對象。

  代理會將全部獲取了指向服務對象或其結果的客戶端記錄在案。代理會時不時地遍歷各個客戶端,檢查它們是否仍在運行。若是相應的客戶端列表爲空,代理就會銷燬該服務對象,釋放底層系統資源。代理還能夠記錄客戶端是否修改了服務對象。其餘客戶端還能夠複用未修改的對象緩存

  4. 結構

  

  5. 效果

  1)能夠在客戶端毫無察覺的狀況下控制服務對象安全

  2)客戶端對服務對象的生命週期沒有特殊要求 你能夠對生命週期進行管理服務器

  3)開閉原則 你能夠在不對服務或客戶端作出修改的狀況下建立新代理網絡

  4)Proxy模式能夠對用戶隱藏另外一種稱爲copy-on-write的優化方式,該優化與根據須要建立對象有關。拷貝一個龐大而複雜的對象是一種開銷很大的操做,若是這個拷貝並無被修改,那麼開銷就沒有必要。能夠用代理延遲這一拷貝過程。在實現copy-on-write時必須對實體進行引用計數。只有當用戶請求一個修改該實體的操做時,代理纔會真正的拷貝它。此時,代理須要減小實體引用計數,當引用數目爲零時,這個實體將被刪除app

  5)服務響應可能會延遲  less

  6. 代碼實現

   some_cool_media_library/ThirdPartyYouTubeLib.java: 遠程服務接口  dom

package proxy.some_cool_media_library;

import java.util.HashMap;

/**
 * @author GaoMing
 * @date 2021/7/20 - 19:25
 */
public interface ThirdPartyYouTubeLib {
    HashMap<String, Video> popularVideos();

    Video getVideo(String videoId);

}

  some_cool_media_library/ThirdPartyYouTubeClass.java: 遠程服務實現ide

package proxy.some_cool_media_library;

import java.util.HashMap;

/**
 * @author GaoMing
 * @date 2021/7/20 - 19:22
 */
public class ThirdPartyYouTubeClass implements ThirdPartyYouTubeLib{
    @Override
    public HashMap<String, Video> popularVideos() {
        connectToServer("http://www.youtube.com");
        return getRandomVideos();
    }

    @Override
    public Video getVideo(String videoId) {
        connectToServer("http://www.youtube.com/" + videoId);
        return getSomeVideo(videoId);
    }

    // -----------------------------------------------------------------------
    // Fake methods to simulate network activity. They as slow as a real life.

    private int random(int min, int max) {
        return min + (int) (Math.random() * ((max - min) + 1));
    }

    private void experienceNetworkLatency() {
        int randomLatency = random(5, 10);
        for (int i = 0; i < randomLatency; i++) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    }

    private void connectToServer(String server) {
        System.out.print("Connecting to " + server + "... ");
        experienceNetworkLatency();
        System.out.print("Connected!" + "\n");
    }

    private HashMap<String, Video> getRandomVideos() {
        System.out.print("Downloading populars... ");

        experienceNetworkLatency();
        HashMap<String, Video> hmap = new HashMap<String, Video>();
        hmap.put("catzzzzzzzzz", new Video("sadgahasgdas", "Catzzzz.avi"));
        hmap.put("mkafksangasj", new Video("mkafksangasj", "Dog play with ball.mp4"));
        hmap.put("dancesvideoo", new Video("asdfas3ffasd", "Dancing video.mpq"));
        hmap.put("dlsdk5jfslaf", new Video("dlsdk5jfslaf", "Barcelona vs RealM.mov"));
        hmap.put("3sdfgsd1j333", new Video("3sdfgsd1j333", "Programing lesson#1.avi"));

        System.out.print("Done!" + "\n");
        return hmap;
    }

    private Video getSomeVideo(String videoId) {
        System.out.print("Downloading video... ");

        experienceNetworkLatency();
        Video video = new Video(videoId, "Some video title");

        System.out.print("Done!" + "\n");
        return video;
    }
}

  some_cool_media_library/Video.java: 視頻文件

package proxy.some_cool_media_library;

/**
 * @author GaoMing
 * @date 2021/7/20 - 19:22
 */
public class Video {
    public String id;
    public String title;
    public String data;

    Video(String id, String title) {
        this.id = id;
        this.title = title;
        this.data = "Random video.";
    }
}

  proxy/YouTubeCacheProxy.java: 緩存代理

package proxy.proxy;

import proxy.some_cool_media_library.ThirdPartyYouTubeClass;
import proxy.some_cool_media_library.ThirdPartyYouTubeLib;
import proxy.some_cool_media_library.Video;

import java.util.HashMap;

/**
 * @author GaoMing
 * @date 2021/7/20 - 19:23
 */
public class YouTubeCacheProxy implements ThirdPartyYouTubeLib{
    private ThirdPartyYouTubeLib youtubeService;
    private HashMap<String, Video> cachePopular = new HashMap<String, Video>();
    private HashMap<String, Video> cacheAll = new HashMap<String, Video>();

    public YouTubeCacheProxy() {
        this.youtubeService = new ThirdPartyYouTubeClass();
    }

    @Override
    public HashMap<String, Video> popularVideos() {
        if (cachePopular.isEmpty()) {
            cachePopular = youtubeService.popularVideos();
        } else {
            System.out.println("Retrieved list from cache.");
        }
        return cachePopular;
    }

    @Override
    public Video getVideo(String videoId) {
        Video video = cacheAll.get(videoId);
        if (video == null) {
            video = youtubeService.getVideo(videoId);
            cacheAll.put(videoId, video);
        } else {
            System.out.println("Retrieved video '" + videoId + "' from cache.");
        }
        return video;
    }

    public void reset() {
        cachePopular.clear();
        cacheAll.clear();
    }
}

  downloader/YouTubeDownloader.java: 媒體下載應用

package proxy.downloader;

import proxy.some_cool_media_library.ThirdPartyYouTubeLib;
import proxy.some_cool_media_library.Video;

import java.util.HashMap;

/**
 * @author GaoMing
 * @date 2021/7/20 - 19:23
 */
public class YouTubeDownloader {
    private ThirdPartyYouTubeLib api;

    public YouTubeDownloader(ThirdPartyYouTubeLib api) {
        this.api = api;
    }

    public void renderVideoPage(String videoId) {
        Video video = api.getVideo(videoId);
        System.out.println("\n-------------------------------");
        System.out.println("Video page (imagine fancy HTML)");
        System.out.println("ID: " + video.id);
        System.out.println("Title: " + video.title);
        System.out.println("Video: " + video.data);
        System.out.println("-------------------------------\n");
    }

    public void renderPopularVideos() {
        HashMap<String, Video> list = api.popularVideos();
        System.out.println("\n-------------------------------");
        System.out.println("Most popular videos on YouTube (imagine fancy HTML)");
        for (Video video : list.values()) {
            System.out.println("ID: " + video.id + " / Title: " + video.title);
        }
        System.out.println("-------------------------------\n");
    }
}

  Demo.java: 初始化代碼

package proxy;

import proxy.downloader.YouTubeDownloader;
import proxy.proxy.YouTubeCacheProxy;
import proxy.some_cool_media_library.ThirdPartyYouTubeClass;

/**
 * @author GaoMing
 * @date 2021/7/20 - 19:21
 */
public class Demo {
    public static void main(String[] args) {
        YouTubeDownloader naiveDownloader = new YouTubeDownloader(new ThirdPartyYouTubeClass());
        YouTubeDownloader smartDownloader = new YouTubeDownloader(new YouTubeCacheProxy());

        long naive = test(naiveDownloader);
        long smart = test(smartDownloader);
        System.out.print("Time saved by caching proxy: " + (naive - smart) + "ms");

    }

    private static long test(YouTubeDownloader downloader) {
        long startTime = System.currentTimeMillis();

        // User behavior in our app:
        downloader.renderPopularVideos();
        downloader.renderVideoPage("catzzzzzzzzz");
        downloader.renderPopularVideos();
        downloader.renderVideoPage("dancesvideoo");
        // Users might visit the same page quite often.
        downloader.renderVideoPage("catzzzzzzzzz");
        downloader.renderVideoPage("someothervid");

        long estimatedTime = System.currentTimeMillis() - startTime;
        System.out.print("Time elapsed: " + estimatedTime + "ms\n");
        return estimatedTime;
    }
}

  執行結果

Connecting to http://www.youtube.com... Connected!
Downloading populars... Done!

-------------------------------
Most popular videos on YouTube (imagine fancy HTML)
ID: sadgahasgdas / Title: Catzzzz.avi
ID: asdfas3ffasd / Title: Dancing video.mpq
ID: 3sdfgsd1j333 / Title: Programing lesson#1.avi
ID: mkafksangasj / Title: Dog play with ball.mp4
ID: dlsdk5jfslaf / Title: Barcelona vs RealM.mov
-------------------------------

Connecting to http://www.youtube.com/catzzzzzzzzz... Connected!
Downloading video... Done!

-------------------------------
Video page (imagine fancy HTML)
ID: catzzzzzzzzz
Title: Some video title
Video: Random video.
-------------------------------

Connecting to http://www.youtube.com... Connected!
Downloading populars... Done!

-------------------------------
Most popular videos on YouTube (imagine fancy HTML)
ID: sadgahasgdas / Title: Catzzzz.avi
ID: asdfas3ffasd / Title: Dancing video.mpq
ID: 3sdfgsd1j333 / Title: Programing lesson#1.avi
ID: mkafksangasj / Title: Dog play with ball.mp4
ID: dlsdk5jfslaf / Title: Barcelona vs RealM.mov
-------------------------------

Connecting to http://www.youtube.com/dancesvideoo... Connected!
Downloading video... Done!

-------------------------------
Video page (imagine fancy HTML)
ID: dancesvideoo
Title: Some video title
Video: Random video.
-------------------------------

Connecting to http://www.youtube.com/catzzzzzzzzz... Connected!
Downloading video... Done!

-------------------------------
Video page (imagine fancy HTML)
ID: catzzzzzzzzz
Title: Some video title
Video: Random video.
-------------------------------

Connecting to http://www.youtube.com/someothervid... Connected!
Downloading video... Done!

-------------------------------
Video page (imagine fancy HTML)
ID: someothervid
Title: Some video title
Video: Random video.
-------------------------------

Time elapsed: 9354ms
Connecting to http://www.youtube.com... Connected!
Downloading populars... Done!

-------------------------------
Most popular videos on YouTube (imagine fancy HTML)
ID: sadgahasgdas / Title: Catzzzz.avi
ID: asdfas3ffasd / Title: Dancing video.mpq
ID: 3sdfgsd1j333 / Title: Programing lesson#1.avi
ID: mkafksangasj / Title: Dog play with ball.mp4
ID: dlsdk5jfslaf / Title: Barcelona vs RealM.mov
-------------------------------

Connecting to http://www.youtube.com/catzzzzzzzzz... Connected!
Downloading video... Done!

-------------------------------
Video page (imagine fancy HTML)
ID: catzzzzzzzzz
Title: Some video title
Video: Random video.
-------------------------------

Retrieved list from cache.

-------------------------------
Most popular videos on YouTube (imagine fancy HTML)
ID: sadgahasgdas / Title: Catzzzz.avi
ID: asdfas3ffasd / Title: Dancing video.mpq
ID: 3sdfgsd1j333 / Title: Programing lesson#1.avi
ID: mkafksangasj / Title: Dog play with ball.mp4
ID: dlsdk5jfslaf / Title: Barcelona vs RealM.mov
-------------------------------

Connecting to http://www.youtube.com/dancesvideoo... Connected!
Downloading video... Done!

-------------------------------
Video page (imagine fancy HTML)
ID: dancesvideoo
Title: Some video title
Video: Random video.
-------------------------------

Retrieved video 'catzzzzzzzzz' from cache.

-------------------------------
Video page (imagine fancy HTML)
ID: catzzzzzzzzz
Title: Some video title
Video: Random video.
-------------------------------

Connecting to http://www.youtube.com/someothervid... Connected!
Downloading video... Done!

-------------------------------
Video page (imagine fancy HTML)
ID: someothervid
Title: Some video title
Video: Random video.
-------------------------------

Time elapsed: 5875ms
Time saved by caching proxy: 3479ms
View Code

  7. 與其餘模式的關係

  • 適配器模式能爲被封裝對象提供不一樣的接口,代理模式能爲對象提供相同的接口,裝飾模式則能爲對象提供增強的接口
  • 外觀模式與代理的類似之處在於它們都緩存了一個複雜實體並自行對其進行初始化。可是,代理與其服務對象遵循同一接口,其與服務對象能夠互換
  • 裝飾和代理有着類似的結構,可是其意圖卻很是不一樣。這兩個模式的構建都基於組合原則,也就是說一個對象應該將部分工做委派給另外一個對象。二者之間的不一樣之處在於代理一般自行管理其服務對象的生命週期,而裝飾的生成則老是由客戶端進行控制

  8. 已知應用

  java.lang.reflect.Proxy

  識別方法:代理模式會將全部實際

  工做委派給一些其餘對象。除非代理是某個服務的子類,不然每一個代理方法最後都應該引用一個服務對象

相關文章
相關標籤/搜索