设计模式 - Factory模式(工厂模式)
Factory模式(工厂模式):定义一个用于创建对象的接口,让子类决定实例化哪一个类。Factory模式使一个类的实例化延迟到其子类。
两个最重要的功能:
1. 定义创建对象的接口,封装了对象的创建;
2. 使得具体化类的工作延迟到了子类中。
uml图
抽象基类:
1)Product:创建出来的对象的抽象基类.
2)Factory创建对象的工厂方法的抽象基类.
接口函数:
1)Creator::FactoryMethod:纯虚函数,由派生类实现,创建出对应的Product.
解析:
在这个模式中,有两个抽象基类,一个是Product为创建出来的对象的抽象基类,一个是Factory是工厂的抽象基类,在互相协作的时候都是由相应的Factory派生类来生成Product的派生类,也就是说如果要新增一种Product那么也要对应的新增一个Factory,创建的过程委托给了这个Factory.也就是说一个Factory和一个Product是一一对应的关系.
备注:
设计模式的演示图上把Factory类命名为Creator,下面的实现沿用了这个命名.
示例代码:
1 // Factory.h
2 #ifndef FACTORY_H
3 #define FACTORY_H
4
5 class Product
6 {
7 public:
8 Product(){}
9 virtual ~Product(){}
10 };
11
12 class ConcreateProduct
13 : public Product
14 {
15 public:
16 ConcreateProduct();
17 virtual ~ConcreateProduct();
18 };
19
20 class Factory
21 {
22 public:
23 Factory(){}
24 virtual ~Factory(){}
25
26 void AnOperation();
27
28 protected:
29 virtual Product* FactoryMethod() = 0;
30 };
31
32 class ConcreateFactory
33 : public Factory
34 {
35 public:
36 ConcreateFactory();
37 virtual ~ConcreateFactory();
38
39 protected:
40 virtual Product* FactoryMethod();
41 };
42
43 #endif
44
2 #ifndef FACTORY_H
3 #define FACTORY_H
4
5 class Product
6 {
7 public:
8 Product(){}
9 virtual ~Product(){}
10 };
11
12 class ConcreateProduct
13 : public Product
14 {
15 public:
16 ConcreateProduct();
17 virtual ~ConcreateProduct();
18 };
19
20 class Factory
21 {
22 public:
23 Factory(){}
24 virtual ~Factory(){}
25
26 void AnOperation();
27
28 protected:
29 virtual Product* FactoryMethod() = 0;
30 };
31
32 class ConcreateFactory
33 : public Factory
34 {
35 public:
36 ConcreateFactory();
37 virtual ~ConcreateFactory();
38
39 protected:
40 virtual Product* FactoryMethod();
41 };
42
43 #endif
44
1 //Factory.cpp
2 #include "Factory.h"
3 #include <iostream>
4
5 using namespace std;
6
7 ConcreateProduct::ConcreateProduct()
8 {
9 std::cout << "construction of ConcreateProduct\n";
10 }
11
12 ConcreateProduct::~ConcreateProduct()
13 {
14 std::cout << "destruction of ConcreateProduct\n";
15 }
16
17 void Factory::AnOperation()
18 {
19 Product* p = FactoryMethod();
20
21 std::cout << "an operation of product\n";
22 }
23
24 ConcreateFactory::ConcreateFactory()
25 {
26 std::cout << "construction of ConcreateFactory\n";
27 }
28
29 ConcreateFactory::~ConcreateFactory()
30 {
31 std::cout << "destruction of ConcreateFactory\n";
32 }
33
34 Product* ConcreateFactory::FactoryMethod()
35 {
36 return new ConcreateProduct();
37 }
38
2 #include "Factory.h"
3 #include <iostream>
4
5 using namespace std;
6
7 ConcreateProduct::ConcreateProduct()
8 {
9 std::cout << "construction of ConcreateProduct\n";
10 }
11
12 ConcreateProduct::~ConcreateProduct()
13 {
14 std::cout << "destruction of ConcreateProduct\n";
15 }
16
17 void Factory::AnOperation()
18 {
19 Product* p = FactoryMethod();
20
21 std::cout << "an operation of product\n";
22 }
23
24 ConcreateFactory::ConcreateFactory()
25 {
26 std::cout << "construction of ConcreateFactory\n";
27 }
28
29 ConcreateFactory::~ConcreateFactory()
30 {
31 std::cout << "destruction of ConcreateFactory\n";
32 }
33
34 Product* ConcreateFactory::FactoryMethod()
35 {
36 return new ConcreateProduct();
37 }
38
1 // Main.cpp
2 #include "Factory.h"
3 #include <stdlib.h>
4
5 int main(int argc,char* argv[])
6 {
7 Factory *p = new ConcreateFactory();
8 p->AnOperation();
9
10 delete p;
11
12 system("pause");
13
14 return 0;
15 }
16
2 #include "Factory.h"
3 #include <stdlib.h>
4
5 int main(int argc,char* argv[])
6 {
7 Factory *p = new ConcreateFactory();
8 p->AnOperation();
9
10 delete p;
11
12 system("pause");
13
14 return 0;
15 }
16