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

相关文章

hdu1043(八数码问题,广搜 + hash(实现状态压缩) )

利用康拓展开将一个排列映射成一个自然数,然后就变成了普通的广搜题。 #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#inclu

【C++】_list常用方法解析及模拟实现

相信自己的力量,只要对自己始终保持信心,尽自己最大努力去完成任何事,就算事情最终结果是失败了,努力了也不留遗憾。💓💓💓 目录   ✨说在前面 🍋知识点一:什么是list? •🌰1.list的定义 •🌰2.list的基本特性 •🌰3.常用接口介绍 🍋知识点二:list常用接口 •🌰1.默认成员函数 🔥构造函数(⭐) 🔥析构函数 •🌰2.list对象

【Prometheus】PromQL向量匹配实现不同标签的向量数据进行运算

✨✨ 欢迎大家来到景天科技苑✨✨ 🎈🎈 养成好习惯,先赞后看哦~🎈🎈 🏆 作者简介:景天科技苑 🏆《头衔》:大厂架构师,华为云开发者社区专家博主,阿里云开发者社区专家博主,CSDN全栈领域优质创作者,掘金优秀博主,51CTO博客专家等。 🏆《博客》:Python全栈,前后端开发,小程序开发,人工智能,js逆向,App逆向,网络系统安全,数据分析,Django,fastapi

让树莓派智能语音助手实现定时提醒功能

最初的时候是想直接在rasa 的chatbot上实现,因为rasa本身是带有remindschedule模块的。不过经过一番折腾后,忽然发现,chatbot上实现的定时,语音助手不一定会有响应。因为,我目前语音助手的代码设置了长时间无应答会结束对话,这样一来,chatbot定时提醒的触发就不会被语音助手获悉。那怎么让语音助手也具有定时提醒功能呢? 我最后选择的方法是用threading.Time

Android实现任意版本设置默认的锁屏壁纸和桌面壁纸(两张壁纸可不一致)

客户有些需求需要设置默认壁纸和锁屏壁纸  在默认情况下 这两个壁纸是相同的  如果需要默认的锁屏壁纸和桌面壁纸不一样 需要额外修改 Android13实现 替换默认桌面壁纸: 将图片文件替换frameworks/base/core/res/res/drawable-nodpi/default_wallpaper.*  (注意不能是bmp格式) 替换默认锁屏壁纸: 将图片资源放入vendo

在JS中的设计模式的单例模式、策略模式、代理模式、原型模式浅讲

1. 单例模式(Singleton Pattern) 确保一个类只有一个实例,并提供一个全局访问点。 示例代码: class Singleton {constructor() {if (Singleton.instance) {return Singleton.instance;}Singleton.instance = this;this.data = [];}addData(value)

C#实战|大乐透选号器[6]:实现实时显示已选择的红蓝球数量

哈喽,你好啊,我是雷工。 关于大乐透选号器在前面已经记录了5篇笔记,这是第6篇; 接下来实现实时显示当前选中红球数量,蓝球数量; 以下为练习笔记。 01 效果演示 当选择和取消选择红球或蓝球时,在对应的位置显示实时已选择的红球、蓝球的数量; 02 标签名称 分别设置Label标签名称为:lblRedCount、lblBlueCount

科研绘图系列:R语言扩展物种堆积图(Extended Stacked Barplot)

介绍 R语言的扩展物种堆积图是一种数据可视化工具,它不仅展示了物种的堆积结果,还整合了不同样本分组之间的差异性分析结果。这种图形表示方法能够直观地比较不同物种在各个分组中的显著性差异,为研究者提供了一种有效的数据解读方式。 加载R包 knitr::opts_chunk$set(warning = F, message = F)library(tidyverse)library(phyl

透彻!驯服大型语言模型(LLMs)的五种方法,及具体方法选择思路

引言 随着时间的发展,大型语言模型不再停留在演示阶段而是逐步面向生产系统的应用,随着人们期望的不断增加,目标也发生了巨大的变化。在短短的几个月的时间里,人们对大模型的认识已经从对其zero-shot能力感到惊讶,转变为考虑改进模型质量、提高模型可用性。 「大语言模型(LLMs)其实就是利用高容量的模型架构(例如Transformer)对海量的、多种多样的数据分布进行建模得到,它包含了大量的先验

Kubernetes PodSecurityPolicy:PSP能实现的5种主要安全策略

Kubernetes PodSecurityPolicy:PSP能实现的5种主要安全策略 1. 特权模式限制2. 宿主机资源隔离3. 用户和组管理4. 权限提升控制5. SELinux配置 💖The Begin💖点点关注,收藏不迷路💖 Kubernetes的PodSecurityPolicy(PSP)是一个关键的安全特性,它在Pod创建之前实施安全策略,确保P