简单工厂:工厂依据传进的参数创建相应的产品。

1
2
{《HeadFirst设计模式》工厂模式之简单工厂 }
3
{ 产品类 }
4
{ 编译工具 :Delphi7.0 }
5
{ 联系方式 :guzh-0417@163.com }
6
7
unit uProducts;
8
9
interface
10
11
type
12
TPizza = class(TObject)
13
public
14
procedure Prepare; virtual; abstract;
15
procedure Bake; virtual; abstract;
16
procedure Cut; virtual; abstract;
17
procedure Box; virtual; abstract;
18
end;
19
20
TCheesePizza = class(TPizza)
21
public
22
procedure Prepare; override;
23
procedure Bake; override;
24
procedure Cut; override;
25
procedure Box; override;
26
end;
27
28
TVeggiePizza = class(TPizza)
29
public
30
procedure Prepare; override;
31
procedure Bake; override;
32
procedure Cut; override;
33
procedure Box; override;
34
end;
35
36
TClamPizza = class(TPizza)
37
public
38
procedure Prepare; override;
39
procedure Bake; override;
40
procedure Cut; override;
41
procedure Box; override;
42
end;
43
44
TPepperoniPizza = class(TPizza)
45
public
46
procedure Prepare; override;
47
procedure Bake; override;
48
procedure Cut; override;
49
procedure Box; override;
50
end;
51
52
implementation
53
54
{ TCheesePizza }
55
56
procedure TCheesePizza.Bake;
57
begin
58
Writeln('烘烤奶油比萨');
59
end;
60
61
procedure TCheesePizza.Box;
62
begin
63
Writeln('将奶油比萨装盒');
64
end;
65
66
procedure TCheesePizza.Cut;
67
begin
68
Writeln('将奶油比萨切成片');
69
end;
70
71
procedure TCheesePizza.Prepare;
72
begin
73
Writeln('准备奶油比萨');
74
end;
75
76
{ TVeggiePizza }
77
78
procedure TVeggiePizza.Bake;
79
begin
80
Writeln('烘烤素比萨');
81
end;
82
83
procedure TVeggiePizza.Box;
84
begin
85
Writeln('将素比萨装盒');
86
end;
87
88
procedure TVeggiePizza.Cut;
89
begin
90
Writeln('将素比萨切成片');
91
end;
92
93
procedure TVeggiePizza.Prepare;
94
begin
95
Writeln('准备素比萨');
96
end;
97
98
{ TClamPizza }
99
100
procedure TClamPizza.Bake;
101
begin
102
Writeln('烘烤蛤蛎比萨');
103
end;
104
105
procedure TClamPizza.Box;
106
begin
107
Writeln('将蛤蛎比萨装盒');
108
end;
109
110
procedure TClamPizza.Cut;
111
begin
112
Writeln('将蛤蛎比萨切成片');
113
end;
114
115
procedure TClamPizza.Prepare;
116
begin
117
Writeln('准备蛤蛎比萨');
118
end;
119
120
{ TPepperoniPizza }
121
122
procedure TPepperoniPizza.Bake;
123
begin
124
Writeln('烘烤Pepperoni比萨');
125
end;
126
127
procedure TPepperoniPizza.Box;
128
begin
129
Writeln('将Pepperoni比萨装盒');
130
end;
131
132
procedure TPepperoniPizza.Cut;
133
begin
134
Writeln('将Pepperoni比萨切成片');
135
end;
136
137
procedure TPepperoniPizza.Prepare;
138
begin
139
Writeln('准备Pepperoni比萨');
140
end;
141
142
end.

1
2
{《HeadFirst设计模式》工厂模式之简单工厂 }
3
{ 简单工厂类 }
4
{ 编译工具 :Delphi7.0 }
5
{ 联系方式 :guzh-0417@163.com }
6
7
unit uSimpleFactory;
8
9
interface
10
11
uses
12
uProducts;
13
14
type
15
{ 静态工厂 }
16
TSimplePizzaFactory = class(TObject)
17
public
18
class function CreatePizza(Category: Integer): TPizza;
19
end;
20
21
implementation
22
23
{ TSimplePizzaFactory }
24
25
class function TSimplePizzaFactory.CreatePizza(Category: Integer): TPizza;
26
begin
27
case Category of
28
0: Result := TCheesePizza.Create; {创建奶油比萨 }
29
1: Result := TVeggiePizza.Create; {创建素比萨 }
30
2: Result := TClamPizza.Create; {创建蛤蛎比萨 }
31
3: Result := TPepperoniPizza.Create; {创建Pepperoni比萨 }
32
else
33
Result := nil;
34
end; // end case.
35
end;
36
37
end.

1
2
{《HeadFirst设计模式》工厂模式之简单工厂 }
3
{ 简单工厂的用户类 }
4
{ 编译工具 :Delphi7.0 }
5
{ 联系方式 :guzh-0417@163.com }
6
7
unit uUserOfSimpleFactory;
8
9
interface
10
11
uses
12
uProducts, uSimpleFactory;
13
14
type
15
{ 工厂的用户 }
16
TPizzaStore = class(TObject)
17
public
18
procedure OrderPizza;
19
end;
20
21
implementation
22
23
{ TOrderPizza }
24
25
procedure TPizzaStore.OrderPizza;
26
var
27
aPizza: TPizza;
28
begin
29
aPizza := TSimplePizzaFactory.CreatePizza(0);
30
31
aPizza.Prepare();
32
aPizza.Bake();
33
aPizza.Cut();
34
aPizza.Box();
35
36
aPizza.Free;
37
end;
38
39
end.

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

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142


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


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


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

运行结果: