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

文章目录

    • 一、什么是装饰器模式
    • 二、为什么需要装饰器模式及应用场景
    • 三、装饰器模式的代码实现

一、什么是装饰器模式

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

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

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

使用场景包括

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

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

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

//+------------------------------------------------------------------+
//| 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
//+------------------------------------------------------------------+

你可能感兴趣的:(新版MQL语言程序设计,装饰器模式,开发语言,MQL,设计模式,MT4,MT5,学习)