forked from yaocoder/DesignPattern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleFactoryPattern.cpp
77 lines (62 loc) · 965 Bytes
/
SimpleFactoryPattern.cpp
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
/*
简单工厂模式
*/
#include <iostream>
#include <string>
using namespace std;
class Pizza
{
public:
virtual void Prapare() = 0;
virtual void Bake() = 0;
};
class CheesePizza:public Pizza
{
public:
void Prapare()
{
cout << "CheesePizza Prapare.\n";
}
void Bake()
{
cout << "CheesePizza Bake.\n";
}
};
class VeggiePizza:public Pizza
{
public:
void Prapare()
{
cout << "VeggiePizza Prapare.\n";
}
void Bake()
{
cout << "VeggiePizza Bake.\n";
}
};
class SimplePizzaFactory
{
public:
static Pizza* CreatePizza(string type)
{
if (0 == type.compare("cheese"))
{
pizza_ = new CheesePizza;
}
else if (0 == type.compare("veggie"))
{
pizza_ = new VeggiePizza;
}
return pizza_;
}
private:
static Pizza* pizza_;
};
Pizza* SimplePizzaFactory::pizza_ = NULL;
int main()
{
SimplePizzaFactory::CreatePizza("cheese")->Prapare();
SimplePizzaFactory::CreatePizza("veggie")->Prapare();
system("pause");
return 0;
}