1.Decorator 模式通过组合的方式提供了一种给类增加职责(操作)的方法。

2.Decorator模式结构图

3.实现

 #ifndef _DECORATOR_H_
#define _DECORATOR_H_ class Component
{
public:
virtual ~Component();
virtual void Operation();
protected:
Component();
private:
}; class ConcreteComponent:public Component
{
public:
ConcreteComponent();
~ConcreteComponent();
void Operation();
protected:
private:
}; class Decorator:public Component
{
public:
Decorator(Component* com);
virtual ~Decorator();
void Operation();
protected:
Component* _com;
private:
}; class ConcreteDecorator:public Decorator
{
public:
ConcreteDecorator(Component* com);
~ConcreteDecorator();
void Operation();
void AddedBehavior();
protected:
private:
}; #endif

Decorator.h

 #include "Decorator.h"
#include <iostream> Component::Component()
{ }
Component::~Component()
{ }
void Component::Operation()
{ }
ConcreteComponent::ConcreteComponent()
{ }
ConcreteComponent::~ConcreteComponent()
{ }
void ConcreteComponent::Operation()
{
std::cout<<"ConcreteComponent operation..."<<std::endl;
}
Decorator::Decorator(Component* com)
{
this->_com = com;
}
Decorator::~Decorator()
{
delete _com;
}
void Decorator::Operation()
{ }
ConcreteDecorator::ConcreteDecorator(Component*com):Decorator(com)
{ }
ConcreteDecorator::~ConcreteDecorator()
{ }
void ConcreteDecorator::AddedBehavior()
{
std::cout<<"ConcreteDecorator::AddedBehacior...."<<std::endl;
}
void ConcreteDecorator::Operation()
{
_com->Operation();
this->AddedBehavior();
}

Decorator.cpp

 //main.cpp
#include "Decorator.h"
#include <iostream>
using namespace std; int main(int argc,char* argv[])
{
Component* com = new ConcreteComponent();
Decorator* dec = new ConcreteDecorator(com);
dec->Operation();
delete dec;
return ;
}

main.cpp

4.Decorator模式的讨论

  为了多态,通过父类指针指向其具体子类,但是这就带来另外一个问题,当具体子类要添加新的职责,就必须向其父类添加一个这个职责的抽象接口,否则是通过父类指针是调用不到这个方法了。这样处于高层的父类就承载了太多的特征(方法),并且继承自这个父类的所有子类都不可避免继承了父类的这些接口,但是可能这并不是这个具体子类所需要的。而在Decorator模式提供了一种较好的解决方法,当需要添加一个操作的时候就可以通过Decorator模式来解决,你可以一步步添加新的职责。

