设计模式:6 装饰模式
2015-08-12 01:47
316 查看
装饰模式:
动态地给一个对象添加一些额外职责。增加功能比生成子类更加灵活。
模式图:
Component:定义对象接口,给对象动态添加职责
ConcreteComponent:定义具体对象,给对象添加职责
Decorator:装饰抽象类,继承自Component,从外类来扩展Coponent类的功能,Component无需知道Decorator的存在
ConcreteDecorator:具体的装饰对象,能给Component添加职责
本质:
装饰模式利用setComponent来对对象进行包装,每个装饰对象的实现就和如何使用这个对象分离开,每个装饰对象只需要关注自己的功能,不需要关心如何被添加到对象链中
技巧:
如果只有一个ConcreteComponent类,而没有抽象的Component类,那么Decorator类可以是ConcreteComponent的一个子类。
如果只有一个ConcreteDecorator类,那么没有必要建立一个单独的Decorator类,可以把Decorator类和ConcreteDecorator类的责任合并成一个类。
因此:这里只有一个具体装饰类即,服装类,那么就将唯一具体装饰类:服装类作为抽象装饰类和具体装饰类合并成一个类。
只有一个具体组件类:人,没有抽象组件类,就让装饰类成为具体组件类:人的子类
总结:
装饰模式是为已有功能动态添加更多功能,新加入的东西只是为了满足某特定情况下才会执行的行为,它把每个需要装饰的功能放在单独的类中,并用这个类包装他要装饰的对象。
把类的核心职责和装饰功能区分开,去除相关类中重复的装饰逻辑。
main.cpp
Component.h
ConcreteComponent.h
ConcreteComponent.cpp
Decorator.h
Decorator.cpp
ConcreteDecoratorA.h
ConcreteDecoratorA.cpp
ConcreteDecoratorB.h
ConcreteDecoratorB.cpp
动态地给一个对象添加一些额外职责。增加功能比生成子类更加灵活。
模式图:
Component:定义对象接口,给对象动态添加职责
ConcreteComponent:定义具体对象,给对象添加职责
Decorator:装饰抽象类,继承自Component,从外类来扩展Coponent类的功能,Component无需知道Decorator的存在
ConcreteDecorator:具体的装饰对象,能给Component添加职责
本质:
装饰模式利用setComponent来对对象进行包装,每个装饰对象的实现就和如何使用这个对象分离开,每个装饰对象只需要关注自己的功能,不需要关心如何被添加到对象链中
技巧:
如果只有一个ConcreteComponent类,而没有抽象的Component类,那么Decorator类可以是ConcreteComponent的一个子类。
如果只有一个ConcreteDecorator类,那么没有必要建立一个单独的Decorator类,可以把Decorator类和ConcreteDecorator类的责任合并成一个类。
因此:这里只有一个具体装饰类即,服装类,那么就将唯一具体装饰类:服装类作为抽象装饰类和具体装饰类合并成一个类。
只有一个具体组件类:人,没有抽象组件类,就让装饰类成为具体组件类:人的子类
总结:
装饰模式是为已有功能动态添加更多功能,新加入的东西只是为了满足某特定情况下才会执行的行为,它把每个需要装饰的功能放在单独的类中,并用这个类包装他要装饰的对象。
把类的核心职责和装饰功能区分开,去除相关类中重复的装饰逻辑。
main.cpp
#include <iostream> #include <stdlib.h> #include "ConcreteComponent.h" #include "ConcreteDecoratorA.h" #include "ConcreteDecoratorB.h" using namespace std; void process() { ConcreteComponent c; ConcreteDecoratorA d1; ConcreteDecoratorB d2; /*装饰的方法:先调用ConcreteComponnet实例化对象c,然后用 ConcreteDecoratorA的实例化对象d1来包装c,再用ConcreteDecoratorB 的对象d2包装d1,最终执行d2的operation */ d1.setComponent(c); d2.setComponent(d1); d2.operation(); } int main(int argc,char* argv[]) { process(); system("pause"); return 0; }
Component.h
#pragma once class Component { public: Component(void); virtual ~Component(void); //virtual void operation()=0; virtual void operation(){} };
ConcreteComponent.h
#pragma once #include "Component.h" class ConcreteComponent : public Component { public: ConcreteComponent(void); ~ConcreteComponent(void); void operation(); };
ConcreteComponent.cpp
#include "ConcreteComponent.h" #include <iostream> ConcreteComponent::ConcreteComponent(void) { } ConcreteComponent::~ConcreteComponent(void) { } void ConcreteComponent::operation() { std::cout << "具体对象的操作" << std::endl; }
Decorator.h
#pragma once #include "component.h" class Decorator : public Component { public: Decorator(void); virtual ~Decorator(void); void setComponent(Component& component); void operation(); protected: Component _component; };
Decorator.cpp
#include "Decorator.h" Decorator::Decorator(void) { } Decorator::~Decorator(void) { } void Decorator::operation() { _component.operation(); } void Decorator::setComponent(Component& component) { _component = component; }
ConcreteDecoratorA.h
#pragma once #include "decorator.h" #include <string> class ConcreteDecoratorA : public Decorator { public: ConcreteDecoratorA(void); ~ConcreteDecoratorA(void); void operation(); private: std::string _addedState;//本类的独有功能,用于区别ConcreteDecoratorB };
ConcreteDecoratorA.cpp
#include "ConcreteDecoratorA.h" #include <iostream> ConcreteDecoratorA::ConcreteDecoratorA(void) { } ConcreteDecoratorA::~ConcreteDecoratorA(void) { } void ConcreteDecoratorA::operation() { _component.operation(); //首先运行原来Component的operation,再执行本类的功能,相当于对原有的Component进行类装饰 _addedState = "新状态"; std::cout << "具体装饰对象A的操作" << std::endl; }
ConcreteDecoratorB.h
#pragma once #include "decorator.h" class ConcreteDecoratorB : public Decorator { public: ConcreteDecoratorB(void); ~ConcreteDecoratorB(void); void operation(); private: void addedBehavior(); };
ConcreteDecoratorB.cpp
#include "ConcreteDecoratorB.h" #include <iostream> ConcreteDecoratorB::ConcreteDecoratorB(void) { } ConcreteDecoratorB::~ConcreteDecoratorB(void) { } void ConcreteDecoratorB::operation() { _component.operation(); addedBehavior(); std::cout << "具体装饰对象B的操作" << std::endl; } void ConcreteDecoratorB::addedBehavior() { }
相关文章推荐
- 2.x ESL第二章习题2.4
- 求gcd(a,b) = ax + by中的gcd(a, b)、a、b(扩展欧几里得算法尾递归版)
- 实现手机QQ的抖动效果
- 【转载】PyTables Windows平台安装说明
- !HDU 5371 最长双回文串(多校7)-卡时间-(manacher+排序+set+lower_bound())
- Keil使用malloc异常进入hardfault调试故障手记
- 08 JAVA 线程 内存模型(二)
- 天猫首页迷思之-jquery实现左侧广告牌图片轮播
- 用WinHex找回丢失的分区
- HDU 3577 Fast Arrangement
- Android中自定义属性(attrs.xml,TypedArray的使用)
- 设计模式:5 依赖倒转原则
- 60.电话 信息 邮件 评分 打开其他应用
- 设计模式: 4开放封闭原则
- SQL语法之查询语句
- hdu 3693 Math teacher's homework(数位dp)
- ACM 2015 中国赛区时间
- 设计模式: 3 单一职责原则
- 设计模式:2策略模式
- POJ 3321 Apple Tree 【树形结构转变为线性结构+线段树OR树状数组】