抽象工厂 C++实现

抽象工厂(Abstract Factory)

抽象工厂是为了提供一系列相关或相互依赖对象的接口。对象创建型模式的一种。

客户Client
抽象工厂接口AbstractFactory
抽象工厂的实现类ConcreteFactory
抽象产品接口AbstractProduct
产品实现类ConcreteProduct

需要生产的几种产品分别是:ConcreteProductA1/ConcreteProductA2/ConcreteProductB1/ConcreteProductB2。

各个系列产品的启动和退出方式相同,但是运行方式不同。这里分别用一个具体工厂ConcreteFactory1和ConcreteFactory2的对象来生产多种产品。

首先是Abstact以及Concrete Product:

下面的代码应该注意的是,ConcreteProductA1与ConcreteProductB1实际上是属于同一产品族的。所谓的统一产品族指的是类似的产品,例如BenzSportCar和BmwSportCar和AudiSportCar属于SportCar产品族.

 1 class AbstractProductA{
 2 public:
 3     AbstractProductA(){}
 4     virtual ~AbstractProductA(){}
 5     void start(){
 6         cout << "Product A start like this !" << endl;
 7     }
 8     virtual void execute() = 0;
 9     void exit(){
10         cout << "Product A exit like this !" << endl;
11     }
12 };
13 
14 
15 class ConcreteProductA1 : public AbstractProductA{
16 public:
17     ConcreteProductA1(){}
18     ~ConcreteProductA1(){}
19     void execute(){
20         cout << "ConcreteProductA1 execute like this!" << endl;
21     }
22 };
23 
24 class ConcreteProductA2 : public AbstractProductA{
25 public:
26     ConcreteProductA2(){}
27     ~ConcreteProductA2(){}
28     void execute(){
29         cout << "ConcreteProductA2 execute like this!" << endl;
30     }
31 };
32 
33 
34 class AbstractProductB{
35 public:
36     AbstractProductB(){}
37     virtual ~AbstractProductB(){}
38     void start(){
39         cout << "Product B start like this!" << endl;
40     }
41     virtual void execute() = 0;
42     void exit(){
43         cout << "ProductB exit like this!" << endl;
44     }
45 };
46 
47 class ConcreteProductB1 : public AbstractProductB{
48 public:
49     ConcreteProductB1(){}
50     ~ConcreteProductB1(){}
51     void execute(){
52         cout << "concreteProductB1 execute like this!" << endl;
53     }
54 };
55 
56 class ConcreteProductB2 : public AbstractProductB{
57 public:
58     ConcreteProductB2(){}
59     ~ConcreteProductB2(){}
60     void execute(){
61         cout << "concreteProductB2 execute like this!" << endl;
62     }
63 };

之后就是Abstract以及Concrete Factory:

 1 class AbstractFactory{
 2 public:
 3     AbstractFactory(){}
 4     virtual ~AbstractFactory(){}
 5     virtual AbstractProductA * createProductA() = 0;
 6     virtual AbstractProductB * createProductB() = 0;
 7 };
 8 
 9 class ConcreteFactory1 : public AbstractFactory{
10 public:
11     ConcreteFactory1(){}
12     ~ConcreteFactory1(){}
13     AbstractProductA * createProductA();
14     AbstractProductB * createProductB();
15 };
16 
17 AbstractProductA * ConcreteFactory1::createProductA()
18 {
19     return new ConcreteProductA1();
20 }
21 
22 AbstractProductB * ConcreteFactory1::createProductB()
23 {
24     return new ConcreteProductB1();
25 }
26 
27 class ConcreteFactory2 : public AbstractFactory{
28 public:
29     ConcreteFactory2(){}
30     ~ConcreteFactory2(){};
31     AbstractProductA * createProductA();
32     AbstractProductB * createProductB();
33 };
34 
35 AbstractProductA * ConcreteFactory2::createProductA()
36 {
37     return new ConcreteProductA2();
38 }
39 
40 AbstractProductB * ConcreteFactory2::createProductB()
41 {
42     return new ConcreteProductB2();
43 }

测试方法如下所示:

 1 int main()
 2 {
 3     AbstractFactory * abf = new ConcreteFactory1();
 4     AbstractProductA * aba = abf->createProductA();
 5     aba->start();
 6     aba->execute();
 7     aba->exit();
 8     AbstractProductB * abb = abf->createProductB();
 9     abb->start();
10     abb->execute();
11     abb->exit();
12 
13     abf = new ConcreteFactory2();
14     aba = abf->createProductA();
15     aba->start();
16     aba->execute();
17     aba->exit();
18     abb = abf->createProductB();
19     abb->start();
20     abb->execute();
21     abb->exit();
22     system("pause");
23 }

参考:

http://www.cnblogs.com/zhangchenliang/p/3700820.html  这个讲的真的很好,推荐

http://www.cnblogs.com/cbf4life/archive/2009/12/23/1630612.html

posted @ 2016-05-19 19:39  eversliver  阅读(2231)  评论(0编辑  收藏  举报