码迷,mamicode.com
首页 > 其他好文 > 详细

Decorator

时间:2014-12-11 12:17:14      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   io   ar   color   os   sp   on   

#include <iostream>

using namespace std;

#define DESTROY_POINTER(ptr) if (ptr) { delete ptr; ptr = NULL; } 



class Product
{
public:
    virtual void Action()=0;
};

class ConcreteProductA : public Product
{
public:
    ConcreteProductA() {}
    ~ConcreteProductA() {}
    
    void Action() { cout<<"ConcreteProductA::Action"<<endl; }
};

class ConcreteProductB : public Product
{
public:
    ConcreteProductB() {}
    ~ConcreteProductB() {}
    
    void Action() { cout<<"ConcreteProductB::Action"<<endl; }
};



class Decorator : public Product
{
public:
    virtual void Action()=0;
};

class DecoratorProductA : public Decorator
{
public:
    DecoratorProductA() { m_pProduct = new ConcreteProductA; }
    ~DecoratorProductA() { DESTROY_POINTER(m_pProduct); }

    void AddModification() { cout<<"DecoratorProductA::AddModification->"; }
    void Action()
    {
        AddModification();
        m_pProduct->Action();
    }
    
private:
    Product* m_pProduct;
};

class DecoratorProductB : public Decorator
{
public:
    DecoratorProductB() { m_pProduct = new ConcreteProductB; }
    ~DecoratorProductB() { DESTROY_POINTER(m_pProduct); }

    void AddModification() { cout<<"DecoratorProductB::AddModification->"; }
    void Action()
    {
        AddModification();
        m_pProduct->Action();
    }

private:
    Product* m_pProduct;
};





int main(int argc, char *argv[])
{
    Product* pProduct;

    pProduct = new ConcreteProductA;
    pProduct->Action();
    DESTROY_POINTER(pProduct);

    pProduct = new ConcreteProductB;
    pProduct->Action();
    DESTROY_POINTER(pProduct);

    Decorator* pDecorator = new DecoratorProductA;
    pDecorator->Action();
    DESTROY_POINTER(pDecorator);
    
    pDecorator = new DecoratorProductB;
    pDecorator->Action();
    DESTROY_POINTER(pDecorator);
    


    return 0;
}

 

Decorator

标签:des   style   blog   io   ar   color   os   sp   on   

原文地址:http://www.cnblogs.com/stanley198610281217/p/4157146.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!