新版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
//+------------------------------------------------------------------+
相关推荐
Hello.Reader18 小时前
算法基础(十)——分治思想把大问题拆成小问题
java·开发语言·算法
一只大袋鼠18 小时前
JavaWeb四种文件上传方式(下篇)
java·开发语言·springmvc·javaweb
风兮雨露18 小时前
VMware虚拟机(安装/绿色版)
学习
TE-茶叶蛋18 小时前
深入研究 yudao-framework 模块:Java 编程能力提升指南
java·开发语言
逻辑驱动的ken18 小时前
Java高频考点场景题24
java·开发语言·面试·职场和发展·求职招聘
兔小盈18 小时前
多线程-(五)线程安全之内存可见性
java·开发语言·多线程
zl_dfq19 小时前
python学习8 之 【集合、datetime模块、字典】
学习
Supersist19 小时前
【设计模式03】使用模版模式+责任链模式优化实战
后端·设计模式·代码规范
yaoxin52112319 小时前
400. Java 文件操作基础 - 使用 Buffered Stream I/O 读取文本文件
java·开发语言·python
kdxiaojie19 小时前
U-Boot分析【学习笔记】(3)
linux·笔记·学习