作用:
动态地给一个对象添加一些额外的职责。就增加功能来说,Decorator 模式相比生成子类更为灵活。
UML结构图:
![](http://teddyma.cnblogs.com/images/cnblogs_com/teddyma/gof24.JPG)
抽象基类:
1)Component:定义一个对象接口,可以为这个接口动态的添加职责.
2)Decorator:维持一个指向Component的指针,并且有一个和Component一致的接口函数.
接口函数:
1)Component::Operation:这个接口函数由Component声明,因此Component的派生类都需要实现,可以在这个接口函数的基础上给它动态添加职责.
解析:
Decorator的派生类可以为ConcreateComponent类的对象动态的添加职责,或者可以这么说:Decorator的派生类装饰ConcreateComponent类的对象.具体是这么实现的,首先初始化一个ConcreateComponent类的对象(被装饰者),采用这个对象去生成一个Decorator对象(装饰者),之后对Operation函数的调用则是对这个Decorator对象成员函数的多态调用.这里的实现要点是Decorator类和ConcreateComponent类都继承自Component,从而两者的接口函数是一致的;其次,Decorator维护了一个指向Component的指针,从而可以实现对Component::Operation函数的动态调用.
实现:
1)Decorator.h
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//********************************************************************
created: 2006/07/20
filename: Decorator.h
author: 李创
http://www.cppblog.com/converse/
![](/Images/OutliningIndicators/InBlock.gif)
purpose: Decorator模式的演示代码
*********************************************************************/
![](/Images/OutliningIndicators/None.gif)
#ifndef DECORATOR_H
#define DECORATOR_H
![](/Images/OutliningIndicators/None.gif)
// 抽象基类,定义一个对象接口,可以为这个接口动态的添加职责.
class Component
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
public:
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Component()
{}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
virtual ~Component()
{}
![](/Images/OutliningIndicators/InBlock.gif)
// 纯虚函数,由派生类实现
virtual void Operation() = 0;
};
![](/Images/OutliningIndicators/None.gif)
// 抽象基类,维护一个指向Component对象的指针
class Decorator
: public Component
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
public:
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Decorator(Component* pComponent) : m_pComponent(pComponent)
{}
virtual ~Decorator();
![](/Images/OutliningIndicators/InBlock.gif)
protected:
Component* m_pComponent;
};
![](/Images/OutliningIndicators/None.gif)
// 派生自Component,在这里表示需要给它动态添加职责的类
class ConcreateComponent
: public Component
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
public:
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
ConcreateComponent()
{}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
virtual ~ConcreateComponent()
{}
![](/Images/OutliningIndicators/InBlock.gif)
virtual void Operation();
};
![](/Images/OutliningIndicators/None.gif)
// 派生自Decorator,这里代表为ConcreateComponent动态添加职责的类
class ConcreateDecorator
: public Decorator
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
public:
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
ConcreateDecorator(Component* pComponent) : Decorator(pComponent)
{}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
virtual ~ConcreateDecorator()
{}
![](/Images/OutliningIndicators/InBlock.gif)
virtual void Operation();
![](/Images/OutliningIndicators/InBlock.gif)
private:
void AddedBehavior();
};
![](/Images/OutliningIndicators/None.gif)
#endif2)Decorator.cpp
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//********************************************************************
created: 2006/07/20
filename: Decorator.cpp
author: 李创
http://www.cppblog.com/converse/
![](/Images/OutliningIndicators/InBlock.gif)
purpose: Decorator模式的演示代码
*********************************************************************/
![](/Images/OutliningIndicators/None.gif)
#include "Decorator.h"
#include <iostream>
![](/Images/OutliningIndicators/None.gif)
Decorator::~Decorator()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
delete m_pComponent;
m_pComponent = NULL;
}
![](/Images/OutliningIndicators/None.gif)
void ConcreateComponent::Operation()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
std::cout << "Operation of ConcreateComponent\n";
}
![](/Images/OutliningIndicators/None.gif)
void ConcreateDecorator::Operation()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
m_pComponent->Operation();
AddedBehavior();
}
![](/Images/OutliningIndicators/None.gif)
void ConcreateDecorator::AddedBehavior()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
std::cout << "AddedBehavior of ConcreateDecorator\n";
}3)Main.cpp
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//********************************************************************
created: 2006/07/20
filename: Main.cpp
author: 李创
http://www.cppblog.com/converse/
![](/Images/OutliningIndicators/InBlock.gif)
purpose: Decorator模式的测试代码
*********************************************************************/
![](/Images/OutliningIndicators/None.gif)
#include "Decorator.h"
#include <stdlib.h>
![](/Images/OutliningIndicators/None.gif)
int main()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
// 初始化一个Component对象
Component* pComponent = new ConcreateComponent();
// 采用这个Component对象去初始化一个Decorator对象,
// 这样就可以为这个Component对象动态添加职责
Decorator* pDecorator = new ConcreateDecorator(pComponent);
![](/Images/OutliningIndicators/InBlock.gif)
pDecorator->Operation();
![](/Images/OutliningIndicators/InBlock.gif)
delete pDecorator;
![](/Images/OutliningIndicators/InBlock.gif)
system("pause");
![](/Images/OutliningIndicators/InBlock.gif)
return 0;
}
![](/Images/OutliningIndicators/None.gif)