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