forked from FengJungle/DesignPattern
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactoryMethod.h
More file actions
118 lines (106 loc) · 1.95 KB
/
FactoryMethod.h
File metadata and controls
118 lines (106 loc) · 1.95 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#ifndef __FACTORY_METHOD__
#define __FACTORY_METHOD__
#include <iostream>
#include <string.h>
using namespace std;
// 抽象产品类AbstractProduct
class AbstractSportProduct
{
public:
AbstractSportProduct(){
}
virtual ~AbstractSportProduct(){}
// 抽象方法:
virtual void printName(){};
virtual void play(){};
};
// 具体产品类Basketball
class Basketball :public AbstractSportProduct
{
public:
Basketball(){
printName();
play();
}
// 具体实现方法
void printName(){
printf("Jungle get Basketball\n");
}
void play(){
printf("Jungle play Basketball\n\n");
}
};
// 具体产品类Football
class Football :public AbstractSportProduct
{
public:
Football(){
printName();
play();
}
// 具体实现方法
void printName(){
printf("Jungle get Football\n");
}
void play(){
printf("Jungle play Football\n\n");
}
};
// 具体产品类Volleyball
class Volleyball :public AbstractSportProduct
{
public:
Volleyball(){
printName();
play();
}
// 具体实现方法
void printName(){
printf("Jungle get Volleyball\n");
}
void play(){
printf("Jungle play Volleyball\n\n");
}
};
// 抽象工厂类
class AbstractFactory
{
public:
virtual ~AbstractFactory(){}
virtual AbstractSportProduct *getSportProduct() = 0;
};
// 具体工厂类BasketballFactory
class BasketballFactory :public AbstractFactory
{
public:
BasketballFactory(){
printf("BasketballFactory\n");
}
AbstractSportProduct *getSportProduct(){
printf("basketball");
return new Basketball();
}
};
// 具体工厂类FootballFactory
class FootballFactory :public AbstractFactory
{
public:
FootballFactory(){
printf("FootballFactory\n");
}
AbstractSportProduct *getSportProduct(){
return new Football();
}
};
// 具体工厂类VolleyballFactory
class VolleyballFactory :public AbstractFactory
{
public:
VolleyballFactory(){
printf("VolleyballFactory\n");
}
AbstractSportProduct *getSportProduct(){
return new Volleyball();
}
};
#endif //__FACTORY_METHOD__