Factory
1、定义创建对象的接口,封装对象的创建
2、将实际创建工作延迟到子类中,例如,类A中药使用类B,B是抽象父类,但是在类A中不知道具体要实例化哪一个B的子类,但是在类A的子类D中是可以知道的。在A中无法使用 new B***()方法
3、将创建工作延迟到子类中后,核心工厂类不再负责产品的创建,这样核心类成为一个抽象工厂,只提供工厂子类必须实现的接口,这样的好处是可以不用修改已有的工厂类的情况下增加新的产品(每一种产品,都分别对应相应的工厂子类负责其创建工作)

C++设计模式-Factory工厂模式  C++设计模式-Factory工厂模式

使用场景:用于一类类(所创建的产品继承共同的产品基类)的创建

实现方式1:所谓的工厂方法模式,对每一个子类产品都分别对应一个工厂子类,用来创建相应的产品,这样若增加了新的产品,只需相应增加工厂子类即可

优点:不用修改已有代码,开放封闭原则:对扩展开放,对更改封闭

代码如下:

  1 //IHuman.h
  2 
  3 #pragma once
  4 class IHuman
  5 {
  6 public:
  7     IHuman(void)
  8     {
  9     }
 10     virtual ~IHuman(void)
 11     {
 12     }
 13     virtual void Laugh() = 0;
 14     virtual void Cry() = 0;
 15     virtual void Talk() = 0;
 16 };
 17 
 18 //YellowHuman.h
 19 
 20 #pragma once
 21 #include "ihuman.h"
 22 class CYellowHuman :
 23     public IHuman
 24 {
 25 public:
 26     CYellowHuman(void);
 27     ~CYellowHuman(void);
 28     void Laugh();
 29     void Cry();
 30     void Talk();
 31 };
 32 
 33 //YellowHuman.cpp
 34 
 35 #include "StdAfx.h"
 36 #include "YellowHuman.h"
 37 #include <iostream>
 38 using std::cout;
 39 using std::endl;
 40 CYellowHuman::CYellowHuman(void)
 41 {
 42 }
 43 CYellowHuman::~CYellowHuman(void)
 44 {
 45 }
 46 void CYellowHuman::Cry()
 47 {
 48     cout << "黄色人种会哭" << endl;
 49 }
 50 void CYellowHuman::Laugh()
 51 {
 52     cout << "黄色人种会大笑,幸福呀!" << endl;
 53 }
 54 void CYellowHuman::Talk()
 55 {
 56     cout << "黄色人种会说话,一般说的都是双字节" << endl;
 57 }
 58 
 59 //WhiteHuman.h
 60 
 61 #pragma once
 62 #include "ihuman.h"
 63 class CWhiteHuman :
 64     public IHuman
 65 {
 66 public:
 67     CWhiteHuman(void);
 68     ~CWhiteHuman(void);
 69     void Laugh();
 70     void Cry();
 71     void Talk();
 72 };
 73 
 74 //WhiteHuman.cpp
 75 
 76 #include "StdAfx.h"
 77 #include "WhiteHuman.h"
 78 #include <iostream>
 79 using std::cout;
 80 using std::endl;
 81 CWhiteHuman::CWhiteHuman(void)
 82 {
 83 }
 84 CWhiteHuman::~CWhiteHuman(void)
 85 {
 86 }
 87 void CWhiteHuman::Cry()
 88 {
 89     cout << "白色人种会哭" << endl;
 90 }
 91 void CWhiteHuman::Laugh()
 92 {
 93     cout << "白色人种会大笑,侵略的笑声" << endl;
 94 }
 95 void CWhiteHuman::Talk()
 96 {
 97     cout << "白色人种会说话,一般都是单字节" << endl;
 98 }
 99 
