MQL语言实现抽象工厂模式

2024-03-15 23:04

本文主要是介绍MQL语言实现抽象工厂模式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

    • 一、定义抽象产品接口
    • 二、定义抽象工厂接口
    • 三、定义具体产品
    • 四、定义具体工厂
    • 五、定义工厂客户端
    • 六、客户端调用工厂客户端
    • 七、抽象工厂模式的结构

一、定义抽象产品接口

//+------------------------------------------------------------------+
//| participants                                                     |
//+------------------------------------------------------------------+
//   abstract product > declares an interface for a type of products
//+------------------------------------------------------------------+
//| participants > abstract product                                  |
//+------------------------------------------------------------------+
interface AbstractProductA{};
//+------------------------------------------------------------------+
//| participants > abstract product                                  |
//+------------------------------------------------------------------+
interface AbstractProductB{void Interact(AbstractProductA*);};

二、定义抽象工厂接口

//+------------------------------------------------------------------+
//| participants                                                     |
//+------------------------------------------------------------------+
interface AbstractFactory
//   declares an interface for operations that create abstract products
{AbstractProductA* CreateProductA(void);AbstractProductB* CreateProductB(void);
};

三、定义具体产品

//+------------------------------------------------------------------+
//| participants                                                     |
//+------------------------------------------------------------------+
//   concrete product 
//      defines > product object to be created > by concrete factory
//      implements > abstract product interface
//+------------------------------------------------------------------+
//| participants > concrete product                                  |
//+------------------------------------------------------------------+
class ProductA1:public AbstractProductA
{public:ProductA1(void);
};
void ProductA1::ProductA1(void) {Print("product a1 constructed");}
//+------------------------------------------------------------------+
//| participants > concrete product                                  |
//+------------------------------------------------------------------+
class ProductA2:public AbstractProductA
{public:ProductA2(void);
};
void ProductA2::ProductA2(void) {Print("product a2 constructed");}
//+------------------------------------------------------------------+
//| participants > concrete product                                  |
//+------------------------------------------------------------------+
class ProductB1:public AbstractProductB
{public:ProductB1(void);void              Interact(AbstractProductA*);
};
void ProductB1::ProductB1(void) {Print("product b1 constructed");}
void ProductB1::Interact(AbstractProductA*src)
{Print("product b1: ",&this," is interacting with product a: ",src);
}
//+------------------------------------------------------------------+
//| participants > concrete product                                  |
//+------------------------------------------------------------------+
class ProductB2:public AbstractProductB
{public:ProductB2(void);void              Interact(AbstractProductA*);
};
void ProductB2::ProductB2(void) {Print("product b2 constructed");}
void ProductB2::Interact(AbstractProductA*src)
{Print("product b2: ",&this," is interacting with product a: ",src);
}
//
//

四、定义具体工厂

//+------------------------------------------------------------------+
//| participants                                                     |
//+------------------------------------------------------------------+
//   concrete factory > implements operations > create concrete products
//+------------------------------------------------------------------+
//| participants > concrete factory                                  |
//+------------------------------------------------------------------+
class Factory1:public AbstractFactory
{public:Factory1(void);AbstractProductA* CreateProductA(void);AbstractProductB* CreateProductB(void);
};
//+------------------------------------------------------------------+
//| participants > concrete factory > factory 1                      |
//+------------------------------------------------------------------+
void Factory1::Factory1(void)
{Print("factory 1: ",&this," constructed");
}
//+------------------------------------------------------------------+
//| participants > concrete factory > factory 1                      |
//+------------------------------------------------------------------+
AbstractProductA* Factory1::CreateProductA(void)
{printf("factory 1 is creating and returning product a1");return new ProductA1;
}
//+------------------------------------------------------------------+
//| participants > concrete factory > factory 1                      |
//+------------------------------------------------------------------+
AbstractProductB* Factory1::CreateProductB(void)
{printf("factory 1 is creating and returning product b1");return new ProductB1;
}
//+------------------------------------------------------------------+
//| participants > concrete factory                                  |
//+------------------------------------------------------------------+
class Factory2:public AbstractFactory
{public:Factory2(void);AbstractProductA* CreateProductA(void);AbstractProductB* CreateProductB(void);
};
//+------------------------------------------------------------------+
//| participants > concrete factory > factory 2                      |
//+------------------------------------------------------------------+
void Factory2::Factory2(void)
{Print("factory 2: ",&this," constructed");
}
//+------------------------------------------------------------------+
//| participants > concrete factory > factory 2                      |
//+------------------------------------------------------------------+
AbstractProductA* Factory2::CreateProductA(void)
{printf("factory 2 is creating and returning product a2");return new ProductA2;
}
//+------------------------------------------------------------------+
//| participants > concrete factory > factory 2                      |
//+------------------------------------------------------------------+
AbstractProductB* Factory2::CreateProductB(void)
{printf("factory 2 is creating and returning product b2");return new ProductB2;
}

