Decorator Pattern
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的更多相关文章
- 设计模式(三):“花瓶+鲜花”中的装饰者模式(Decorator Pattern)
在前两篇博客中详细的介绍了"策略模式"和“观察者模式”,今天我们就通过花瓶与鲜花的例子来类比一下“装饰模式”(Decorator Pattern).在“装饰模式”中很好的提现了开放 ...
- 装饰模式(Decorator pattern)
装饰模式(Decorator pattern): 又名包装模式(Wrapper pattern), 它以对客户端透明的方式扩展对象的功能,是继承关系的一个替代方案. 装饰模式以对客户透明的方式动态的给 ...
- 浅谈设计模式--装饰者模式(Decorator Pattern)
挖了设计模式这个坑,得继续填上.继续设计模式之路.这次讨论的模式,是 装饰者模式(Decorator Pattern) 装饰者模式,有时也叫包装者(Wrapper),主要用于静态或动态地为一个特定的对 ...
- 深入浅出设计模式——装饰模式(Decorator Pattern)
模式动机 一般有两种方式可以实现给一个类或对象增加行为: 继承机制,使用继承机制是给现有类添加功能的一种有效途径,通过继承一个现有类可以使得子类在拥有自身方法的同时还拥有父类的方法.但是这种方法是静 ...
- 二十四种设计模式:装饰模式(Decorator Pattern)
装饰模式(Decorator Pattern) 介绍动态地给一个对象添加一些额外的职责.就扩展功能而言,它比生成子类方式更为灵活.示例有一个Message实体类,某个对象对它的操作有Insert()和 ...
- C#设计模式之装饰者模式(Decorator Pattern)
1.概述 装饰者模式,英文名叫做Decorator Pattern.装饰模式是在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能.它是通过创建一个包装对象,也就是装饰来包裹真实的对象. 2 ...
- 第 13 章 装饰模式【Decorator Pattern】
以下内容出自:<<24种设计模式介绍与6大设计原则>> Ladies and gentlemen,May I get your attention,Please?,Now I’ ...
- .NET设计模式(10):装饰模式(Decorator Pattern)
.NET设计模式(10):装饰模式(Decorator Pattern) 装饰模式(Decorator Pattern) --.NET设计模式系列之十 年月..在....对于..由于使用装饰模 ...
- 设计模式 - 装饰者模式(Decorator Pattern) Java的IO类 用法
装饰者模式(Decorator Pattern) Java的IO类 用法 本文地址: http://blog.csdn.net/caroline_wendy/article/details/26716 ...
- 乐在其中设计模式(C#) - 装饰模式(Decorator Pattern)
原文:乐在其中设计模式(C#) - 装饰模式(Decorator Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 装饰模式(Decorator Pattern) 作者:weba ...
随机推荐
- js 实现点击复制文本内容
js 实现点击复制文本内容 <table> <tr><td>姓名:<span onclick="copyContent(this);" ...
- iOS内购总结
内购流程: 1. 用户先拿到购买产品的单子, 2. 拿着单子去苹果那里交钱,交完钱让苹果在单子上盖个章 3.拿着盖了章的单子传给自己的服务器来验证是否真的支付成功,服务器是跟苹果验证(我们客户端也是可 ...
- CSS3-文本渐变色
在活动中,文字的颜色经常采用渐变的效果,在此,做一下笔记 <span>你好呀!</span> span{ background-image: linear-gradient(t ...
- Java 基础【04】数组内存分配
可能Java 数组大家都很熟悉,最近我遇到了一个关于Java 数组内存分配的问题. 突然发现许多书上“基本数据类型存储在栈内存当中,对象则保存在堆内存”这句话完全是错误的.下面是个简单的例子代码: p ...
- SQLite的sqlite_sequence表
SQLite的sqlite_sequence表 sqlite_sequence表也是SQLite的系统表.该表用来保存其他表的RowID的最大值.数据库被创建时,sqlite_sequence表会 ...
- luogu P3119 [USACO15JAN]草鉴定Grass Cownoisseur
题目描述 In an effort to better manage the grazing patterns of his cows, Farmer John has installed one-w ...
- springboot 集成 freemarker
前面我们已经实现了thymeleaf模板,其实freemarker和thymeleaf差不多,都可以取代JSP页面,实现步骤也差不多,我们来简单实现一下 引入pom.xml依赖如下 <depen ...
- java正则表达式的知识
/** 用途:正则表达式 * 创建人:向家康 * 创建日期:2019年4月21日 下午9:59:08 */ //有了登录界面当然少不了正则表达式啦,这是做项目必备的知识点 //通过本博客的代码,想必即 ...
- android -- 存储byte
public static String byteArrayToHexStr(byte[] byteArray) { if (byteArray == null){ return null; } ch ...
- iOS上如何让按钮(UIbutton)文本左对齐展示
// button.titleLabel.textAlignment = NSTextAlignmentLeft; 这句无效 button.contentHorizontalAlignment = U ...