factory.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. //工厂方法,类工厂以及抽象工厂(和构造器模式有一些重叠)
  2. //工厂的核心是对类的构建
  3. #include<memory>
  4. #include<iostream>
  5. #include<map>
  6. //从基本的类开始
  7. //给一个类设计一个工厂方法
  8. //工厂方法的好处是可以直接强制对外要求生成的类的封装格式(如智能指针,json数据流等)
  9. class basic_factory
  10. {
  11. private:
  12. int data;
  13. protected:
  14. basic_factory(){}
  15. public:
  16. static std::shared_ptr<basic_factory> create() //一个简单的工厂方法,只能通过工厂方法构建(这样可以强制要求只能获取智能指针中的对象)
  17. {
  18. return std::shared_ptr<basic_factory>(new basic_factory);
  19. }
  20. };
  21. //类工厂(简单版本)
  22. //将所有类的的构造函数全部设置为protected,将工厂类设置为对应类的友元(即一种侵入式的构建方式,必须在设计的时候就做好,否则很难修改)
  23. class factory;
  24. class A
  25. {
  26. int data;
  27. friend factory;
  28. protected:
  29. A(){}
  30. };
  31. class B
  32. {
  33. int data;
  34. friend factory;
  35. protected:
  36. B(){}
  37. };
  38. //当然由于只有工厂能创建对象(产品)那么实际上工厂还可以负责售后(因为工厂有所有的类的信息),即保存每一个数据指针,最后一次性释放空间
  39. class factory
  40. {
  41. public:
  42. static std::shared_ptr<A> create_A()
  43. {
  44. return std::shared_ptr<A>(new A);
  45. }
  46. static std::shared_ptr<B> create_B()
  47. {
  48. return std::shared_ptr<B>(new B);
  49. }
  50. };
  51. //工厂方法与多态
  52. class base
  53. {
  54. public:
  55. virtual void print()
  56. {
  57. std::cout<<"basic"<<std::endl;
  58. }
  59. };
  60. class base_A:public base
  61. {
  62. public:
  63. void print()override
  64. {
  65. std::cout<<"A"<<std::endl;
  66. }
  67. };
  68. class base_B:public base
  69. {
  70. public:
  71. void print()override
  72. {
  73. std::cout<<"B"<<std::endl;
  74. }
  75. };
  76. class factory_V
  77. {
  78. public:
  79. static std::shared_ptr<base> create(int type)
  80. {
  81. switch(type)
  82. {
  83. case 1:
  84. {
  85. return std::shared_ptr<base>(new base);
  86. break;
  87. }
  88. case 2:
  89. {
  90. return std::shared_ptr<base>(new base_A);
  91. break;
  92. }
  93. case 3:
  94. {
  95. return std::shared_ptr<base>(new base_B);
  96. break;
  97. }
  98. default:
  99. return std::shared_ptr<base>(new base);
  100. }
  101. }
  102. };
  103. //嵌套工厂(在类内定义工厂)直接将类和工厂绑定
  104. class insert
  105. {
  106. int data;
  107. class factory
  108. {
  109. protected:
  110. factory()=default;
  111. public:
  112. std::shared_ptr<insert> create()
  113. {
  114. return std::move(std::shared_ptr<insert>(new insert));
  115. }
  116. };
  117. public:
  118. static factory lab;
  119. };
  120. //抽象工厂
  121. //抽象工厂设计模式是一种高度复杂的设计模式
  122. //抽象工厂的名称来源是当有大量相似的物体需要不同的工厂,会设计一个抽象类(抽象工厂作为接口),其余的工厂从这个抽象工厂中继承
  123. //当然最后为了不使用这么大量的工厂,还会在使用一个工厂对所有的构造工厂进行封装
  124. //总感觉带点过度设计的意思
  125. //有两种饮料,要分别准备
  126. class prepare_coffee;
  127. class prepare_water;
  128. struct drink
  129. {
  130. };
  131. struct coffee:public drink
  132. {
  133. int c;
  134. private:
  135. coffee()=default;
  136. friend prepare_coffee;
  137. };
  138. struct water:public drink
  139. {
  140. int c;
  141. private:
  142. water()=default;
  143. friend prepare_water;
  144. };
  145. class abstract //抽象工厂
  146. {
  147. public:
  148. virtual std::shared_ptr<drink> prepare()=0;
  149. };
  150. class prepare_water:public abstract
  151. {
  152. public:
  153. std::shared_ptr<drink> prepare()override
  154. {
  155. return std::move(std::shared_ptr<drink>(new water));
  156. }
  157. };
  158. class prepare_coffee:public abstract
  159. {
  160. public:
  161. std::shared_ptr<drink> prepare()override
  162. {
  163. return std::move(std::shared_ptr<drink>(new coffee));
  164. }
  165. };
  166. //当然如果用工厂群太抽象了
  167. class prepare_drink
  168. {
  169. private:
  170. std::map<std::string,std::shared_ptr<abstract>> prepare_drinks;
  171. public:
  172. prepare_drink()
  173. {
  174. prepare_drinks["coffee"]=std::make_shared<prepare_coffee>();
  175. prepare_drinks["water"]=std::make_shared<prepare_water>();
  176. }
  177. std::shared_ptr<drink> prepare(const std::string& name)
  178. {
  179. std::shared_ptr<drink> result=prepare_drinks[name]->prepare();
  180. return std::move(result);
  181. }
  182. };
  183. int main()
  184. {
  185. basic_factory::create();
  186. factory_V::create(1)->print();
  187. factory_V::create(2)->print();
  188. factory_V::create(3)->print();
  189. insert::lab.create();
  190. }