五、定义工厂客户端

//+------------------------------------------------------------------+
//| participants                                                     |
//+------------------------------------------------------------------+
class FactoryClient
//   uses interfaces > declared by > abstract factory, abstract product
{public:void              Run(void);void              Switch(AbstractFactory*);FactoryClient(AbstractFactory*);~FactoryClient(void);protected:AbstractProductA* apa;AbstractProductB* apb;AbstractFactory*  factory;void              Delete(void);
};
//+------------------------------------------------------------------+
//| participants > factory client                                    |
//+------------------------------------------------------------------+
void FactoryClient::FactoryClient(AbstractFactory* af)
{Print("factory client created and received abstract factory ",af);Print("factory client is requesting to accept/switch the factories");Switch(af);
}
//+------------------------------------------------------------------+
//| participants > factory client                                    |
//+------------------------------------------------------------------+
void FactoryClient::~FactoryClient(void)
{Delete();
}
//+------------------------------------------------------------------+
//| participants > factory client                                    |
//+------------------------------------------------------------------+
void FactoryClient::Run(void)
{Print("factory client is running abstract product b");apb.Interact(apa);
}
//+------------------------------------------------------------------+
//| participants > factory client                                    |
//+------------------------------------------------------------------+
void FactoryClient::Delete(void)
{delete apa;delete apb;delete factory;
}
//+------------------------------------------------------------------+
//| participants > factory client                                    |
//+------------------------------------------------------------------+
void FactoryClient::Switch(AbstractFactory *af)
{string sFactory;StringConcatenate(sFactory,sFactory,factory);int iFactory=(int)StringToInteger(sFactory);if(iFactory>0){Print("factory client is switching old factory ",factory," to new factory ",af);}else{Print("factory client is accepting new factory ",af);}Delete();factory=af;Print("factory client saved the new factory");Print("factory client is requesting its new factory to create product a");apa=factory.CreateProductA();Print("factory client is requesting its new factory to create product b");apb=factory.CreateProductB();
}

六、客户端调用工厂客户端

//+------------------------------------------------------------------+
//| interface for patterns                                           |
//+------------------------------------------------------------------+
interface ClientExample //pattern client
{string Output(void); //returns headervoid Run(void); //execute the pattern client
};//+------------------------------------------------------------------+
//| participants                                                     |
//+------------------------------------------------------------------+
class Client:public ClientExample{
public:string            Output(void);void              Run(void);};
string Client::Output(void) {return __FUNCTION__;}
//+------------------------------------------------------------------+
//| collaborations                                                   |
//+------------------------------------------------------------------+
void Client::Run(void)
//   concrete factory
//      a single instance > normally created at run-time
//      creates products > with particular implementation
//      client uses other factory > for different product objects 
//   abstract factory
//      defers creation > product objects > concrete factory subclass{Print("client is requesting to create factory 1");Print("client is requesting to create the factory client");Print("client is requesting the factory client to manage factory 1");FactoryClient client(new Factory1);Print("client is requesting the factory client to operate");client.Run();Print("client is requesting to create new factory 2 and asking factory client to switch factories");client.Switch(new Factory2);Print("client is requesting the factory client to run again");client.Run();}
}

七、抽象工厂模式的结构

