装飾モード(C++)

4158 ワード

#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;
}