设计模式的解析和实现(C++)之九-Decorator模式
来源:互联网 发布:transmit mac 编辑:程序博客网 时间:2024/06/05 20:39
动态地给一个对象添加一些额外的职责。就增加功能来说,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
#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();
};
#endif
2)Decorator.cpp
#include "Decorator.h"
#include <iostream>
Decorator::~Decorator()
{
delete m_pComponent;
m_pComponent = NULL;
}
void ConcreateComponent::Operation()
{
std::cout << "Operation of ConcreateComponentn";
}
void ConcreateDecorator::Operation()
{
m_pComponent->Operation();
AddedBehavior();
}
void ConcreateDecorator::AddedBehavior()
{
std::cout << "AddedBehavior of ConcreateDecoratorn";
}
#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;
}
#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;
}
- 常见设计模式的解析和实现(C++)之九-Decorator模式
- 常见设计模式的解析和实现(C++)之九-Decorator模式
- 常见设计模式的解析和实现(C++)之九-Decorator模式
- 常见设计模式的解析和实现(C++)之九-Decorator模式
- 设计模式的解析和实现(C++)之九-Decorator模式
- 常见设计模式的解析和实现(C++)之九—Decorator模式
- 设计模式之(九)装饰模式Decorator
- 设计模式解析和实现(C++)之十五-Observer模式
- 设计模式解析和实现(C++)之八-Composite模式
- 设计模式解析和实现(C++)之十五-Observer模式
- 设计模式解析和实现(C++)之十九-Memento模式
- 设计模式(九)之Decorator - 装饰模式
- 设计模式之(九)--包装模式(decorator)
- 常见设计模式的解析和实现(C++)---Adapt模式
- 设计模式的解析和实现(C++)之一-Factory模式
- 设计模式的解析和实现(C++)之一-Factory模式
- 设计模式之Decorator
- 设计模式之 Decorator
- 编译android源码前的准备
- 认识店面桌面图标的作用和意义
- test
- Android使用OpenCV和FFMpeg的简单方法-开源项目javacv的使用
- ctags
- 设计模式的解析和实现(C++)之九-Decorator模式
- (算法题)几道算法题
- 设计C++回调模型(二):线程安全
- 软件测试用例的设计心得
- 关于json对象和json字符串的转换以及操作
- Linux-2.6.25 TCPIP函数调用大致流程
- 从客户端检测到有潜在危险的Request.Form 值
- C#上机 第十周 单选按钮和复选框
- 加盟商必须知道的收银知识