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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/747062.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

城乡居民基本医疗信息管理系统|基于Springboot的城乡居民基本医疗信息管理系统设计与实现(源码+数据库+文档)

城乡居民基本医疗信息管理系统目录 目录 基于Springboot的城乡居民基本医疗信息管理系统设计与实现 一、前言 二、系统设计 三、系统功能设计 1、病例管理 2、医院资讯信息管理 3、医院资讯类型管理 四、数据库设计 五、核心代码 六、论文参考 七、最新计算机毕设选…

微信小程序开发学习笔记《21》uni-app框架-楼层图片跳转

微信小程序开发学习笔记《21》uni-app框架-楼层图片跳转 博主正在学习微信小程序开发&#xff0c;希望记录自己学习过程同时与广大网友共同学习讨论。建议仔细阅读uni-app对应官方文档 一、创建新的分包goods_list 二、将请求到的楼层数据url调整为本地的 可以看到上图是请求…

关于固件的简单解释

我不喜欢等人也不喜欢被别人等——赤砂之蝎 简而言之 固件是什么 固件&#xff08;Firmware&#xff09;是一种软件类型&#xff0c;它是嵌入式系统中的一部分&#xff0c;通常存储在设备的非易失性存储器中&#xff0c;如闪存或ROM&#xff08;只读存储器&#xff09;。与操作…

蓝桥杯---棋盘(典型的二维差分问题)

题目链接&#xff1a;棋盘 这道题真的是非常典型的二维差分问题了&#xff08;在我个人看来&#xff09;&#xff0c;题目中的0和1&#xff0c;我们直接让差分数组&#xff0c;偶数就是0&#xff0c;奇数就是1.初始化是0&#xff0c;是白子&#xff08;偶数&#xff09;&#x…

libevent中bufferevent事件及常用的API函数

自带buffer的事件-bufferevent bufferevent实际上也是一个event&#xff0c;只不过比普通的event高级&#xff0c;他的内部有两个缓冲区&#xff0c;以及一个文件描述符&#xff08;网络套接字&#xff09;。一个网络套接字有读写两个缓冲区&#xff0c;bufferevent同样也带有…

探索仿函数(Functor):C++中的灵活函数对象

文章目录 一、仿函数定义及使用二、仿函数与函数指针的区别三、仿函数与算法的关系四、仿函数的实践用例 在C编程中&#xff0c;我们经常需要对数据进行排序、筛选或者其他操作。为了实现这些功能&#xff0c;C标准库提供了许多通用的算法和容器&#xff0c;而其中一个重要的概…

思科防火墙如何配置静态NAT

环境&#xff1a; 思科防火墙ASA5555 Cisco Adaptive Security Appliance Software Version 9.4(2)6 Device Manager Version 7.5(2)153 问题描述&#xff1a; 思科防火墙如何配置静态NAT 解决方案&#xff1a; 1.做之前要先查一下有没有端口被占用&#xff0c;要和业务确…

nut-ui组件库icon中使用阿里图标

1.需求 基本每个移动端组件库都有组件 icon组件 图标组件、 但是很多组件库中并找不到我们需要的图标 这时候 大家有可能会找图标库 最大众的就是iconfont的图标了 2.使用 有很多方式去使用这个东西 比如将再限链接中的css引入 在使用 直接下载图标 symbol 方式 等....…

【NR 定位】3GPP NR Positioning 5G定位标准解读(十三)-DL-AoD定位

前言 3GPP NR Positioning 5G定位标准&#xff1a;3GPP TS 38.305 V18 3GPP 标准网址&#xff1a;Directory Listing /ftp/ 【NR 定位】3GPP NR Positioning 5G定位标准解读&#xff08;一&#xff09;-CSDN博客 【NR 定位】3GPP NR Positioning 5G定位标准解读&#xff08;…

buuctf warmup 超详细

目录 1.代码审计&#xff1a; 2.逻辑分析 3.总结分析 4.分析记录 5.疑点解答 1.代码审计&#xff1a; <?phphighlight_file(__FILE__);class emmm //定义了一个类{public static function checkFile(&$page) 类里面又申明创建…

移动通信网络AT指令

