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

  

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

实现方式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 }

实现方式2:所谓的简单工厂模式,通过参数传递来决定要创建哪一个具体产品。

若不需延迟实例化(将实例化放到子类中),则在Factory中增加对应的创建方法即可,如:Product* CreateConcreteProduct(int i);

若需要延迟实例化,则在抽象Factory与具体ConcreteFactory中增加相应方法,在ConcreteFactory中实现方法Product* CreateConcreteProduct(int i)

优点:无需新增产品工厂类ConcreteFactory

缺点:需要修改已有代码,存在风险

代码如下:

  1 //Product.h
  2 // Product.h: interface for the Product class.
  3 //
  4 //
  5
  6 #if !defined(AFX_PRODUCT_H__714E1D10_AFA3_473E_A16C_759490E60B92__INCLUDED_)
  7 #define AFX_PRODUCT_H__714E1D10_AFA3_473E_A16C_759490E60B92__INCLUDED_
  8
  9 #if _MSC_VER > 1000
 10 #pragma once
 11 #endif // _MSC_VER > 1000
 12
 13 class Product
 14 {
 15 public:
 16     Product();
 17     virtual ~Product();
 18
 19 };
 20
 21 class ConcreteProduct : public Product
 22 {
 23 public:
 24     ConcreteProduct();
 25     virtual ~ConcreteProduct();
 26
 27 };
 28
 29 class ConcreteProduct1 : public Product
 30 {
 31 public:
 32     ConcreteProduct1();
 33     virtual ~ConcreteProduct1();
 34
 35 };
 36
 37 #endif // !defined(AFX_PRODUCT_H__714E1D10_AFA3_473E_A16C_759490E60B92__INCLUDED_)
 38
 39
 40
 41 //Product.cpp
 42 // Product.cpp: implementation of the Product class.
 43 //
 44 //
 45
 46 #include "Product.h"
 47 #include <iostream>
 48
 49 using namespace std;
 50
 51 //
 52 // Construction/Destruction
 53 //
 54
 55 Product::Product()
 56 {
 57 }
 58
 59 Product::~Product()
 60 {
 61 }
 62
 63 ConcreteProduct::ConcreteProduct()
 64 {
 65     cout<<"ConcreteProduct..."<<endl;
 66 }
 67
 68 ConcreteProduct::~ConcreteProduct()
 69 {
 70 }
 71
 72 ConcreteProduct1::ConcreteProduct1()
 73 {
 74     cout<<"ConcreteProduct1..."<<endl;
 75 }
 76
 77 ConcreteProduct1::~ConcreteProduct1()
 78 {
 79 }
 80
 81
 82 //Factory.h
 83
 84 #if !defined(AFX_FACTORY_H__32B11FEA_57A7_484E_B289_AB0E12783D7D__INCLUDED_)
 85 #define AFX_FACTORY_H__32B11FEA_57A7_484E_B289_AB0E12783D7D__INCLUDED_
 86
 87 #if _MSC_VER > 1000
 88 #pragma once
 89 #endif // _MSC_VER > 1000
 90
 91 class Product;
 92 class Factory
 93 {
 94 public:
 95     virtual Product* CreateConcreteProduct(int i)=0;
 96     Factory();
 97     virtual ~Factory() = 0;
 98     virtual Product* CreateProduct() = 0;
 99     virtual Product* CreateProduct1() = 0;
100 };
101
102 class ConcreteFactory : public Factory
103 {
104 public:
105     ConcreteFactory();
106     virtual ~ConcreteFactory();
107     virtual Product* CreateProduct();
108     virtual Product* CreateProduct1();
109     virtual Product* CreateConcreteProduct(int i);
110 };
111
112 #endif // !defined(AFX_FACTORY_H__32B11FEA_57A7_484E_B289_AB0E12783D7D__INCLUDED_)
113
114
115 //Factory.cpp
116
117 // Factory.cpp: implementation of the Factory class.
118 //
119 //
120
121 #include "Factory.h"
122 #include "Product.h"
123 #include <iostream>
124
125 using namespace std;
126
127 //
128 // Construction/Destruction
129 //
130
131 Factory::Factory()
132 {
133 }
134
135 Factory::~Factory()
136 {
137 }
138
139 ConcreteFactory::ConcreteFactory()
140 {
141     cout<<"ConcreteFactory..."<<endl;
142 }
143
144 ConcreteFactory::~ConcreteFactory()
145 {
146 }
147
148 Product* ConcreteFactory::CreateProduct()
149 {
150     return new ConcreteProduct();
151 }
152
153 Product* ConcreteFactory::CreateProduct1()
154 {
155     return new ConcreteProduct1();
156 }
157
158
159 Product* ConcreteFactory::CreateConcreteProduct(int i)
160 {
161     Product* pProduct = NULL;
162     switch(i)
163     {
164     case 0:
165         pProduct = new ConcreteProduct();
166         break;
167     case 1:
168         pProduct = new ConcreteProduct1();
169         break;
170     default:
171         break;
172     }
173     return pProduct;
174 }
175
176 //main.cpp
177 #include "Factory.h"
178 #include "Product.h"
179 #include <iostream>
180
181 using namespace std;
182
183 int main()
184 {
185     Factory* pFactory = new ConcreteFactory();
186
187     Product* pProduct = pFactory->CreateProduct1();
188
189     //Product* pProduct = pFactory->CreateConcreteProduct(1);
190     return 0;
191 }

