设计模式 之 《装饰模式》

 

#ifndef __DECORATOR__
#define __DECORATOR__

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

//
class Person
{
public:
    Person(string strName)
    {
        m_strName = strName;
    }

    Person(){}

    virtual void show()
    {
        cout<<"装扮的是:"<<m_strName<<endl;
    }

private:
    string m_strName;

};

//装饰类
class Finery : public Person
{
protected:
    Person* m_component;
public:
    void Decorator(Person* component)
    {
        m_component = component;
    }
    virtual void show()
    {
        m_component->show();
    }
};

class TShirts : public Finery
{
public:
    virtual void show()
    {
        cout<<"T Shirts"<<endl;
        m_component->show();
    }

};

class BigTrouser : public Finery
{
public:
    virtual void show()
    {
        cout<<"Big Trouser"<<endl;
        m_component->show();
    }
};


#endif //__DECORATOR__



int _tmain(int argc, _TCHAR* argv[])
{
    Person* person = new Person("张三");
    
    BigTrouser* bt = new BigTrouser();
    TShirts* ts = new TShirts();
    bt->Decorator(person);
    ts->Decorator(bt);
    ts->show();

    return 0;
}

 

 

 

posted @ 2013-10-22 23:09  解放1949  阅读(148)  评论(0编辑  收藏  举报