装饰模式(C++)

#include <iostream>
#include <string>
using namespace std;

class text
{
public:
    text(){}
    virtual ~text(){}
    virtual void operation(){cout<<"a text"<<endl;}
};

class Scoll :public text
{
public:
    Scoll(){}
    virtual ~Scoll(){}
    void decorator(text* comp){this->tx=comp;}
    void operation(){tx->operation();}

protected:
    text *tx;
};

class vScoll :public Scoll
{
public:
    vScoll(){}
    virtual ~vScoll(){}
    void operation(){cout<<"vscoll+";Scoll::operation();}
};

class hScoll : public Scoll
{
public:
    hScoll(){}
    virtual ~hScoll(){}
    void operation(){cout<<"hscoll+";Scoll::operation();}
};

int main()
{
    text *ptx=new text;
    Scoll *pvs=new vScoll;
    Scoll *phs=new hScoll;

    ptx->operation();

    pvs->decorator(ptx);
    pvs->operation();

    phs->decorator(pvs);
    phs->operation();

    delete phs;
    delete pvs;
    delete ptx;

    system("pause");
    return 0;
}
posted @ 2012-06-26 15:28  Dsp Tian  阅读(485)  评论(0编辑  收藏  举报