首页 > 其他 > 详细

Decorator

时间:2014-12-11 12:15:13      阅读:252      评论:0      收藏:0      [点我收藏+]
#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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!