作用:
动态地给一个对象添加一些额外的职责。就增加功能来说,Decorator 模式相比生成子类更为灵活。
UML结构图:

抽象基类:
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

/**//********************************************************************
created: 2006/07/20
filename: Decorator.h
author: 李创
http://www.cppblog.com/converse/

purpose: Decorator模式的演示代码
*********************************************************************/

#ifndef DECORATOR_H
#define DECORATOR_H

// 抽象基类,定义一个对象接口,可以为这个接口动态的添加职责.
class Component


{
public:

Component()
{}

virtual ~Component()
{}

// 纯虚函数,由派生类实现
virtual void Operation() = 0;
};

// 抽象基类,维护一个指向Component对象的指针
class Decorator
: public Component


{
public:

Decorator(Component* pComponent) : m_pComponent(pComponent)
{}
virtual ~Decorator();

protected:
Component* m_pComponent;
};

// 派生自Component,在这里表示需要给它动态添加职责的类
class ConcreateComponent
: public Component


{
public:

ConcreateComponent()
{}

virtual ~ConcreateComponent()
{}

virtual void Operation();
};

// 派生自Decorator,这里代表为ConcreateComponent动态添加职责的类
class ConcreateDecorator
: public Decorator


{
public:

ConcreateDecorator(Component* pComponent) : Decorator(pComponent)
{}

virtual ~ConcreateDecorator()
{}

virtual void Operation();

private:
void AddedBehavior();
};

#endif2)Decorator.cpp

/**//********************************************************************
created: 2006/07/20
filename: Decorator.cpp
author: 李创
http://www.cppblog.com/converse/

purpose: Decorator模式的演示代码
*********************************************************************/

#include "Decorator.h"
#include <iostream>

Decorator::~Decorator()


{
delete m_pComponent;
m_pComponent = NULL;
}

void ConcreateComponent::Operation()


{
std::cout << "Operation of ConcreateComponent\n";
}

void ConcreateDecorator::Operation()


{
m_pComponent->Operation();
AddedBehavior();
}

void ConcreateDecorator::AddedBehavior()


{
std::cout << "AddedBehavior of ConcreateDecorator\n";
}3)Main.cpp

/**//********************************************************************
created: 2006/07/20
filename: Main.cpp
author: 李创
http://www.cppblog.com/converse/

purpose: Decorator模式的测试代码
*********************************************************************/

#include "Decorator.h"
#include <stdlib.h>

int main()


{
// 初始化一个Component对象
Component* pComponent = new ConcreateComponent();
// 采用这个Component对象去初始化一个Decorator对象,
// 这样就可以为这个Component对象动态添加职责
Decorator* pDecorator = new ConcreateDecorator(pComponent);

pDecorator->Operation();

delete pDecorator;

system("pause");

return 0;
}
