Chinaunix首页 | 论坛 | 博客
  • 博客访问: 41380
  • 博文数量: 17
  • 博客积分: 1432
  • 博客等级: 上尉
  • 技术积分: 165
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-27 09:38
文章分类

全部博文(17)

文章存档

2011年(1)

2009年(16)

最近访客

分类: C/C++

2009-02-24 16:42:43

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

class Component
{
public:
    virtual void Operation(){};
};

class ConcreateComponent :public Component
{
public:
    void Operation()
    {
        cout<<"具体对象的操作"<<endl;
    }
};

class Decorator : public Component
{
protected:
    Component* m_component;

public:
    Decorator()
    {
        m_component = NULL;
    }
    void SetCompenent(Component* component)
    {
        m_component = component;
    }

    virtual void Operation()
    {
        if (m_component != NULL)
        {
            m_component->Operation();
        }
    }
};

class ConcreateDecoratorA : public Decorator
{
private:
    //Decorator m_decorator;

    string addedState;

public:
    //ConcreateDecoratorA():

    void Operation()
    {
        Decorator::Operation();
        addedState = "New state";
        cout<<"具体装饰对象A的操作"<<endl;
    }
};


class ConcreateDecoratorB : public Decorator
{
private:
    //Decorator m_decorator;

    //string addedState;


public:
    //ConcreateDecoratorA():

    void Operation()
    {
        Decorator::Operation();
        cout<<"具体装饰对象B的操作"<<endl;
    }
};

int main(void)
{
    ConcreateComponent* c = new ConcreateComponent();
    ConcreateDecoratorA *da = new ConcreateDecoratorA();
    ConcreateDecoratorA *da2 = new ConcreateDecoratorA();
    ConcreateDecoratorB *db = new ConcreateDecoratorB();

    da->SetCompenent(c);
    db->SetCompenent(da);
    da2->SetCompenent(db);

    da2->Operation();
};

阅读(637) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~