c++设计模式之工厂模式

1:简单工厂模式

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

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

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

 1 // Factory.cpp : 定义控制台应用程序的入口点。
 2 //
 3 
 4 #include "stdafx.h"
 5 #include<iostream>
 6 
 7 using namespace std;
 8 
 9 class Product
10 {
11 public:
12     virtual void show() = 0;  
13 };
14 
15 class Product_A : public Product
16 {
17 public:
18     void show()
19     {
20         cout << "Product_A" << endl;
21     }
22 };
23 
24 class Product_B : public Product
25 {
26 public:
27     void show()
28     {
29         cout << "Product_B" << endl;
30     }
31 };
32 
33 class Factory
34 {
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<iostream>
 3 
 4 using namespace std;
 5 
 6 class Product
 7 {
 8 public:
 9     virtual void show() = 0;  
10 };
11 
12 class Product_A : public Product
13 {
14 public:
15     void show()
16     {
17         cout << "Product_A" << endl;
18     }
19 };
20 
21 class Product_B : public Product
22 {
23 public:
24     void show()
25     {
26         cout << "Product_B" << endl;
27     }
28 };
29 
30 class Factory
31 {
32 public:
33     virtual Product* create() = 0;
34 };
35 
36 class Factory_A : public Factory
37 {
38 public:
39     Product* create()
40     {
41         return new Product_A;
42     }
43 };
44 
45 class Factory_B : public Factory
46 {
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 <iostream>    
 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 }  

 

 

 

 

 

 

 

 

 

 

 

 

posted @ 2017-03-13 19:10  evilsnake  阅读(21259)  评论(4编辑  收藏  举报