装饰模式
模式 装饰
2023-09-11 14:21:03 时间
装饰模式,是在不改变原类文件和使用继承的情况下,动态的扩展一个对象的功能。它是通过创建一个包装对象,也就是装饰来包裹真实的对象。是为已有功能动态加入很多其它功能的一种方式。当系统须要新功能的时候,把每一个要装饰的功能放在单独的类中,并让这个类包装它所装饰的对象,因此,当须要执行特殊行为时,客户代码就能够在执行时依据须要有选择的、按顺序地使用装饰功能包装对象。
![](//img-blog.csdn.net/20140519225030609?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd3d3ZG9uZ3pp/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
装饰模式适用的场景:
- 须要动态的给一个对象加入功能,这些功能还能够动态的撤销。
- 须要添加一些基本功能的排列组合而产生的很大量的功能。或者因排列的顺序不同产生不同的效果的时候。
装饰模式的长处:
- Deorator模式与继承关系的目的都是要扩展对象的功能,可是Decorator能够提供比继承很多其它的灵活性。
- 通过使用不同的详细装饰类以及这些装饰类的排列组合,设计师能够创造床非常多不同行为的组合。
- 更重要的是,装饰模式把类中的装饰功能从类中搬移去除,这样能够简化原有的类,有效的把类的核心职责和装饰功能区分开了,并且能够去除相关类中反复的装饰逻辑。
装饰模式的缺点:
- 这样的比继承更加灵活机动的特性,也同一时候意味着更加多的复杂性。
- 装饰模式会导致设计中出现非常多的小类,假设过度使用,会使程序变得非常复杂。
C++代码实现:
Decorator.h
#include "stdafx.h" #include <iostream> #include <memory> using namespace std; class Component { public: virtual void Operation() = 0; }; class ConcreteComponent :public Component { virtual void Operation() { cout << "详细对象的操作!" << endl; } }; class Decorator :public Component { private: shared_ptr<Component > _pComponent; protected: shared_ptr<Component > GetComponent() { return _pComponent; } public: void SetComponent( shared_ptr<Component > pComponent ) { _pComponent = pComponent; } virtual void Operation() { if (_pComponent != NULL) { _pComponent->Operation(); } } }; class ConcreteDecorator1 :public Decorator { public: virtual void Operation() { //首先执行原Component的方法,再执行本类的方法 GetComponent()->Operation(); cout << "详细装饰对象ConcreteDecorator1" << endl; } }; class ConcreteDecorator2 :public Decorator { public: virtual void Operation() { GetComponent()->Operation(); cout << "详细装饰对象ConcreteDecorator2" << endl; } };
// DecorateMethod.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "Decorator.h" int _tmain (int argc , _TCHAR * argv []) { //装饰的方法是,首先建造ConcreteComponent对象, //然后建造ConcreteDecorator1来装饰ConcreteComponent //借助智能指针来实现. //最后运行的是最后的装饰类的Operation shared_ptr<ConcreteComponent > pConcreteComponent(new ConcreteComponent); shared_ptr<ConcreteDecorator1 > pConcreteDecorator1(new ConcreteDecorator1); pConcreteDecorator1->SetComponent(pConcreteComponent); pConcreteDecorator1->Operation(); std::cout << std::endl; shared_ptr<ConcreteDecorator2 > pConcreteDecorator2(new ConcreteDecorator2); pConcreteDecorator2->SetComponent(pConcreteDecorator1); pConcreteDecorator2->Operation(); getchar(); return 0; }
相关文章
- 设计模式复习-装饰模式
- C++设计模式 ==> 装饰(者)模式
- C# 最基本的涉及模式(单例模式) C#种死锁:事务(进程 ID 112)与另一个进程被死锁在 锁 | 通信缓冲区 资源上,并且已被选作死锁牺牲品。请重新运行该事务,解决方案: C#关闭应用程序时如何关闭子线程 C#中 ThreadStart和ParameterizedThreadStart区别
- Linux Epoll ET模式EPOLLOUT和EPOLLIN触发时刻
- 设计模式之“行为型模式”(二)
- Decorator [ˈdekəreɪtə(r)] 修饰器/装饰器 -- 装饰模式
- java反射增加装饰模式的普适性
- 设计模式--结构性模式--装饰器模式(装饰设计模式)
- linux内核中i2c驱动中slave模式接口的调用
- Hadoop的单机模式
- 踢球时想到的装饰器模式
- 从ES6重新认识JavaScript设计模式: 装饰器模式
- 【设计模式】我终于读懂了装饰者模式。。。
- Spring中Template模式与callback的结合使用浅析
- vue cli选择lint模式
- 《JavaScript设计模式》——9.12 Decorator(装饰者)模式
- 《游戏编程模式》一7.10 下推自动机
- Python实现装饰模式的一段代码
- 基于Task的异步模式--全面介绍
- 数理逻辑——算法数学的形式化语言---通过一些确定的思维“模式”建立起连接假设与结论的纽带
- 完备对象模式-配置-构造模式
- C++装饰者模式
- libcurl使用easy模式阻塞卡死等问题的完美解决---超时设置
- 设计模式——模板方法模式
- 设计模式【装饰模式】
- 单片机根据应答发送AT指令控制ESP8266设置为服务器—AP模式
- Decorator装饰者模式(结构型模式)
- 简说设计模式——外观模式