意图:
动态的给一个对象添加一些额外的职责。比生成子类更为灵活
UML结构图:

适用:
在不影响其他对象的情况下,以动态,透明的方式给单个对象添加职责
处理那些不可撤消的职责
当不能采用生成子类的方式进行扩充时
//test.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(); //动态添加的职责
};
// test.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include "test.h"

//////////////////////////////////////////////////////////////////////////
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";
}
//////////////////////////////////////////////////////////////////////////
int main(int argc, char* argv[])
{
Component* Pcomponent = new ConCreateComponent;
//用这个对象去初始化一个Decorator对象
//通过多态调用动态添加了职责
Decorator* pDecorator = new ConCreateDecorator(Pcomponent);
pDecorator->Operation();
delete pDecorator;

system("pause");
return 0;
}
动态的给一个对象添加一些额外的职责。比生成子类更为灵活
UML结构图:

适用:
在不影响其他对象的情况下,以动态,透明的方式给单个对象添加职责
处理那些不可撤消的职责
当不能采用生成子类的方式进行扩充时
//test.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(); //动态添加的职责
};
// test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include "test.h"
//////////////////////////////////////////////////////////////////////////
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";
}
//////////////////////////////////////////////////////////////////////////
int main(int argc, char* argv[])
{
Component* Pcomponent = new ConCreateComponent;
//用这个对象去初始化一个Decorator对象
//通过多态调用动态添加了职责
Decorator* pDecorator = new ConCreateDecorator(Pcomponent);
pDecorator->Operation();
delete pDecorator;
system("pause");
return 0;
}
本文介绍了一种用于在不改变原有对象的基础上动态添加职责的设计模式——装饰者模式,并提供了详细的实现代码示例。装饰者模式允许在运行时为对象增加新的功能,而无需通过继承来实现。
524

被折叠的 条评论
为什么被折叠?



