[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]
[color=red][b]实现[/b][/color]
[color=red][b]客户端[/b][/color]
[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]
[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]