//+------------------------------------------------------------------+
//| structure                                                        |
//+------------------------------------------------------------------+
//
//            | AbstractFactory|<-----------------------------------------|Client|
//            |----------------|                                              |
//            |CreateProductA()|                                              |
//            |CreateProductA()|                    |AbstractProductA|<-------+
//                     ^                                    ^                 |
//                     |                                    |                 |
//         +-----------+----------+                   +-----+-----+           |
//         |                      |                   |           |           |
//|ConcreteFactory1|- +  |ConcreteFactory2|- + ->|ProductA2| |ProductA1|<- +  |
//|----------------|  |  |----------------|  |                             |  |
//|CreateProductA()|     |CreateProductA()|                                   |
//|CreateProductB()|  |  |CreateProductB()|  |                             |  |
//                                                  |AbstractProductB|<----+--+
//                    |                      |              ^              |
//                                                          |
//                    |                      |        +-----+-----+        |
//                                                    |           |
//                    |                      + ->|ProductB2| |ProductB1|<- +
//                                                                         |
//                    +  - - - - - - - - - - - - - - - - - - - - - - - - - +

这篇关于MQL语言实现抽象工厂模式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/813551

相关文章

深入理解Go语言中二维切片的使用

《深入理解Go语言中二维切片的使用》本文深入讲解了Go语言中二维切片的概念与应用,用于表示矩阵、表格等二维数据结构,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧... 目录引言二维切片的基本概念定义创建二维切片二维切片的操作访问元素修改元素遍历二维切片二维切片的动态调整追加行动态

Linux下删除乱码文件和目录的实现方式

《Linux下删除乱码文件和目录的实现方式》:本文主要介绍Linux下删除乱码文件和目录的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux下删除乱码文件和目录方法1方法2总结Linux下删除乱码文件和目录方法1使用ls -i命令找到文件或目录

SpringBoot+EasyExcel实现自定义复杂样式导入导出

《SpringBoot+EasyExcel实现自定义复杂样式导入导出》这篇文章主要为大家详细介绍了SpringBoot如何结果EasyExcel实现自定义复杂样式导入导出功能,文中的示例代码讲解详细,... 目录安装处理自定义导出复杂场景1、列不固定,动态列2、动态下拉3、自定义锁定行/列,添加密码4、合并

mybatis执行insert返回id实现详解

《mybatis执行insert返回id实现详解》MyBatis插入操作默认返回受影响行数,需通过useGeneratedKeys+keyProperty或selectKey获取主键ID,确保主键为自... 目录 两种方式获取自增 ID:1. ​​useGeneratedKeys+keyProperty(推

Spring Boot集成Druid实现数据源管理与监控的详细步骤

《SpringBoot集成Druid实现数据源管理与监控的详细步骤》本文介绍如何在SpringBoot项目中集成Druid数据库连接池,包括环境搭建、Maven依赖配置、SpringBoot配置文件... 目录1. 引言1.1 环境准备1.2 Druid介绍2. 配置Druid连接池3. 查看Druid监控

Linux在线解压jar包的实现方式

《Linux在线解压jar包的实现方式》:本文主要介绍Linux在线解压jar包的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux在线解压jar包解压 jar包的步骤总结Linux在线解压jar包在 Centos 中解压 jar 包可以使用 u

c++ 类成员变量默认初始值的实现

《c++类成员变量默认初始值的实现》本文主要介绍了c++类成员变量默认初始值,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录C++类成员变量初始化c++类的变量的初始化在C++中,如果使用类成员变量时未给定其初始值,那么它将被

Qt使用QSqlDatabase连接MySQL实现增删改查功能

《Qt使用QSqlDatabase连接MySQL实现增删改查功能》这篇文章主要为大家详细介绍了Qt如何使用QSqlDatabase连接MySQL实现增删改查功能,文中的示例代码讲解详细,感兴趣的小伙伴... 目录一、创建数据表二、连接mysql数据库三、封装成一个完整的轻量级 ORM 风格类3.1 表结构

基于Python实现一个图片拆分工具

《基于Python实现一个图片拆分工具》这篇文章主要为大家详细介绍了如何基于Python实现一个图片拆分工具,可以根据需要的行数和列数进行拆分,感兴趣的小伙伴可以跟随小编一起学习一下... 简单介绍先自己选择输入的图片,默认是输出到项目文件夹中,可以自己选择其他的文件夹,选择需要拆分的行数和列数,可以通过

Python中将嵌套列表扁平化的多种实现方法

《Python中将嵌套列表扁平化的多种实现方法》在Python编程中,我们常常会遇到需要将嵌套列表(即列表中包含列表)转换为一个一维的扁平列表的需求,本文将给大家介绍了多种实现这一目标的方法,需要的朋... 目录python中将嵌套列表扁平化的方法技术背景实现步骤1. 使用嵌套列表推导式2. 使用itert