( Design Patterns ) Structural Design Patterns 2 -- Proxy Pattern

Definition

Provide a surrogate or placeholder for another object to control access to it. This pattern can be used when we don't want to access the resource or subject directly.

Components

  • Subject: Provides an interface that both actual and proxy class will implement. In this way the proxy can easily be used as a substitute for real subject.
  • Proxy: This class will be used by the applications and will expose the methods exposed by the Subject.
  • RealSubject: Real object that contains the actual logic to retrieve the data/functionality, which represented by the proxy.
Proxy Pattern UML

Code

public interface ISubject
{
    double Add(double x, double y);
    double Sub(double x, double y);
    double Mul(double x, double y);
    double Div(double x, double y);
}

public class Math : ISubject
{
    public double Add(double x, double y) { return x + y; }
    public double Sub(double x, double y) { return x - y; }
    public double Mul(double x, double y) { return x * y; }
    public double Div(double x, double y) { return x / y; }
}

public class MathProxy : ISubject
{
    private Math _math;

    public MathProxy()
    {
        _math = new Math();
    }

    public double Add(double x, double y)
    {
        return _math.Add(x, y);
    }
    public double Sub(double x, double y)
    {
        return _math.Sub(x, y);
    }
    public double Mul(double x, double y)
    {
        return _math.Mul(x, y);
    }
    public double Div(double x, double y)
    {
        return _math.Div(x, y);
    }
}

public class ProxyPatternRunner : IPatterRunner
{
    public void RunPattern()
    {
        var proxy = new MathProxy();

        Console.WriteLine("4 + 2 = " + proxy.Add(4, 2));
        Console.WriteLine("4 - 2 = " + proxy.Sub(4, 2));
        Console.WriteLine("4 * 2 = " + proxy.Mul(4, 2));
        Console.WriteLine("4 / 2 = " + proxy.Div(4, 2));

        Console.ReadKey();
    }
}

Proxy Types

  • Remote proxies: Representing the object located remotely.

  • Virtual proxies: These proxies will provide some default behaviors if the real objects needs some time to perform these behaviors. Once the real object is done, these proxies push the actual data to the client.

  • Protection proxies: When an application does not have access to some resource then these proxies will talk to the objects.

Reference

Understanding and Implementing Proxy Pattern in C# - CodeProject

Design Patterns 2 of 3 - Structural Design Patterns - CodeProject

Head First Design Patterns - O'Reilly Media

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末慕嚷,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌闯冷,老刑警劉巖砂心,帶你破解...
    沈念sama閱讀 216,997評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異蛇耀,居然都是意外死亡辩诞,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,603評論 3 392
  • 文/潘曉璐 我一進店門纺涤,熙熙樓的掌柜王于貴愁眉苦臉地迎上來译暂,“玉大人,你說我怎么就攤上這事撩炊⊥庥溃” “怎么了?”我有些...
    開封第一講書人閱讀 163,359評論 0 353
  • 文/不壞的土叔 我叫張陵拧咳,是天一觀的道長伯顶。 經(jīng)常有香客問我,道長骆膝,這世上最難降的妖魔是什么祭衩? 我笑而不...
    開封第一講書人閱讀 58,309評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮阅签,結果婚禮上掐暮,老公的妹妹穿的比我還像新娘。我一直安慰自己政钟,他們只是感情好路克,可當我...
    茶點故事閱讀 67,346評論 6 390
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著养交,像睡著了一般精算。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上碎连,一...
    開封第一講書人閱讀 51,258評論 1 300
  • 那天灰羽,我揣著相機與錄音,去河邊找鬼破花。 笑死,一個胖子當著我的面吹牛疲吸,可吹牛的內(nèi)容都是我干的座每。 我是一名探鬼主播,決...
    沈念sama閱讀 40,122評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼摘悴,長吁一口氣:“原來是場噩夢啊……” “哼峭梳!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 38,970評論 0 275
  • 序言:老撾萬榮一對情侶失蹤葱椭,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后秦陋,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,403評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡驳概,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,596評論 3 334
  • 正文 我和宋清朗相戀三年顺又,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片稚照。...
    茶點故事閱讀 39,769評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡俯萌,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出雕憔,到底是詐尸還是另有隱情,我是刑警寧澤斤彼,帶...
    沈念sama閱讀 35,464評論 5 344
  • 正文 年R本政府宣布蘸泻,位于F島的核電站,受9級特大地震影響悦施,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜抡诞,卻給世界環(huán)境...
    茶點故事閱讀 41,075評論 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望肴熏。 院中可真熱鬧顷窒,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,705評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至坷檩,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間矢炼,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,848評論 1 269
  • 我被黑心中介騙來泰國打工夷陋, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人骗绕。 一個月前我還...
    沈念sama閱讀 47,831評論 2 370
  • 正文 我出身青樓资昧,卻偏偏與公主長得像,于是被迫代替她去往敵國和親格带。 傳聞我的和親對象是個殘疾皇子撤缴,可洞房花燭夜當晚...
    茶點故事閱讀 44,678評論 2 354

推薦閱讀更多精彩內(nèi)容