博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c++设计模式之工厂模式
阅读量:4487 次
发布时间:2019-06-08

本文共 4182 字,大约阅读时间需要 13 分钟。

1:简单工厂模式

  简单工厂模式是属于创建型模式,又叫做静态工厂方法(static Factory Method)模式,简单工厂模式是由一个工厂对象决定创建出来哪一种产品类的实例.

  简单工厂模式的实质是由一个工厂类根据传入的参数,动态决定应该创建哪一类产品类(这些产品类继承自一个父类或接口)的实例。打个比方

  假设有一个工厂,他能生产出A、B两种产品。当客户需要产品的时候一定要告诉共产是哪种产品,是A还是B。当新增加一种新产品的时候,那么就要去修改工厂的类。 

1 // Factory.cpp : 定义控制台应用程序的入口点。 2 // 3  4 #include "stdafx.h" 5 #include
6 7 using namespace std; 8 9 class Product10 {11 public:12 virtual void show() = 0; 13 };14 15 class Product_A : public Product16 {17 public:18 void show()19 {20 cout << "Product_A" << endl;21 }22 };23 24 class Product_B : public Product25 {26 public:27 void show()28 {29 cout << "Product_B" << endl;30 }31 };32 33 class Factory34 {35 public:36 Product* Create(int i)37 {38 switch (i)39 {40 case 1:41 return new Product_A;42 break;43 case 2:44 return new Product_B;45 break;46 default:47 break;48 }49 }50 };51 52 int main()53 {54 Factory *factory = new Factory();55 factory->Create(1)->show();56 factory->Create(2)->show();57 system("pause");58 return 0;59 }

 

二:工厂方法模式:

  上面的简单工厂模式的缺点是当新增产品的时候就要去修改工厂的类,这就违反了开放封闭原则,(类、模块、函数)可以扩展,但是不可以修改,于是,就出现了工厂方法模式。所谓工厂方法模式,是指定义一个用于创建对象的接口,让子类决定实例化哪一个类。打个比方

现在有A、B两种产品,那么久开两个工厂。工厂A负责生产A产品,工厂B负责生产B种产品。这时候客户不需要告诉共产生产哪种产品了,只需要告诉共产生产就可以了。

  

1 #include "stdafx.h" 2 #include
3 4 using namespace std; 5 6 class Product 7 { 8 public: 9 virtual void show() = 0; 10 };11 12 class Product_A : public Product13 {14 public:15 void show()16 {17 cout << "Product_A" << endl;18 }19 };20 21 class Product_B : public Product22 {23 public:24 void show()25 {26 cout << "Product_B" << endl;27 }28 };29 30 class Factory31 {32 public:33 virtual Product* create() = 0;34 };35 36 class Factory_A : public Factory37 {38 public:39 Product* create()40 {41 return new Product_A;42 }43 };44 45 class Factory_B : public Factory46 {47 public:48 Product* create()49 {50 return new Product_B;51 }52 };53 54 int main()55 {56 Factory_A* productA = new Factory_A();57 Factory_B* productB = new Factory_B();58 59 productA->create()->show();60 productB->create()->show();61 system("pause");62 return 0;63 }

抽象工厂模式

  为什么要有抽象工厂模式,假如我们A产品中有A1和A2两种型号的厂品,B产品中有B1和B2两种型号的厂品,那怎么办,上面两种工厂模式就不能解决了。这个时候抽象工厂模式就登场了。还是开设两家工厂,工厂A负责生产A1 、A2型号产品,B工厂负责生产B1、B2型号的产品。  

提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。  适用性:一个系统要独立于它的产品的创建、组合和表示时。一个系统要由多个产品系列中的一个来配置时。当你要强调一系列相关的产品对象的设计以便进行联合使用时。当你提供一个产品类库,而只想显示它们的接口而不是实现时。

1 #include 
2 using namespace std; 3 4 //定义抽象类 5 class product1 6 { 7 public: 8 virtual void show() = 0; 9 }; 10 11 //定义具体类 12 class product_A1 :public product1 13 { 14 public: 15 void show(){ cout << "product A1" << endl; } 16 }; 17 18 class product_B1 :public product1 19 { 20 public: 21 void show(){ cout << "product B1" << endl; } 22 }; 23 24 //定义抽象类 25 class product2 26 { 27 public: 28 virtual void show() = 0; 29 }; 30 31 //定义具体类 32 class product_A2 :public product2 33 { 34 public: 35 void show(){ cout << "product A2" << endl; } 36 }; 37 38 class product_B2 :public product2 39 { 40 public: 41 void show(){ cout << "product B2" << endl; } 42 }; 43 44 45 class Factory 46 { 47 public: 48 virtual product1 *creat1() = 0; 49 virtual product2 *creat2() = 0; 50 }; 51 52 class FactoryA 53 { 54 public: 55 product1 *creat1(){ return new product_A1(); } 56 product2 *creat2(){ return new product_A2(); } 57 }; 58 59 class FactoryB 60 { 61 public: 62 product1 *creat1(){ return new product_B1(); } 63 product2 *creat2(){ return new product_B2(); } 64 }; 65 66 int main() 67 { 68 FactoryA *factoryA = new FactoryA(); 69 factoryA->creat1()->show(); 70 factoryA->creat2()->show(); 71 72 FactoryB *factoryB = new FactoryB(); 73 factoryB->creat1()->show(); 74 factoryB->creat2()->show(); 75 76 return 0; 77 }

 

 

 

 

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/cxq0017/p/6544517.html

你可能感兴趣的文章
DHTML5(控件动态效果综合应用与表单校验)
查看>>
Oracle Enterprise Manager 12c 新特性:实时Real-Time Addm
查看>>
同时安装office2016与visio2016的实现过程
查看>>
vtk类之vtkShrinkFilter :收缩构成对其质心任意数据集的单元格,返回vtkUnstructuredGrid数据集...
查看>>
在此记录一下SharpGL最初创建的程序
查看>>
View Controller Programming Guide for iOS---(六)---Responding to Display-Related Notifications
查看>>
redis数据操作笔记
查看>>
委托的匿名方法
查看>>
五种在家观看3D电影的解决方案
查看>>
安装windowbuilder错误一例
查看>>
PS CC 2014破解版
查看>>
Cinder 挂盘创建不成功解决方案日志中报错
查看>>
[CGGeometry]CGRectInset解析
查看>>
虚机克隆搭建kafka服务器集群
查看>>
二叉排序树
查看>>
Linux 基础入门二
查看>>
最基本的Git使用方式(eclipse上)
查看>>
写给2013的自己
查看>>
Laravel-lumen 配置JWT
查看>>
MySQL常用存储引擎:MyISAM与InnoDB之华山论剑
查看>>