新版MQL语言程序设计:装饰器模式的原理、应用及代码实现

文章目录

一、什么是装饰器模式

装饰器模式是一种结构型设计模式,它允许你通过将对象包装在一个装饰器类的对象中来动态地扩展其功能。装饰器模式提供了一种比继承更加灵活的方式来扩展对象的功能,同时也遵循了开闭原则。

二、为什么需要装饰器模式及应用场景

当需要在不改变原有对象结构的情况下,动态地给对象增加额外的功能。它可以在运行时动态地添加、修改或删除对象的行为,使得代码更加灵活和可扩展。

使用场景包括

  1. 动态地给对象添加功能:装饰器模式可以在不改变原有对象的情况下,动态地给对象添加新的功能。这样可以避免创建大量的子类来实现不同的功能组合,提高了代码的可维护性和可扩展性。
  2. 需要对对象的功能进行扩展或修改:装饰器模式可以在运行时动态地给对象添加新的功能,而不需要修改原有对象的代码。这样可以避免因为修改原有对象而引入风险和错误。
  3. 需要对对象的功能进行组合:装饰器模式可以通过不同的装饰器组合来实现不同的功能效果。这样可以灵活地组合不同的功能,满足不同的需求。

总之,装饰器模式的主要作用是在不改变原有对象结构的情况下,动态地给对象增加额外的功能,提高了代码的灵活性和可扩展性。

三、装饰器模式的代码实现

cpp 复制代码
//+------------------------------------------------------------------+
//| interface --- for patterns                                         |
//+------------------------------------------------------------------+
interface ClientInterface
{
    string Output(void); // returns header
    void Run(void);      // execute the pattern client
};
//+------------------------------------------------------------------+
//| interface --- for patterns                                         |
//+------------------------------------------------------------------+
void Run(ClientInterface* client) //launches a pattern
{
    printf("---\n%s", client.Output()); //print pattern header
    client.Run(); //execute client collaborations
    delete client; //exit
}
//+------------------------------------------------------------------+
//| structure                                                        |
//+------------------------------------------------------------------+
//
//                  | Component |<------------------------------+
//                  |-----------|                               |
//                  |Operation()|                               |
//                        ^                                     |
//                        |                                     |
//            +-----------+-------------+                       |
//            |                         |             component |
//   |ConcreteComponent|    |       Decorator       |-----------+
//   |-----------------|    |-----------------------|
//   |Operation()      |    |Operation()            |
//                          |  component.Operation()|
//                                      ^
//                                      |
//                        +-------------+----------+
//                        |                        |
//               |ConcreteDecoratorA|  |   ConcreteDecoratorB  |
//               |------------------|  |-----------------------|
//               |Operation()       |  |Operation()            |
//               |------------------|  | Decorator::Operation()|
//               |added_state       |  | AddedBehavior()       |
//                                     |AddedBehavior()        |
//
//+------------------------------------------------------------------+
// 对象的接口, 可以动态添加职责
class Component
{
    public:
        virtual void      Operation(void)=0;
};
// 装饰器, 符合组件的接口, 对组件对象的引用
class Decorator:public Component
{
    public:
        Component*        component;
        void              Operation(void);
};
//
void Decorator::Operation(void)
{
    if (CheckPointer(component) > 0)
    {
        component.Operation();
    }
}
// 定义--对象--可以附加额外责任的对象
class ConcreteComponent:public Component
{
    public:
        void              Operation(void);
};
//
void ConcreteComponent::Operation(void)
{
    Print("concrete operation");
}
// 为组件添加职责
class ConcreteDecoratorA:public Decorator
{
    protected:
        string            added_state;
    public:
        ConcreteDecoratorA(void);
        void              Operation(void);
};
//+------------------------------------------------------------------+
//| participants > concrete decorator > a                            |
//+------------------------------------------------------------------+
ConcreteDecoratorA::ConcreteDecoratorA(void) : added_state("added state")
{

}
//+------------------------------------------------------------------+
//| participants > concrete decorator > a                            |
//+------------------------------------------------------------------+
void ConcreteDecoratorA::Operation(void)
{
    Decorator::Operation();
    Print(added_state);
}
//+------------------------------------------------------------------+
//| participants > concrete decorator > b                            |
//+------------------------------------------------------------------+
class ConcreteDecoratorB : public Decorator
{
    public:
        void              AddedBehavior(void);
        void              Operation(void);
};
//+------------------------------------------------------------------+
//| participants > concrete decorator > b                            |
//+------------------------------------------------------------------+
void ConcreteDecoratorB::AddedBehavior(void)
{
    Print("added behavior");
}
//+------------------------------------------------------------------+
//| participants > concrete decorator > b                            |
//+------------------------------------------------------------------+
void ConcreteDecoratorB::Operation(void)
{
    Decorator::Operation();
    AddedBehavior();
}
//+------------------------------------------------------------------+
//| participants > client                                            |
//+------------------------------------------------------------------+
class Client:public ClientInterface
{
    public:
        string            Output(void);
        void              Run(void);
};
string Client::Output(void) {return __FUNCTION__;}
//+------------------------------------------------------------------+
//| collaborations                                                   |
//+------------------------------------------------------------------+
void Client::Run(void)
{
    Component* component = new ConcreteComponent();
    Decorator* decorator_a = new ConcreteDecoratorA();
    Decorator* decorator_b = new ConcreteDecoratorB();
    decorator_a.component = component;
    decorator_b.component = decorator_a;
    decorator_b.Operation();
    //---
    delete component;
    delete decorator_a;
    delete decorator_b;
}
//+------------------------------------------------------------------+
//| related patterns                                                 |
//+------------------------------------------------------------------+
// 适配器模式 -> 将为对象提供一个全新的接口
// 装饰器模式 -> 更改对象的职责,而不是其接口
//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
void OnStart() 
{
    //---structural
    Run(new Decorator::Client);
}
//+------------------------------------------------------------------+
//| output                                                           |
//+------------------------------------------------------------------+
//   Decorator::Client::Output
//   concrete operation
//   added state
//   added behavior
//+------------------------------------------------------------------+
相关推荐
yufei-coder14 分钟前
C#基础语法
开发语言·c#·.net
长天一色14 分钟前
【ECMAScript 从入门到进阶教程】第三部分:高级主题(高级函数与范式,元编程,正则表达式,性能优化)
服务器·开发语言·前端·javascript·性能优化·ecmascript
yngsqq19 分钟前
031集——文本文件按空格分行——C#学习笔记
笔记·学习·c#
_.Switch26 分钟前
Python机器学习模型的部署与维护:版本管理、监控与更新策略
开发语言·人工智能·python·算法·机器学习
醉颜凉28 分钟前
银河麒麟桌面操作系统修改默认Shell为Bash
运维·服务器·开发语言·bash·kylin·国产化·银河麒麟操作系统
刷帅耍帅31 分钟前
设计模式-桥接模式
设计模式·桥接模式
NiNg_1_23434 分钟前
Vue3 Pinia持久化存储
开发语言·javascript·ecmascript
带带老表学爬虫43 分钟前
java数据类型转换和注释
java·开发语言
qianbo_insist1 小时前
simple c++ 无锁队列
开发语言·c++
zengy51 小时前
Effective C++中文版学习记录(三)
数据结构·c++·学习·stl