一、何為代理
顧名思意,所謂代理模式就是通過增加一個中介層(代理類)來操控我們實際要操控的另一個對象,就像一個歌星或專業運動員的經紀人一樣,被操控的對象或者是因為很複雜,或者是因為需要較長的時間才能進行構造,也或者是因為分布在網路的其它位置,這些都需要我們通過代理來解決如何使用這些對象的問題。
二、 我們的例子
比如在網路異地,有一個專門的伺服器用於提供專業的天氣預報的相關資訊,我們在網路的其它地方需要擷取我們感興趣的城市的天氣預報,為此,我們需要在用戶端本地建立一個代理類,由它來與異地的天氣預報伺服器上的相關服務進行互動,並按照定製的介面對本地開放,我們通過這些介面所開放的服務來擷取我們想要的相關資訊。下面就是我們的實現。
1、定義介面IWeather.它的作用:當我們在代理類WeatherProxy中調用了原實作類別Weather的方法時,Weather類並不一定實現了所有的方法,為了強迫Weather類實現所有的方法,同時也為了我們更加透明的去操作對象,我們在Weather類和WeatherProxy類的基礎上加上一層抽象,即它們都實現與IWeather介面
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ProxyPatternWeather
{
//定義一個介面
//Weather類和WeatherProxy類都需要實現與IMath介面
public interface IWeather
{
string GetTemperature(string city);
string GetWind(string city);
string GetAirQuality(string city);
}
}
2、定義原實作類別Weather
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ProxyPatternWeather
{
public class Weather:IWeather
{
//根據傳參,在伺服器資料庫中擷取相應資料並返回結果
//在Weather類中需要實現IWeather介面
public string GetTemperature(string city)
{
//資料庫操作
return city + "的溫度為23攝氏度";
}
public string GetWind(string city)
{
//資料庫操作
return city + "風向東南二級";
}
public string GetAirQuality(string city)
{
//資料庫操作
return city + "空氣品質優";
}
}
}
3、定義代理類WeatherProxy
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ProxyPatternWeather
{
public class WeatherProxy:IWeather
{
Weather localweather;
public WeatherProxy()
{
//通過複雜的網路操作從遠程得到相應資料
localweather = new Weather();
}
//在WeatherProxy代理類中也需要實現IWeather介面
public string GetTemperature(string city)
{
return localweather.GetTemperature(city);
}
public string GetWind(string city)
{
return localweather.GetWind(city);
}
public string GetAirQuality(string city)
{
return localweather.GetWind(city);
}
}
}
4、使用代理類擷取相關資訊
Code
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace ProxyPatternWeather
{
class Program
{
static void Main(string[] args)
{
WeatherProxy wp = new WeatherProxy();
string city = "BeiJing";
Console.WriteLine("通過WeatherProxy代理得到北京當天的天氣情況:");
Console.WriteLine(wp.GetAirQuality(city));
Console.WriteLine( wp.GetTemperature(city));
Console.WriteLine(wp.GetWind(city));
Console.ReadLine();
}
}
}
運行結果:
總結:
1、代理的分類:按照使用目的來劃
遠程(Remote)代理:為一個位於不同的地址空間的對象提供一個局域代表對象。這個不同的地址空間可以是在本機器中,也可是在另一台機器中。遠程代理又叫做大使(Ambassador)。
虛擬(Virtual)代理:根據需要建立一個資源消耗較大的對象,使得此對象只在需要時才會被真正建立。
Copy-on-Write代理:虛擬代理的一種。把複製(複製)拖延到只有在用戶端需要時,才真正採取行動。
保護(Protect or Access)代理:控制對一個對象的訪問,如果需要,可以給不同的使用者提供不同層級的使用許可權。
Cache代理:為某一個目標操作的結果提供臨時的儲存空間,以便多個用戶端可以共用這些結果。
防火牆(Firewall)代理:保護目標,不讓惡意使用者接近。
同步化(Synchronization)代理:使幾個使用者能夠同時使用一個對象而沒有衝突。
智能引用(Smart Reference)代理:當一個對象被引用時,提供一些額外的操作,比如將對此對象調用的次數記錄下來等。
在所有種類的代理模式中,虛擬(Virtual)代理、遠程(Remote)代理、智能引用代理(Smart Reference Proxy)和保護(Protect or Access)代理是最為常見的代理模式。
2、 一些可以使用代理模式(Proxy)的情況舉例:
(1)、一個對象,比如一幅很大的映像,需要載入的時間很長。
(2)、一個需要很長時間才可以完成的計算結果,並且需要在它計算過程中顯示中間結果
(3)、一個存在於遠端電腦上的對象,需要通過網路載入這個遠程對象則需要很長時間,特別是在網路傳輸高峰期。
(4)、一個對象只有有限存取權,代理模式(Proxy)可以驗證使用者的許可權
前往:設計模式學習筆記清單