C++实现设计模式:Decorator Pattern

有时我们希望给某个对象而不是整个类添加一些功能。例如,一个图形用户界面工具箱允许你对任意一个用户界面组件添加一些特性,例如边框,或是一些行为,例如窗口滚动。(GoF)

下面请看示例程序:

#ifndef _COMPONENT_H
#define _COMPONENT_H

#include <iostream>
using std::cout;
using std::endl;

class virtualComponent
{
public:
    virtual ~virtualComponent()
    {
        std::cout<<"Destructor of virtualComponent"<<endl;
    }
    virtual void Draw()
    {
        std::cout<<"virtualComponent::Draw()"<<std::endl;
    }
private:
   
};
class TextView:public virtualComponent
{
public:
    TextView(){std::cout<<"Constructor of TextView"<<endl;}
    virtual ~TextView()
    {
        std::cout<<"Destructor of TextView"<<endl;
    }
    virtual void Draw()
    {
        std::cout<<"TextView::Draw()"<<std::endl;
    }
private:
   
};
#endif


#ifndef _DECORATOR_H
#define _DECORATOR_H
#include "Component.h"
#include <iostream>
class Decorator:public virtualComponent
{
public:
    Decorator(virtualComponent* component):_pComponent(component){}
    virtual ~Decorator()
    {
        std::cout<<"Destructor of Decorator"<<endl;
        if(_pComponent)
            _pComponent->~virtualComponent();
    }
    virtual void Draw(){std::cout<<"Decorator::Draw()"<<std::endl;}
private:
    virtualComponent* _pComponent;
};
class ScrollDecorator:public Decorator
{
public:
    ScrollDecorator(virtualComponent* component, int scrollPosition=0):
      Decorator(component),_scrollPosition(scrollPosition)
    {
        std::cout<<"Constructor of ScrollDecorator"<<endl;
    }
   
    virtual ~ScrollDecorator()
    {
        std::cout<<"Destructor of ScrollDecorator"<<endl;
    }
    virtual void Draw()
    {
        std::cout<<"ScrollDecorator::Draw()"<<endl;
        ScrollTo();
    }
    virtual void ScrollTo(){std::cout<<"ScrollDecorator::ScrollTo()"<<endl;}
private:
    int _scrollPosition;
};
class BorderDecorator:public Decorator
{
public:
    BorderDecorator(virtualComponent* component, int borderWidth =0):
      Decorator(component),_borderWidth(borderWidth)
    {
        std::cout<<"Constructor of BorderDecorator"<<endl;
    }
   
    virtual ~BorderDecorator()
    {
        std::cout<<"Destructor of BorderDecorator"<<endl;
    }
    virtual void Draw()
    {
        std::cout<<"BorderDecorator::Draw()"<<endl;
        DrawBorder();
    }
    virtual void DrawBorder(){std::cout<<"BorderDecorator::DrawBorder()"<<endl;}
private:
    int _borderWidth;
};
#endif


#include <iostream>
#include "Decorator.h"
int main()
{
    Decorator* dec1= new ScrollDecorator(new TextView());
    dec1->Draw();
    Decorator* dec2= new ScrollDecorator(new BorderDecorator(new TextView()));
    dec2->Draw();
   
    delete dec1;
    std::cout<<"--------------------------"<<endl;
    delete dec2;
    system("PAUSE");
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值