若要为不同类的类提供一个创建对象的接口,要用AbstractFactory。

工厂模式主要是为创建对象提供过渡接口,以便将创建对象的具体过程屏蔽隔离起来,达到提高灵活性的目的。 
工厂模式可以分为三类: 
1)简单工厂模式(Simple Factory) 
2)工厂方法模式(Factory Method) 
3)抽象工厂模式(Abstract Factory) 
         这三种模式从上到下逐步抽象,并且更具一般性。 
        GOF在《设计模式》一书中将工厂模式分为两类:工厂方法模式(Factory Method)与抽象工厂模式(Abstract Factory)。将简单工厂模式(Simple Factory)看为工厂方法模式的一种特例,两者归为一类。

区别 
工厂方法模式:
一个抽象产品类,可以派生出多个具体产品类。   
一个抽象工厂类,可以派生出多个具体工厂类。   
每个具体工厂类只能创建一个具体产品类的实例。
抽象工厂模式:
多个抽象产品类,每个抽象产品类可以派生出多个具体产品类。   
一个抽象工厂类,可以派生出多个具体工厂类。   
每个具体工厂类可以创建多个具体产品类的实例。   
区别:
工厂方法模式只有一个抽象产品类,而抽象工厂模式有多个。   
工厂方法模式的具体工厂类只能创建一个具体产品类的实例,而抽象工厂模式可以创建多个。
两者皆可。