100 //BlackHuman.h
101 
102 #pragma once
103 #include "ihuman.h"
104 class CBlackHuman :
105     public IHuman
106 {
107 public:
108     CBlackHuman(void);
109     ~CBlackHuman(void);
110     void Laugh();
111     void Cry();
112     void Talk();
113 };
114 
115 //BlackHuman.cpp
116 
117 #include "StdAfx.h"
118 #include "BlackHuman.h"
119 #include <iostream>
120 using std::cout;
121 using std::endl;
122 CBlackHuman::CBlackHuman(void)
123 {
124 }
125 CBlackHuman::~CBlackHuman(void)
126 {
127 }
128 void CBlackHuman::Cry()
129 {
130     cout << "黑人会哭" << endl;
131 }
132 void CBlackHuman::Laugh()
133 {
134     cout << "黑人会笑" << endl;
135 }
136 void CBlackHuman::Talk()
137 {
138     cout << "黑人可以说话,一般人听不懂" << endl;
139 }
140 
141 //IHumanFactory.h
142 
143 #pragma once
144 #include "IHuman.h"
145 class IHumanFactory
146 {
147 public:
148     IHumanFactory(void)
149     {
150     }
151     virtual ~IHumanFactory(void)
152     {
153     }
154     virtual IHuman * CreateHuman() = 0;
155 };
156 //YellowHuman.h
157 
158 #pragma once
159 #include "ihumanfactory.h"
160 class CYellowHumanFactory :
161     public IHumanFactory
162 {
163 public:
164     CYellowHumanFactory(void);
165     ~CYellowHumanFactory(void);
166     virtual IHuman * CreateHuman(void);
167 };
168 
169 //YellowHumanFactory.cpp
170 
171 #include "StdAfx.h"
172 #include "YellowHumanFactory.h"
173 #include "YellowHuman.h"
174 CYellowHumanFactory::CYellowHumanFactory(void)
175 {
176 }
177 CYellowHumanFactory::~CYellowHumanFactory(void)
178 {
179 }
180 IHuman * CYellowHumanFactory::CreateHuman( void )
181 {
182     return new CYellowHuman();
183 }
184 //WhiteHuman.h
185 
186 #pragma once
187 #include "ihumanfactory.h"
188 class CWhiteHumanFactory :
189     public IHumanFactory
190 {
191 public:
192     CWhiteHumanFactory(void);
193     ~CWhiteHumanFactory(void);
194     virtual IHuman * CreateHuman(void);
195 };
196 
197 //WhiteHumanFactory.cpp
198 
199 #include "StdAfx.h"
200 #include "WhiteHumanFactory.h"
201 #include "WhiteHuman.h"
202 CWhiteHumanFactory::CWhiteHumanFactory(void)
203 {
204 }
205 CWhiteHumanFactory::~CWhiteHumanFactory(void)
206 {
207 }
208 IHuman * CWhiteHumanFactory::CreateHuman( void )
209 {
210     return new CWhiteHuman();
211 }
212 //BlackHuman.h
213 
214 #pragma once
215 #include "ihumanfactory.h"
216 class CBlackHumanFactory :
217     public IHumanFactory
218 {
219 public:
220     CBlackHumanFactory(void);
221     ~CBlackHumanFactory(void);
222     virtual IHuman * CreateHuman();
223 };
224 //BlackHumanFactory.cpp
225 
226 #include "StdAfx.h"
227 #include "BlackHumanFactory.h"
228 #include "BlackHuman.h"
229 CBlackHumanFactory::CBlackHumanFactory(void)
230 {
231 }
232 CBlackHumanFactory::~CBlackHumanFactory(void)
233 {
234 }
235 IHuman * CBlackHumanFactory::CreateHuman()
236 {
237     return new CBlackHuman();
238 }
239 
240 //FactoryMethod.cpp
241 
242 // FactoryMethod.cpp : 定义控制台应用程序的入口点。
243 //
244 #include "stdafx.h"
245 #include "IHuman.h"
246 #include "YellowHuman.h"
247 #include "WhiteHuman.h"
248 #include "BlackHuman.h"
249 #include "SimpleHumanFactory.h"
250 #include "StandardHumanFactory.h"
251 #include "IHumanFactory.h"
252 #include "YellowHumanFactory.h"
253 #include "WhiteHumanFactory.h"
254 #include "BlackHumanFactory.h"
255 #include <iostream>
256 using std::cout;
257 using std::endl;
258 using std::string;
259 void DoFactoryMethod1()
260 {
261     cout << "----------第一批人是这样的:黄种人工厂来生产黄种人" << endl;
262     IHumanFactory *pHumanFactory = new CYellowHumanFactory();
263     IHuman *pHuman = pHumanFactory->CreateHuman();
264     pHuman->Cry();
265     pHuman->Laugh();
266     pHuman->Talk();
267     delete pHuman;
268     delete pHumanFactory;
269 }
270 void DoFactoryMethod2()
271 {
272     cout << "----------第二批人是这样的:白种人工厂来生产白种人" << endl;
273     IHumanFactory *pHumanFactory = new CWhiteHumanFactory();
274     IHuman *pHuman = pHumanFactory->CreateHuman();
275     pHuman->Cry();
276     pHuman->Laugh();
277     pHuman->Talk();
278     delete pHuman;
279     delete pHumanFactory;
280 }
281 void DoFactoryMethod3()
282 {
283     cout << "----------第一批人是这样的:黑种人工厂来生产黑种人" << endl;
284     IHumanFactory *pHumanFactory = new CBlackHumanFactory();
285     IHuman *pHuman = pHumanFactory->CreateHuman();
286     pHuman->Cry();
287     pHuman->Laugh();
288     pHuman->Talk();
289     delete pHuman;
290     delete pHumanFactory;
291 }
292 int _tmain(int argc, _TCHAR* argv[])
293 {
294     //工厂方法
295     cout << "----------工厂方法:" << endl;
296     DoFactoryMethod1();
297     DoFactoryMethod2();
298     DoFactoryMethod3();
299     
300     _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF | _CRTDBG_ALLOC_MEM_DF);
301     _CrtDumpMemoryLeaks();
302     return 0;
303 }
View Code

相关文章:

  • 2021-10-27
  • 2021-05-25
  • 2021-08-23
  • 2022-12-23
  • 2021-10-29
  • 2021-06-29
  • 2021-11-03
  • 2021-12-30
猜你喜欢
  • 2022-03-06
  • 2021-08-27
  • 2021-05-26
  • 2022-12-23
  • 2021-11-20
相关资源
相似解决方案