c++设计模式代理模式 -电脑资料

电脑资料 时间:2019-01-01 我要投稿
【www.unjs.com - 电脑资料】

    #include

    using namespace std;

    //代理模式中注意代理虽然可以实现某个功能,但是代理并不具备该功能,它的实现是通过调用别人的功能

    //至于具体哪个功能,多态中基类指针指派生类对象,指向哪个派生类,就去调用哪个派生类的fun函数

    class Base

    {

    public :virtual void fun()=0;

    virtual ~Base(){}

    };

    class Derived:public Base

    {

    public :void fun()

    {cout<<"Derived fun"<

    };

    class proxy:public Base

    {

    private:Base *pBase;//注意,这里如果写Derived类的对象或指针,当然可以实现,

    //但是如果该代理类又要实现别的功能,就又要加该类的对象或指针,

c++设计模式代理模式

电脑资料

c++设计模式代理模式》(https://www.unjs.com)。

    //违背c++设计模式中开闭原则(开放接口,关闭修改)

    public:proxy(Base *t){pBase=t;}

    void fun()

    {

    pBase->fun();

    }

    };

    int main (void)

    {

    Derived *pDerived=new Derived();

    proxy *p=new proxy(pDerived);

    p->fun();

    delete pDerived;

    delete p;

    }

   

最新文章