PLMN 移动通信网络PLMN = MCC + MNC,PLMN由MCC移动国家码和MNC移动网络码组成,例如:中国移动GSM的PLMN为:46000(MCC:460, MNC:00)中国联通GSM的PLMN国家码MCC为460,网络码MNC为01: 46001中国大陆相关的移动网络码:中国移动系统使用00、02、04、07,中国联通GSM系统…

题目 2656: 蓝桥杯2022年第十三届省赛真题-刷题统计

时间限制: 3s 内存限制: 320MB 提交: 31968 解决: 5255 题目描述 小明决定从下周一开始努力刷题准备蓝桥杯竞赛。他计划周一至周五每天做 a 道题目&#xff0c;周六和周日每天做 b 道题目。请你帮小明计算&#xff0c;按照计划他将在第几天实现做题数大于等于 n 题&#xff1…

3、Redis持久化之RDB

Redis持久化之RDB 何为持久化&#xff1f;所谓持久化就是将数据持久化保存&#xff0c;也就是将数据保存到硬盘中。 Redis持久化的方法&#xff1a; RDB AOF AOF在下一篇介绍 什么是RDB RDB是redis默认的持久化策略&#xff0c;在RDB模式下&#xff0c;可以将redis在内存…

Android 架构师研发技术进阶之路:不同阶段需要掌握的那些技术及软技能

资深 而到了资深层次&#xff0c;技术栈已经不再是阻碍。能够从更高层面看待问题&#xff0c;理解整个系统的设计&#xff0c;作为系统架构师的角色存在。 1. 理解微服务、SOA思想&#xff0c;对于后端开发有一定涉猎。 2. 了解前端研发工具和思想&#xff0c;知道vue react…

centos破解root密码以及如何防止他人破解root密码

目录 破解root密码 服务器重启 1.再重启页面上下选择第一个按e进入内核编辑模式 2.找到linux16开头的一行&#xff0c;光标移动到最后添加 init/bin/sh Ctrlx 保存 3.进入单用户模式 4.重新挂在根分区 5.关闭selinux 6.更新密码 passwd 7.在根分区下面创建一个隐藏文件…

【C语言步行梯】一维数组、二维数组介绍与应用详谈

&#x1f3af;每日努力一点点&#xff0c;技术进步看得见 &#x1f3e0;专栏介绍&#xff1a;【C语言步行梯】专栏用于介绍C语言相关内容&#xff0c;每篇文章将通过图片代码片段网络相关题目的方式编写&#xff0c;欢迎订阅~~ 文章目录 为什么要有数组&#xff1f;一维数组数组…

uni-app微信小程序上拉加载,下拉刷新

pages.json配置官网链接 onPullDownRefresh、onReachBottom函数跟生命周期同级 data() {return {orderList:[],total: null, //总共多少条数据page: 1,pageSize: 10,} }, onLoad() {}, mounted(){this.getInfo() }, methods:{getInfo(){API.getListxxx().then(res > {const…

探索TikTok云手机在社交媒体营销的作用

近年来&#xff0c;TikTok作为全球短视频平台之一&#xff0c;其用户基数呈现持续增长的趋势。伴随社交媒体的蓬勃发展&#xff0c;企业和个人纷纷涌入TikTok平台&#xff0c;追求更广泛的曝光和用户互动。为满足这一需求&#xff0c;TikTok云手机应运而生。本文将深度剖析TikT…

蓝桥杯[OJ 1621]挑选子串-CPP-双指针

目录 一、题目描述&#xff1a; 二、整体思路&#xff1a; 三、代码&#xff1a; 一、题目描述&#xff1a; 二、整体思路&#xff1a; 要找子串&#xff0c;则必须找头找尾&#xff0c;找头可以遍历连续字串&#xff0c;找尾则是要从头的基础上往后遍历&#xff0c;可以设头…

【JS逆向学习】猿人学第六题 js混淆 回溯

逆向目标 网址&#xff1a;https://match.yuanrenxue.cn/match/6接口&#xff1a;https://match.yuanrenxue.cn/api/match/6参数&#xff1a;payload(m、q) 逆向过程 老规矩&#xff0c;先来分析网络请求&#xff0c;加密的地方一目了然&#xff0c;没什么可多说的&#xff…