Decorator Pattern的更多相关文章

  1. 设计模式(三):“花瓶+鲜花”中的装饰者模式(Decorator Pattern)

    在前两篇博客中详细的介绍了"策略模式"和“观察者模式”,今天我们就通过花瓶与鲜花的例子来类比一下“装饰模式”(Decorator Pattern).在“装饰模式”中很好的提现了开放 ...

  2. 装饰模式(Decorator pattern)

    装饰模式(Decorator pattern): 又名包装模式(Wrapper pattern), 它以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案. 装饰模式以对客户透明的方式动态的给 ...

  3. 浅谈设计模式--装饰者模式(Decorator Pattern)

    挖了设计模式这个坑,得继续填上.继续设计模式之路.这次讨论的模式,是 装饰者模式(Decorator Pattern) 装饰者模式,有时也叫包装者(Wrapper),主要用于静态或动态地为一个特定的对 ...

  4. 深入浅出设计模式——装饰模式(Decorator Pattern)

    模式动机 一般有两种方式可以实现给一个类或对象增加行为: 继承机制,使用继承机制是给现有类添加功能的一种有效途径,通过继承一个现有类可以使得子类在拥有自身方法的同时还拥有父类的方法.但是这种方法是静 ...

  5. 二十四种设计模式:装饰模式(Decorator Pattern)

    装饰模式(Decorator Pattern) 介绍动态地给一个对象添加一些额外的职责.就扩展功能而言,它比生成子类方式更为灵活.示例有一个Message实体类,某个对象对它的操作有Insert()和 ...

  6. C#设计模式之装饰者模式(Decorator Pattern)

    1.概述 装饰者模式,英文名叫做Decorator Pattern.装饰模式是在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能.它是通过创建一个包装对象,也就是装饰来包裹真实的对象. 2 ...

  7. 第 13 章 装饰模式【Decorator Pattern】

    以下内容出自:<<24种设计模式介绍与6大设计原则>> Ladies and gentlemen,May I get your attention,Please?,Now I’ ...

  8. .NET设计模式(10):装饰模式(Decorator Pattern)

      .NET设计模式(10):装饰模式(Decorator Pattern)   装饰模式(Decorator Pattern) --.NET设计模式系列之十 年月..在....对于..由于使用装饰模 ...

  9. 设计模式 - 装饰者模式(Decorator Pattern) Java的IO类 用法

    装饰者模式(Decorator Pattern) Java的IO类 用法 本文地址: http://blog.csdn.net/caroline_wendy/article/details/26716 ...

  10. 乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)

    原文:乐在其中设计模式(C#) - 装饰模式(Decorator Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 装饰模式(Decorator Pattern) 作者:weba ...

随机推荐

  1. Laravel 基础知识

    使用版本Laravel5.1.======================================================目录简单介绍:app目录,核心目录,应用目录.bootstra ...

  2. es6 Number.isFinite()、Number.isNaN()、Number.isInteger()、Math.trunc()、Math.sign()、Math.cbrt()、Math.fround()、Math.hypot()、Math 对数方法

    ES6在Number对象上,新提供了Number.isFinite()和Number.isNaN()两个方法,用来检查Infinite和NaN这两个特殊值. Number.isFinite()用来检查 ...

  3. 使用KNN对iris数据集进行分类——python

    filename='g:\data\iris.csv' lines=fr.readlines()Mat=zeros((len(lines),4))irisLabels=[]index=0for lin ...

  4. eclipse代码不能自动提示的问题解决

    步骤如下:window->Preferences->Java->Editor->content assist 替换输入成: 普通:[.abcdefghijklmnopqrstu ...

  5. Go语言:变参函数

    变参函数: 函数中形式参数的数目通常是确定的,在调用的时候要依次传入与形式参数对应的所有实际参数,但是在某些函数的参数个数可以根据实际需要来确定,这就是变参函数. Go语言支持不定长变参,但是要注意不 ...

  6. JavaScript : 零基础打造自己的jquery类库

    写作不易,转载请注明出处,谢谢. 文章类别:Javascript基础(面向初学者) 前言 在之前的章节中,我们已经不依赖jQuery,单纯地用JavaScript封装了很多方法,这个时候,你一定会想, ...

  7. Android ANR原理分析

    一.概述 ANR(Application Not responding),是指应用程序未响应,Android系统对于一些事件需要在一定的时间范围内完成,如果超过预定时间能未能得到有效响应或者响应时间过 ...

  8. Java反射之Field用法

    在Java反射中Field用于获取某个类的属性或该属性的属性值 一:如何通过Field反射获取类的属性 Field提供如下几种方法: :1:Class.getDeclaredField(String ...

  9. 如何下载合适自己系统环境的Xdebug

      访问https://xdebug.org     在浏览器输入http://localhost/?phpinfo=1 (前提已经安装了wamp环境) Ctrl+a 全选 ,复制 粘贴到刚才的网站的 ...

  10. FTPClient listFiles 阻塞问题

    Android端使用 FTPClient 实现上传文件到到filezilla server(filezilla server部署在阿里云服务器)出现 listFiles阻塞.具体的现象是 Ftp Cl ...