《大話設計模式》讀書筆記(C++代碼實現) 第七章:代理模式

來源:互聯網
上載者:User

代理模式(Proxy)

  為其他對象提供一種代理以控制對這個對象的訪問。

 

  

// ProxyTest01.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <iostream>#include <string>using namespace std;//想要追求的姑娘class SchoolGirl{public :    string name;};//贈送禮物class IGiveGift{public :    virtual void GiveDolls() = 0;    virtual void GiveFlowers() = 0;    virtual void GiveChocolate() = 0;};//追求者,具體表現是送給喜歡的姑娘禮物class Pursuit : public IGiveGift{private :    SchoolGirl mm;public :    //Pursuit(){}    Pursuit(SchoolGirl mm)    {        this->mm = mm;    }public :    void GiveDolls()    {        cout<<mm.name<<":送你洋娃娃"<<endl;    }    void GiveFlowers()    {        cout<<mm.name<<":送你鮮花"<<endl;    }    void GiveChocolate()    {        cout<<mm.name<<":送你巧克力"<<endl;    }};//代理,幫追求者來做事,表現也是送東西給姑娘,當然是代追求者送的class Proxy : public IGiveGift{private :    Pursuit *pursuit;public :    ~Proxy(){ delete pursuit; }    Proxy(SchoolGirl mm)    {        pursuit = new Pursuit(mm);    }public :    void GiveDolls()    {        pursuit->GiveDolls();    }    void GiveFlowers()    {        pursuit->GiveFlowers();    }    void GiveChocolate()    {        pursuit->GiveChocolate();    }};int _tmain(int argc, _TCHAR* argv[]){    SchoolGirl mm;    mm.name = "美女";    //追求者親自來追女生     //Pursuit p(mm);    //p.GiveDolls();    //p.GiveFlowers();    //p.GiveChocolate();    //代理來代某個人來追女生     Proxy proxy(mm);    proxy.GiveDolls();    proxy.GiveFlowers();    proxy.GiveChocolate();    system("pause");    return 0;}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.