简单工厂模式
一、第一种方法
//实现了客户端调用和implOne,implTwo的解耦合
//factory类实现了变化隔离
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<string> 2 #include "DynOBJ.h" 3 using namespace std; 4 5 class Api { 6 public: 7 virtual void test(string s) = 0; 8 protected: 9 Api(){} 10 }; 11 12 13 class ImpleOne :public Api { 14 public: 15 void test(string s) { 16 cout << "现在是One在执行" << s; 17 } 18 }; 19 20 class ImpleTwo :public Api { 21 public: 22 void test(string s) { 23 cout << "现在是Two在执行" << s; 24 } 25 }; 26 27 class Factory { 28 public: 29 static Api* createApi(int type) { 30 31 Api* pApi = nullptr; 32 if (type == 1) { 33 pApi = new ImpleOne(); 34 } 35 if (type == 2) { 36 pApi = new ImpleTwo(); 37 } 38 return pApi; 39 } 40 41 static Api* createApi() { 42 return new ImpleOne(); 43 } 44 }; 45 46 47 /** 48 传入参数1,可以实现从数据库读入的功能 49 传入参数2,可以实现从文本文件读入的功能 50 */ 51 int main(void) { 52 Api* pApi = Factory::createApi(2);//客户端,还是知道工厂的细节 53 54 pApi->test("--现在是使用简单工厂方法重构\r\n"); 55 Api* pApiEx = Factory::createApi(); 56 system("pause"); 57 return 0; 58 } 59 //实现了客户端调用和implOne,implTwo的解耦合 60 //factory类实现了变化隔离
二、一个自动化的方法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #ifndef OBJDYN_H_ 2 #define OBJDYN_H_ 3 #include <string> 4 #include <map> 5 typedef void* (*Constructor)(); 6 7 class CObjectFactory { 8 public: 9 static void registerClass(std::string className, Constructor constructor) { 10 constructors()[className] = constructor; 11 } 12 13 static void* creatObject(const std::string& className) { 14 Constructor constructor = NULL; 15 if (constructors().find(className) != constructors().end()) 16 constructor = constructors().find(className)->second; 17 18 if (constructor == NULL) 19 return NULL; 20 return (*constructor)(); 21 } 22 private: 23 //string->key:动态创建的类的类名,value是构建 24 inline static std::map<std::string, Constructor>& constructors() { 25 static std::map<std::string, Constructor> instance; 26 return instance; 27 } 28 }; 29 //##表示连接 30 //#表示字符串 31 #define REG_CLASS(class_name) \ 32 class class_name##Helper { \ 33 public: \ 34 class_name##Helper() { \ 35 CObjectFactory::registerClass(#class_name, class_name##Helper::createObjFunc); \ 36 }\ 37 static void* createObjFunc() { \ 38 return new ImpleTwo;\ 39 }\ 40 };\ 41 class_name##Helper class_name##helper; 42 #endif // !OBJDYN_H_
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include<string> 3 #include "DynOBJ.h" 4 using namespace std; 5 6 class Api { 7 public: 8 virtual void test(string s) = 0; 9 protected: 10 Api(){} 11 }; 12 13 14 class ImpleOne :public Api { 15 public: 16 void test(string s) { 17 cout << "现在是One在执行" << s; 18 } 19 }; 20 21 class ImpleTwo :public Api { 22 public: 23 void test(string s) { 24 cout << "现在是Two在执行" << s; 25 } 26 }; 27 28 29 //"ImpleTwo"从文件里读写,就可以实现自动化了。 30 REG_CLASS(ImpleTwo) 31 class AutoFactory{ 32 public: 33 static Api* createApi() { 34 Api* pApi = nullptr; 35 pApi = static_cast<Api*>(CObjectFactory::creatObject("ImpleTwo")); 36 return pApi; 37 } 38 }; 39 40 int main() { 41 Api* pApi = AutoFactory::createApi(); 42 pApi->test("哈哈完全不知道,里面的东西了"); 43 system("pause"); 44 return 0; 45 }
三、视频截图