首页 > 其他分享 >设计模式模板-抽象工厂

设计模式模板-抽象工厂

时间:2023-07-14 09:45:06浏览次数:46  
标签:std object TemplateAbstractFactory factory 抽象 CObjectList sm 设计模式 模板

 1 #ifndef TEMPLATE_ABSTRACT_FACTORY_H
 2 #define TEMPLATE_ABSTRACT_FACTORY_H
 3 
 4 #include <algorithm>
 5 #include <list>
 6 #include <mutex>
 7 
 8 template <typename Abstract, typename ...Args>
 9 class TemplateAbstractFactory
10 {
11 public:
12     template <typename Factory, typename Callable, typename ...ParamArgs>
13     static Abstract *CreateObject(Factory *factoryObject, Callable &&callable, Args &...args)
14     {
15         std::lock_guard<std::mutex> unique(sm_CMutex);
16 
17         Abstract *object = std::bind(callable, factoryObject, args...)(args...);
18         if (std::find(sm_CObjectList.begin(), sm_CObjectList.end(), object) == sm_CObjectList.end())
19         {
20             sm_CObjectList.push_back(object);
21         }
22         else
23         {
24             delete object;
25             object = nullptr;
26         }
27 
28         return object;
29     }
30 
31     static void DeleteObject(Abstract *object)
32     {
33         std::lock_guard<std::mutex> unique(sm_CMutex);
34 
35         if (std::find(sm_CObjectList.begin(), sm_CObjectList.end(), object) != sm_CObjectList.end())
36         {
37             delete object;
38             sm_CObjectList.remove(object);
39         }
40     }
41 
42 
43 private:
44     static std::list<Abstract *> sm_CObjectList;
45     static std::mutex sm_CMutex;
46 };
47 
48 
49 template <typename Abstract, typename ...Args>
50 std::list<Abstract *> TemplateAbstractFactory<Abstract, Args...>::sm_CObjectList;
51 
52 template <typename Abstract, typename ...Args>
53 std::mutex TemplateAbstractFactory<Abstract, Args...>::sm_CMutex;
54 
55 #endif // TEMPLATE_ABSTRACT_FACTORY_H
  1 #include "template_abstract_factory.h"
  2 #include <iostream>
  3 
  4 class AppleBase
  5 {
  6 public:
  7     virtual ~AppleBase(){   std::cout << "苹果已销毁" << std::endl;  }
  8     virtual void info() = 0;
  9 };
 10 
 11 class ChinaApple : public AppleBase
 12 {
 13 public:
 14     void info(){    std::cout << "中国苹果" << std::endl;    }
 15 };
 16 
 17 
 18 class RussiaApple : public AppleBase
 19 {
 20 public:
 21     void info(){    std::cout << "俄国苹果" << std::endl;   }
 22 };
 23 
 24 class MangoBase
 25 {
 26 public:
 27     virtual ~MangoBase(){   std::cout << "芒果已销毁" << std::endl;  }
 28     virtual void info() = 0;
 29 };
 30 
 31 class ChinaMango : public MangoBase
 32 {
 33 public:
 34     void info(){    std::cout << "中国芒果" << std::endl;    }
 35 };
 36 
 37 class RussiaMango : public MangoBase
 38 {
 39 public:
 40     void info(){    std::cout << "俄国芒果" << std::endl;   }
 41 };
 42 
 43 class FactoryAbstract
 44 {
 45 public:
 46     virtual ~FactoryAbstract(){}
 47     virtual AppleBase *CreateApple() = 0;
 48     virtual MangoBase *CreateMango() = 0;
 49 };
 50 
 51 class ChinaFactory : public FactoryAbstract
 52 {
 53 public:
 54     AppleBase *CreateApple(){   return new ChinaApple();    }
 55     MangoBase *CreateMango(){   return new ChinaMango();    }
 56 };
 57 
 58 class RussiaFactory : public FactoryAbstract
 59 {
 60 public:
 61     AppleBase *CreateApple(){   return new RussiaApple();    }
 62     MangoBase *CreateMango(){   return new RussiaMango();    }
 63 };
 64 
 65 class JapanFactory : public FactoryAbstract
 66 {
 67 public:
 68     AppleBase *CreateApple(){   return new ChinaApple();    }
 69     MangoBase *CreateMango(){   return new RussiaMango();   }
 70 };
 71 
 72 
 73 void testAbstractFactory()
 74 {
 75     {
 76         std::cout << "需求变更前 - 使用中国工厂(生产本地的水果)" << std::endl;
 77         FactoryAbstract *factory = new ChinaFactory();
 78 
 79         AppleBase *apple = TemplateAbstractFactory<AppleBase>::CreateObject<FactoryAbstract, AppleBase* (FactoryAbstract::*)()>(factory, &FactoryAbstract::CreateApple);
 80         MangoBase *mango = TemplateAbstractFactory<MangoBase>::CreateObject<FactoryAbstract, MangoBase* (FactoryAbstract::*)()>(factory, &FactoryAbstract::CreateMango);
 81 
 82         apple->info();
 83         mango->info();
 84 
 85         TemplateAbstractFactory<AppleBase>::DeleteObject(apple);
 86         TemplateAbstractFactory<MangoBase>::DeleteObject(mango);
 87 
 88         delete factory;
 89         std::cout << std::endl;
 90     }
 91 
 92     {
 93         std::cout << "需求变更后 - 使用俄国工厂(生产本地的水果)" <<std::endl;
 94         FactoryAbstract *factory = new RussiaFactory();
 95 
 96         AppleBase *apple = TemplateAbstractFactory<AppleBase>::CreateObject<FactoryAbstract, AppleBase* (FactoryAbstract::*)()>(factory, &FactoryAbstract::CreateApple);
 97         MangoBase *mango = TemplateAbstractFactory<MangoBase>::CreateObject<FactoryAbstract, MangoBase* (FactoryAbstract::*)()>(factory, &FactoryAbstract::CreateMango);
 98 
 99         apple->info();
100         mango->info();
101 
102         TemplateAbstractFactory<AppleBase>::DeleteObject(apple);
103         TemplateAbstractFactory<MangoBase>::DeleteObject(mango);
104 
105         delete factory;
106         std::cout << std::endl;
107     }
108 
109     {
110         std::cout << "需求再变更 - 使用日本代工(奸诈的小日子分别采购便宜的中国苹果和俄国芒果,赚取差价牟利)" <<std::endl;
111         FactoryAbstract *factory = new JapanFactory();
112 
113         AppleBase *apple = TemplateAbstractFactory<AppleBase>::CreateObject<FactoryAbstract, AppleBase* (FactoryAbstract::*)()>(factory, &FactoryAbstract::CreateApple);
114         MangoBase *mango = TemplateAbstractFactory<MangoBase>::CreateObject<FactoryAbstract, MangoBase* (FactoryAbstract::*)()>(factory, &FactoryAbstract::CreateMango);
115 
116         apple->info();
117         mango->info();
118 
119         TemplateAbstractFactory<AppleBase>::DeleteObject(apple);
120         TemplateAbstractFactory<MangoBase>::DeleteObject(mango);
121 
122         delete factory;
123         std::cout << std::endl;
124     }
125 }

 

