我与C++设计模式(七)——装饰者
来源:互联网 发布:宁波大数据培训 编辑:程序博客网 时间:2024/06/08 03:01
装饰者这个模式不好理解,或者说是我自己理解得不好吧。它的应用需求来源于添加需求,当我们已经设计好了一个接口(父类),并且也有几个实现类,但是当需求被添加的时候,我们可能需要修改接口,在接口中添加这个新需求的虚函数,并在各个实现类中去添加实现,这样的做法比价低效,工作量大,也不符合我们的开放-封闭原则。这时装饰者模式就仓啷啷出场了,装饰者,作为半成品完成,后期加工的一个过程,不就如问题中所描述的一样么,蛋糕做好了,该写生快了。
还是按UML图来说,见图,或点击这儿
Component就是所谓的接口,左边是之前实现的类,这里仅列举一个意思一下。右边是装饰者的接口类Decorator和实现类C..Decoractor。Decorator也继承了Component,并且类中还设计了一个Component的指针,client只需要操作ConcreteDecorator即可,为了不丢失之前继承实现的那些操作,我就需要一个Component的指针,如果只是在Decorator中设计一个Component的指针能保证这点,而为了让用户能够用Decorator类指针完成这些前期操作,那么需要为其一一设置接口,不就是把Component的接口复制一遍么,还不如继承一次,虽然逻辑深了一步,但问题很方便就解决了,剩下的工作就是通过内置的Component指针调用各接口,而Decorator的继承类实现完成新功能即可,这就是装饰者。
代码:
//component
#ifndef _COMPONENT_H__#define _COMPONENT_H__class component{ public: component(); virtual ~component(); virtual void operation() = 0;};#endif
//component.cpp#include "component.h"component::component(){}component::~component(){}
//concrete_component
#ifndef _CONCRETE_COMPONENT__#define _CONCRETE_COMPONENT__#include "component.h"class concrete_component:public component{ public: concrete_component(); ~concrete_component(); void operation();};#endif
#include <iostream>#include "concrete_component.h"using namespace std;concrete_component::concrete_component(){}concrete_component::~concrete_component(){}void concrete_component::operation(){ cout<<"OPERATION FROM CONCRETE_COMPONENT"<<endl;}
//decorator
#ifndef _DECORATOR_H__#define _DECORATOR_H__#include "component.h"class decorator:public component{ public: decorator(); virtual ~decorator(); void set_component(component *); virtual void operation(); protected: component *pCom;};#endif
<pre name="code" class="cpp">//decorator.cpp#include "decorator.h"decorator::decorator():pCom(0){}decorator::~decorator(){}void decorator::set_component(component *pCom){ this->pCom = pCom;}void decorator::operation(){ pCom->operation();}
//concrete_decorator
<pre name="code" class="cpp">#ifndef _CONCRETE_DECORATOR__#define _CONCRETE_DECORATOR__#include "decorator.h"class concrete_decorator:public decorator{ public: concrete_decorator(); ~concrete_decorator(); void operation(); virtual void add_behavior();};#endif
//concrete_decorator.cpp#include "concrete_decorator.h"#include <iostream>using namespace std;concrete_decorator::concrete_decorator(){}concrete_decorator::~concrete_decorator(){}void concrete_decorator::add_behavior(){ cout<<"A NEW BEHAVIOR FORM CONCRETE_DECORATOR"<<endl;}void concrete_decorator::operation(){ pCom->operation(); this->add_behavior();}
//main
<pre name="code" class="cpp">//main.cpp#include "concrete_component.h"#include "concrete_decorator.h"int main(int argc, char **argv){ component * pCom = new concrete_component(); decorator * pDec = new concrete_decorator(); pDec->set_component(pCom); pDec->operation();}
0 0
- 我与C++设计模式(七)——装饰者
- 设计模式(七)——装饰模式
- AspectJ实现设计模式(七)—装饰模式
- 设计模式——装饰者模式(c++)
- 设计模式——装饰者模式(C++)
- 设计模式——装饰者模式与适配器模式
- 设计模式(七)装饰者和观察者模式
- C#设计模式(七)装饰者模式
- 设计模式之七 装饰者模式
- JS设计模式七:装饰者模式
- JAVA设计模式(七)——装饰模式
- Java设计模式(七)----装饰模式
- 设计模式(七)装饰模式
- 设计模式(七)装饰模式
- 设计模式(七):装饰模式
- 软考设计模式——装饰模式(C++)
- Android与设计模式——装饰者(Decorator)模式
- C++与设计模式(11)——装饰模式
- c语言实现求逆矩阵
- Android Bundle类
- 稀疏表达: 向量、矩阵与张量(上篇和中篇)
- 黑马程序员_gets()和scanf()
- 找工作经验之——面试(华为篇)
- 我与C++设计模式(七)——装饰者
- 592- 使用的Hint 无效-续
- PAT 2-08 用扑克牌计算24点(C语言实现)
- uva1306 - The K-League 网络流
- 找工作经验之——笔试
- hdu1074 状态压缩dp+记录方案
- 杭电oj An easy problem
- Json技术使用代码示例
- 《牛腩新闻发布系统》回顾(一)