C++设计模式-Factory工厂模式相关推荐

  1. C++设计模式 | Factory工厂模式(简单工厂、工厂方法、抽象工厂)

    目录 简单工厂 工厂方法 抽象工厂 学习工厂模式需要有C++虚函数 纯虚函数等相关继承与多态知识 简单工厂 在简单工厂模式中,可以根据参数的不同返回不同的实例..简单工厂模式专门定义一个类来负责创建其 ...

  2. 【JAVA进阶系列】JAVA 设计模式 -- 抽象工厂模式(Abstract Factory)

    [JAVA进阶系列]JAVA 设计模式 -- 抽象工厂模式(Abstract Factory) [1.1]抽象工厂模式简介 抽象工厂者模式的类结构图 AbstractProduct(抽象产品),Abs ...

  3. 设计模式之工厂模式(Factory)

    设计模式之工厂模式 设计模式之工厂模式 简介 代码准备 需求一 需求二 简单工厂 工厂方法 抽象工厂 需求三 总结 设计模式之工厂模式 简介 工厂顾名思义就是创建产品,根据产品是具体产品还是具体工厂可 ...

  4. Java设计模式(工厂模式>抽象工厂模式和原型模式)

    Java设计模式Ⅱ 1.工厂模式 1.1 简单工厂模式 1.2 工厂方法模式 2.抽象工厂模式 3.总结 4.原型模式 4.1 原型模式 4.2 浅拷贝 4.3 深拷贝 5.建造者模式 1.工厂模式 ...

  5. 设计模式之工厂模式(三)

    上一次我们已经通过代码,简单的认识了工厂方法模式,具体的思路请移步到设计模式之工厂模式(二),进行查看.这次,让我们通过设计模式的思想,来好好认识下工厂方法模式. 创建者和产品 所有工厂模式都用来封装 ...

  6. php工厂模式和单例模式,php 设计模式之工厂模式、单例模式、注册树模式

    php 设计模式之工厂模式.单例模式.注册树模式 在软件工程中,创建型设计模式承担着对象创建的职责,尝试创建适合程序上下文的对象,对象创建设计模式的产生是由于软件工程设计的问题,具体说是向设计中增加复 ...

  7. 教你如何一篇博客读懂设计模式之—--工厂模式

    一篇博客读懂设计模式之-工厂模式 工厂模式在我们日常开发的时候经常用到,相信大家都有了一定的了解,工厂模式是一种创建对象的设计模式,它提供一种创建对象的最佳方式. 主要过程是: 定义一个创建对象的接口 ...

  8. 常见设计模式—抽象工厂模式

    设计模式-抽象工厂模式 1.什么是抽象工厂模式 抽象工厂模式是围绕一个超级工厂创建其它工厂,是工厂方法模式的升级版本,工厂方法模式只生产一个等级的产品,而抽象工厂模式可生产多个等级的产品. 2.角色分 ...

  9. java设计模式之工厂模式(UML类图分析+代码详解)

    大家好,我是一名在算法之路上不断前进的小小程序猿!体会算法之美,领悟算法的智慧~ 希望各位博友走过路过可以给我点个免费的赞,你们的支持是我不断前进的动力!! 加油吧!未来可期!! 本文将介绍java设 ...

最新文章

  1. JSON.stringify报cyclic object value错误
  2. 路由(route)是什么意思?
  3. 如何使用两个堆栈实现队列_使用两个队列实现堆栈
  4. @程序员,你该如何做职业规划才能不被“废”?
  5. 腾讯围棋AI绝艺战胜DeepZenGo又夺一冠
  6. ASP.NET Core DI 手动获取注入对象
  7. windows mobile设置插移动卡没反应_u盘插入电脑没反应怎么办 u盘插入电脑没反应解决方法【详解】...
  8. github fork别人项目后如何同步更新原项目
  9. Java线程基础回顾及内存模型,看你还记得多少?
  10. [转帖]浪潮openstack的逆袭
  11. houdini 常用
  12. 【书山有路】互联网+:从IT到DT 读书笔记
  13. Gif 录制工具:Screen2Gif
  14. 苏大计算机考研专业课,苏州大学软件工程考研初试科目考什么?
  15. IPCam的启动过程和工作流程
  16. 盗将行——微信群防骗指南
  17. 华为关于LINUX的认证,华为认证:华为TA128在LINUX下的简单设置
  18. ipv6联网几十分钟后显示无网络连接,v4网络正常的解决方法
  19. SSH三大框架笔面试总结
  20. 2022年深圳市国家高新技术企业认定条件、时间、流程

热门文章

  1. expdp oracle 并行_DMP文件导入ORACLE指南
  2. coreldraw x4如何重叠图片_CorelDRAW复制及镜面反转对象
  3. C++中拷贝构造函数
  4. gganimate|让你的图动起来!!!
  5. 汇总下最近没时间更新的机器学习,五一后更起来
  6. C/C++传递二维数组[转载]
  7. 种草!这些好像用处不大但是很可爱的Mac软件
  8. 雪花算法生成数字id_全局唯一iD的生成 雪花算法详解及其他用法
  9. 学什么c语言标准,C语言的标准 “输入输出”!今天是你学C语言的第几天?
  10. 【ES9(2018)】String 扩展 标签模板里字符串转义