类间通信-门面模式

来源:互联网 发布:狼图腾 知乎 编辑:程序博客网 时间:2024/05/17 22:38

例:如图的类图和内存图
这里写图片描述

这里写图片描述

#include<iostream>using namespace std;class Base{protected:Base *next;public:    Base()    {        next=NULL;        cout<<"Base create"<<endl;    }    Base(Base *t)    {        next=t;        cout<<"Base create"<<endl;    }    virtual void fun()=0;    virtual ~Base()    {        cout<<"Base des"<<endl;    }};class A:public Base{public:    A()    {        cout<<"A Create"<<endl;        next=NULL;    }    A(Base *t)    {        cout<<"A Create"<<endl;        next=t;    }    void fun()    {        cout<<"A fun()"<<endl;        if(next)            next->fun();    }    ~A()    {        cout<<"A Des"<<endl;    }};class B:public Base{public:    B()    {        cout<<"B Create"<<endl;        next=NULL;    }    B(Base *t)    {        cout<<"B Create"<<endl;        next=t;    }    void fun()    {        cout<<"B fun()"<<endl;        if(next)            next->fun();    }    ~B()    {        cout<<"B Des"<<endl;    }};class Decorator:public Base{public:    Decorator()    {        cout<<"Decorator Create"<<endl;        next=NULL;    }    Decorator(Base *t)    {        cout<<"Decorator Create"<<endl;        next=t;    }    virtual void fun()=0;    ~Decorator()    {        cout<<"Decorator Des"<<endl;    }};class D1:public Decorator{public:    D1()    {        cout<<"D1 Create"<<endl;    }    D1(Base *t)    {        next=t;    }    void fun()    {        cout<<"D1 fun()"<<endl;        if(next)            next->fun();    }    ~D1()    {        cout<<"D1 des"<<endl;    }};class D2:public Decorator{public:    D2()    {        cout<<"D2 Create"<<endl;    }    D2(Base *t)    {        next=t;    }    void fun()    {        cout<<"D2 fun()"<<endl;        if(next)            next->fun();    }    ~D2()    {        cout<<"D2 des"<<endl;    }};class D3:public Decorator{public:    D3()    {        cout<<"D3 Create"<<endl;    }    D3(Base *t)    {        next=t;    }    void fun()    {        cout<<"D3 fun()"<<endl;        if(next)            next->fun();    }    ~D3()    {        cout<<"D3 des"<<endl;    }};class My{private:    Base * pBase;public:    My()    {        cout<<"My Create"<<endl;    }    My(Base *t)    {        pBase=t;    }    void dofun()    {        pBase->fun();    }    ~My()    {        cout<<"My des"<<endl;    }};int main(void){    D3 *pD3=new D3();    D1 *pD1=new D1(pD3);    A *pA=new A(pD1);    My *pMy=new My(pA);    pMy->dofun();    delete pMy;    delete pA;    delete pD1;    delete pD3;    return 0;}

例:将next,由protected改为private

#include<iostream>using namespace std;class Base{private:Base *next;public:    Base()    {        next=NULL;        cout<<"Base create"<<endl;    }    Base(Base *t)    {        next=t;        cout<<"Base create"<<endl;    }    virtual void fun()=0;    void WinNext()    {        if(next)            next->fun();    }    virtual ~Base()    {        cout<<"Base des"<<endl;    }};class A:public Base{public:    A()    {        cout<<"A Create"<<endl;    }    A(Base *t):Base(t)    {        cout<<"A Create"<<endl;    }    void fun()    {        cout<<"A fun()"<<endl;        WinNext();    }    ~A()    {        cout<<"A Des"<<endl;    }};class B:public Base{public:    B()    {        cout<<"B Create"<<endl;    }    B(Base *t):Base(t)    {        cout<<"B Create"<<endl;    }    void fun()    {        cout<<"B fun()"<<endl;        WinNext();    }    ~B()    {        cout<<"B Des"<<endl;    }};class Decorator:public Base{public:    Decorator()    {        cout<<"Decorator Create"<<endl;    }    Decorator(Base *t):Base(t)    {        cout<<"Decorator Create"<<endl;    }    virtual void fun()=0;    ~Decorator()    {        cout<<"Decorator Des"<<endl;    }};class D1:public Decorator{public:    D1()    {        cout<<"D1 Create"<<endl;    }    D1(Base *t):Decorator(t)    {    }    void fun()    {        cout<<"D1 fun()"<<endl;        WinNext();    }    ~D1()    {        cout<<"D1 des"<<endl;    }};class D2:public Decorator{public:    D2()    {        cout<<"D2 Create"<<endl;    }    D2(Base *t):Decorator(t)    {    }    void fun()    {        cout<<"D2 fun()"<<endl;        WinNext();    }    ~D2()    {        cout<<"D2 des"<<endl;    }};class D3:public Decorator{public:    D3()    {        cout<<"D3 Create"<<endl;    }    D3(Base *t):Decorator(t)    {    }    void fun()    {        cout<<"D3 fun()"<<endl;        WinNext();    }    ~D3()    {        cout<<"D3 des"<<endl;    }};class My{private:    Base * pBase;public:    My()    {        cout<<"My Create"<<endl;    }    My(Base *t)    {        pBase=t;    }    void dofun()    {        pBase->fun();    }    ~My()    {        cout<<"My des"<<endl;    }};int main(void){    D3 *pD3=new D3();    D1 *pD1=new D1(pD3);    A *pA=new A(pD1);    My *pMy=new My(pA);    pMy->dofun();    delete pMy;    delete pA;    delete pD1;    delete pD3;    return 0;}
原创粉丝点击