[结构型模式] 装饰者模式的理解

[img]http://api5.yunpan.360.cn/intf.php?method=File.getThumbByNid&qid=108635719&nid=13770720602900135&size=800_600&devtype=web&v=1.0.1&rtick=14661274237039&sign=67fd31b391787cd8d039cb8df7c378ab&[/img]
[img]http://api5.yunpan.360.cn/intf.php?method=File.getThumbByNid&qid=108635719&nid=14661242769476707&size=800_600&devtype=web&v=1.0.1&rtick=14661277273148&sign=82fb4f9fbc776935a9e74224768128a8&[/img]
[img]http://api5.yunpan.360.cn/intf.php?method=File.getThumbByNid&qid=108635719&nid=14661242769480056&size=800_600&devtype=web&v=1.0.1&rtick=14661277276112&sign=58d1112ade6fe6c5e786f4ef051e5eb0&[/img]
[color=red][b]头文件[/b][/color]

//DecoratorPattern.h

#ifndef DECORATOR_PATTERN_H
#define DECORATOR_PATTERN_H

#include <Windows.h>
#include <iostream>
using namespace std;

namespace DecoratorPattern
{
/*
** FileName : DecoratorPatternDemo
** Author : Jelly Young
** Date : 2013/12/19
** Description : More information, please go to http://www.jellythink.com
*/

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

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

//
//
class Decorator : public Component
{
public:
Decorator(Component* pComponent);
virtual ~Decorator();
virtual void Operation();

protected:
Component* m_pComponentObj;
};

class ConcreteDecoratorA : public Decorator
{
public:
ConcreteDecoratorA(Component* pDecorator) ;
virtual ~ConcreteDecoratorA();

virtual void Operation();

void AddedBehavior();
};

class ConcreteDecoratorB : public Decorator
{
public:
ConcreteDecoratorB(Component* pDecorator);
virtual ~ConcreteDecoratorB();

virtual void Operation();
void AddedBehavior();
};

//
void DecoratorPattern_Test();
}

#endif


[color=red][b]实现[/b][/color]

#include "DecoratorPattern.h"

namespace DecoratorPattern
{
Component::Component()
{
}

Component::~Component()
{
}

//
//
ConcreteComponent::ConcreteComponent()
{
}

ConcreteComponent::~ConcreteComponent()
{
}

void ConcreteComponent::Operation()
{
cout<<"I am no decoratored ConcreteComponent"<<endl;
}


//
//
Decorator::Decorator(Component *pComponent)
: m_pComponentObj(pComponent)
{}

Decorator::~Decorator()
{}

void Decorator::Operation()
{
if (m_pComponentObj != NULL)
{
m_pComponentObj->Operation();
}
}

//
ConcreteDecoratorA::ConcreteDecoratorA(Component* pDecorator)
: Decorator(pDecorator)
{}

ConcreteDecoratorA::~ConcreteDecoratorA()
{}

void ConcreteDecoratorA::Operation()
{
AddedBehavior();
Decorator::Operation();
}
void ConcreteDecoratorA::AddedBehavior()
{
cout<<"This is added behavior A."<<endl;
}

ConcreteDecoratorB::ConcreteDecoratorB(Component *pDecorator)
: Decorator(pDecorator)
{}
ConcreteDecoratorB::~ConcreteDecoratorB()
{}
void ConcreteDecoratorB::Operation()
{
AddedBehavior();
Decorator::Operation();
}
void ConcreteDecoratorB::AddedBehavior()
{
cout<<"This is added behavior B."<<endl;
}


//
void DecoratorPattern_Test()
{
Component *pComponentObj = new ConcreteComponent();
Decorator *pDecoratorAOjb = new ConcreteDecoratorA(pComponentObj);
pDecoratorAOjb->Operation();
cout<<"============================================="<<endl;
Decorator *pDecoratorBOjb = new ConcreteDecoratorB(pComponentObj);
pDecoratorBOjb->Operation();
cout<<"============================================="<<endl;
Decorator *pDecoratorBAOjb = new ConcreteDecoratorB(pDecoratorAOjb);
pDecoratorBAOjb->Operation();
cout<<"============================================="<<endl;

delete pDecoratorBAOjb;
pDecoratorBAOjb = NULL;
delete pDecoratorBOjb;
pDecoratorBOjb = NULL;
delete pDecoratorAOjb;
pDecoratorAOjb = NULL;
delete pComponentObj;
pComponentObj = NULL;
}
}


[color=red][b]客户端[/b][/color]

#include "DecoratorPattern.h"


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

void main()
{
DecoratorPattern_Test();
}

[color=red][b]运行结果[/b][/color]
[img]http://api5.yunpan.360.cn/intf.php?method=File.getThumbByNid&qid=108635719&nid=14661278299439906&size=800_600&devtype=web&v=1.0.1&rtick=14661278295096&sign=c458cc2454ee68847aab5e136df962e9&[/img]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值