标签:std,object,TemplateAbstractFactory,factory,抽象,CObjectList,sm,设计模式,模板
From: https://www.cnblogs.com/anqingshu/p/17552826.html

相关文章

  • 抽象代数 1
    基本概念和引理代数系统:在一个集合上$S$定义一个二元运算$\times$,如果二元运算满足封闭性,则称$(S,\times)$为一个代数系统。半群:如果一个代数系统二元运算满足结合律,那么这个代数系统称为半群。幺半群:如果一个半群里面有幺元(单位元),那么这个群为幺半群。群:如果一个幺半群......
  • Vscode 设置别名路径和创建快捷模板
    设置别名路径创建jsconfig.json文件,配置@文件路径{"compilerOptions":{"baseUrl":"./","paths":{"@/*":["src/*"]}}} 创建快捷模板 文件->首选项->配置用户代码片段 新建全局代码片段文件......
  • Django 模板语言获取列表(可迭代对象)的下标、索引。从而实现显示序号(转载)
    ......
  • 设计模式-建造者模式在Java中使用示例
    场景建造者模式复杂对象的组装与创建没有人买车会只买一个轮胎或者方向盘,大家买的都是一辆包含轮胎、方向盘和发动机等多个部件的完整汽车。如何将这些部件组装成一辆完整的汽车并返回给用户,这是建造者模式需要解决的问题。建造者模式又称为生成器模式,它是一种较为复杂、使用......
  • ALV简单模板
    ALV简单模板根据结构(表)名创建LT_ALV_CAT,后续更改显示字段,直接改结构(表)就可以了。ZPPR0102*&---------------------------------------------------------------------**&ReportZPPR0102*&*&---------------------------------------------------------------------**&......
  • 各类模板
    高精加luoguP1601A+BProblem(高精)高精减luoguP2142高精度减法高精乘luoguP1303A*BProblem高精除求商luoguP2005A/BProblemII,luoguP1480A/BProblem高精除求余数luoguP2818天使的起誓高精阶乘SP24FCTRL2-Smallfactorials栈luoguB3614【模板】栈队列lu......
  • 树链剖分模板
    区间,边权描述松鼠爸爸为了让松鼠宝宝更熟悉地熟悉采松果的流程,为其定制了一颗“树”,树上有n个点,n-1条边(无环),每条边上都有一定数量的松果。松鼠爸爸为了让松鼠宝宝得到更多的松果,有m次操作,每次操作给定两个点x,y和一个add,在x点到y点的简单路径上所有......
  • 现代C++(Modern C++)基本用法实践:四、模板
    概述C++的模板是泛型编程思想的一种实现。C++是强类型语言,处处强调类型。同样的加法运算,int和float的加法运算需定义两个函数(重载),而使用模板则可以只用一个函数(见下面示例)。这类似我们面向对象所说的多态(定义加法运算,各个类型有不同的实现),所以是所谓静多态的一种实现方式,不同的......
  • JAVA设计模式之策略模式
    设计模式设计模式(DesignPattern)是前辈们对代码开发经验的总结,是解决特定问题的一系列套路。它不是语法规定,而是一套用来提高代码可复用性、可维护性、可读性、稳健性以及安全性的解决方案。总体来说设计模式分为三大类:创建型模式,共五种:工厂方法模式、抽象工厂模式、单例模式、......
  • C# 使用Windows服务项目模板快速创建Windows服务程序
    之前写了一篇使用Topshelf创建Windows服务程序的文章:https://www.cnblogs.com/log9527blog/p/17325795.html还可以直接使用VS自带的Windows服务项目模板快速创建Windows服务程序 Service1.cs里面的OnStart和OnStop方法分别代表服务开始,服务停止时执行的逻辑 配置服务Serv......