简单工厂模式:当在程序中创建对象的时候少不了new,有时候new会很多,又分布在程序的不同地方,管理起来很不方便,这个时候需要一个工厂类,专门负责对象的创建和释放,将对象的这种操作统一在一起,同时工厂类向外部提供了创建对象的接口,而对对象的使用则和这个工厂类毫无关系。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
#ifndef
_FACTORY_H_ #define
_FACTORY_H_ #include
<iostream> using namespace
std; enum ProductType { typeA, typeB }; //产品类的基类 class Product { public : //存虚函数 virtual void
show()=0; }; class ProductA
: public Product { public : void show() { cout<< "ProductA\n" ; }; }; class ProductB
: public Product { public : void show() { cout<< "ProductB\n" ; }; }; //工厂类,用来产生产品 class Factory { public : //根据传入的不同产品类型产生不同的对象 Product
* createProduct(ProductType type) { switch (type) { case typeA: return new
ProductA(); case typeB: return new
ProductB(); default : return NULL; } }; }; #endif #include
"Factory.h" int main() { Factory
* factory = new Factory(); //产生产品 ProductA
* pa = (ProductA *)factory->createProduct(typeA); pa->show(); ProductB
* pb = (ProductB *)factory->createProduct(typeB); pb->show(); //记得析构 delete factory; //产品的析构可以放到factory类的析构函数中去做 delete pa; delete pb; return 0; } |
工厂方法模式:是为了解决简单工厂模式的弊端存在的,简单工厂模式的扩展性不好,比如我们有了第三个产品ProductC,我们需要工厂为我们产生这个对象,怎么办,需要修改工厂类中的创建对象的函数,也就是switch结构,还有就是枚举处也要进行修改,而这种修改会带来不少的弊端,所以我们就有了工厂方法模式。这个模式将Factory设计为抽象类,其中包含子类必须实现的方法,而对产品的具体创建则放到Factory的子类中去完成。这个时候如果有一个产品C,我们就创建一个工厂类FactoryC,专门用来产生产品C,就不需要改动其他地方的代码了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
#ifndef
_FACTORY_H_ #define
_FACTORY_H_ #include
<iostream> using namespace
std; //产品类的基类 class Product { public : //存虚函数 virtual void
show()=0; }; class ProductA
: public Product { public : void show() { cout<< "ProductA\n" ; }; }; class ProductB
: public Product { public : void show() { cout<< "ProductB\n" ; }; }; //工厂抽象类,定义子类必须实现的接口 class Factory { public : //根据传入的不同产品类型产生不同的对象 virtual Product
* createProduct()=0; }; //具体的工厂类,用来产生不同的产品 class FactoryA
: public Factory { public : Product
* createProduct() { return new
ProductA(); }; }; class FactoryB
: public Factory { public : Product
* createProduct() { return new
ProductB(); }; }; #endif #include
"Factory.h" int main() { //产生产品 FactoryA
* factoryA = new FactoryA(); Product
* pa = factoryA->createProduct(); pa->show(); FactoryB
* factoryB = new FactoryB(); Product
* pb = factoryB->createProduct(); pb->show(); //记得析构 delete factoryA; delete factoryB; //产品的析构可以放到factory类的析构函数中去做 delete pa; delete pb; return 0; } |
抽象工厂模式:现在我们的产品类都是继承自Product的,如果有一个产品不是Product的子类怎么办呢?这个时候就用到了抽象工厂模式,这个模式是工厂方法模式的叠加,其他的东西类似,看下代码就清楚了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
#ifndef
_FACTORY_H_ #define
_FACTORY_H_ #include
<iostream> using namespace
std; //产品类A类的基类 class ProductA { public : //存虚函数 virtual void
show()=0; }; class ProductA1
: public ProductA { public : void show() { cout<< "ProductA1\n" ; }; }; class ProductA2
: public ProductA { public : void show() { cout<< "ProductA2\n" ; }; }; //产品类B类的基类 class ProductB { virtual void
show()=0; }; class ProductB1
: public ProductB { void show() { cout<< "ProductB1\n" ; }; }; class ProductB2
: public ProductB { void show() { cout<< "ProductB2\n" ; }; }; //工厂抽象类,定义子类必须实现的接口 class Factory { public : //根据传入的不同产品类型产生不同的对象 virtual ProductA
* createProductA()=0; virtual ProductB
* createProductB()=0; }; //具体的工厂类,用来产生不同的产品 class FactoryA
: public Factory { public : ProductA
* createProductA() { return new
ProductA1(); }; ProductB
* createProductB() { return new
ProductB1(); }; }; class FactoryB
: public Factory { public : ProductA
* createProductA() { return new
ProductA2(); }; ProductB
* createProductB() { return new
ProductB2(); }; }; #endif |
一切生成并返回一个对象的静态函数就是一个工厂方法,这样的话,Cocos2d-x中是不是有很多这样的方法?比如创建场景的createScene函数,创建多数对象的create函数,一个经典的工厂方法如同这样:
1
2
3
4
5
6
7
|
Sprite*
factoryMethod() { Sprite*
ret = new Sprite(); //在这里对
ret 对象进行必要的初始化操作 ret->autorelease(); return ret; } |