简单工厂:工厂依据传进的参数创建相应的产品。
1
2{《HeadFirst设计模式》工厂模式之简单工厂 }
3{ 产品类 }
4{ 编译工具 :Delphi7.0 }
5{ 联系方式 :guzh-0417@163.com }
6
7unit uProducts;
8
9interface
10
11type
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
52implementation
53
54{ TCheesePizza }
55
56procedure TCheesePizza.Bake;
57begin
58 Writeln('烘烤奶油比萨');
59end;
60
61procedure TCheesePizza.Box;
62begin
63 Writeln('将奶油比萨装盒');
64end;
65
66procedure TCheesePizza.Cut;
67begin
68 Writeln('将奶油比萨切成片');
69end;
70
71procedure TCheesePizza.Prepare;
72begin
73 Writeln('准备奶油比萨');
74end;
75
76{ TVeggiePizza }
77
78procedure TVeggiePizza.Bake;
79begin
80 Writeln('烘烤素比萨');
81end;
82
83procedure TVeggiePizza.Box;
84begin
85 Writeln('将素比萨装盒');
86end;
87
88procedure TVeggiePizza.Cut;
89begin
90 Writeln('将素比萨切成片');
91end;
92
93procedure TVeggiePizza.Prepare;
94begin
95 Writeln('准备素比萨');
96end;
97
98{ TClamPizza }
99
100procedure TClamPizza.Bake;
101begin
102 Writeln('烘烤蛤蛎比萨');
103end;
104
105procedure TClamPizza.Box;
106begin
107 Writeln('将蛤蛎比萨装盒');
108end;
109
110procedure TClamPizza.Cut;
111begin
112 Writeln('将蛤蛎比萨切成片');
113end;
114
115procedure TClamPizza.Prepare;
116begin
117 Writeln('准备蛤蛎比萨');
118end;
119
120{ TPepperoniPizza }
121
122procedure TPepperoniPizza.Bake;
123begin
124 Writeln('烘烤Pepperoni比萨');
125end;
126
127procedure TPepperoniPizza.Box;
128begin
129 Writeln('将Pepperoni比萨装盒');
130end;
131
132procedure TPepperoniPizza.Cut;
133begin
134 Writeln('将Pepperoni比萨切成片');
135end;
136
137procedure TPepperoniPizza.Prepare;
138begin
139 Writeln('准备Pepperoni比萨');
140end;
141
142end.
1
2{《HeadFirst设计模式》工厂模式之简单工厂 }
3{ 简单工厂类 }
4{ 编译工具 :Delphi7.0 }
5{ 联系方式 :guzh-0417@163.com }
6
7unit uSimpleFactory;
8
9interface
10
11uses
12 uProducts;
13
14type
15 { 静态工厂 }
16 TSimplePizzaFactory = class(TObject)
17 public
18 class function CreatePizza(Category: Integer): TPizza;
19 end;
20
21implementation
22
23{ TSimplePizzaFactory }
24
25class function TSimplePizzaFactory.CreatePizza(Category: Integer): TPizza;
26begin
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.
35end;
36
37end.
1
2{《HeadFirst设计模式》工厂模式之简单工厂 }
3{ 简单工厂的用户类 }
4{ 编译工具 :Delphi7.0 }
5{ 联系方式 :guzh-0417@163.com }
6
7unit uUserOfSimpleFactory;
8
9interface
10
11uses
12 uProducts, uSimpleFactory;
13
14type
15 { 工厂的用户 }
16 TPizzaStore = class(TObject)
17 public
18 procedure OrderPizza;
19 end;
20
21implementation
22
23{ TOrderPizza }
24
25procedure TPizzaStore.OrderPizza;
26var
27 aPizza: TPizza;
28begin
29 aPizza := TSimplePizzaFactory.CreatePizza(0);
30
31 aPizza.Prepare();
32 aPizza.Bake();
33 aPizza.Cut();
34 aPizza.Box();
35
36 aPizza.Free;
37end;
38
39end.
1
2{《HeadFirst设计模式》工厂模式之简单工厂 }
3{ 产品类 }
4{ 编译工具 :Delphi7.0 }
5{ 联系方式 :guzh-0417@163.com }
6
7unit uProducts;
8
9interface
10
11type
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
52implementation
53
54{ TCheesePizza }
55
56procedure TCheesePizza.Bake;
57begin
58 Writeln('烘烤奶油比萨');
59end;
60
61procedure TCheesePizza.Box;
62begin
63 Writeln('将奶油比萨装盒');
64end;
65
66procedure TCheesePizza.Cut;
67begin
68 Writeln('将奶油比萨切成片');
69end;
70
71procedure TCheesePizza.Prepare;
72begin
73 Writeln('准备奶油比萨');
74end;
75
76{ TVeggiePizza }
77
78procedure TVeggiePizza.Bake;
79begin
80 Writeln('烘烤素比萨');
81end;
82
83procedure TVeggiePizza.Box;
84begin
85 Writeln('将素比萨装盒');
86end;
87
88procedure TVeggiePizza.Cut;
89begin
90 Writeln('将素比萨切成片');
91end;
92
93procedure TVeggiePizza.Prepare;
94begin
95 Writeln('准备素比萨');
96end;
97
98{ TClamPizza }
99
100procedure TClamPizza.Bake;
101begin
102 Writeln('烘烤蛤蛎比萨');
103end;
104
105procedure TClamPizza.Box;
106begin
107 Writeln('将蛤蛎比萨装盒');
108end;
109
110procedure TClamPizza.Cut;
111begin
112 Writeln('将蛤蛎比萨切成片');
113end;
114
115procedure TClamPizza.Prepare;
116begin
117 Writeln('准备蛤蛎比萨');
118end;
119
120{ TPepperoniPizza }
121
122procedure TPepperoniPizza.Bake;
123begin
124 Writeln('烘烤Pepperoni比萨');
125end;
126
127procedure TPepperoniPizza.Box;
128begin
129 Writeln('将Pepperoni比萨装盒');
130end;
131
132procedure TPepperoniPizza.Cut;
133begin
134 Writeln('将Pepperoni比萨切成片');
135end;
136
137procedure TPepperoniPizza.Prepare;
138begin
139 Writeln('准备Pepperoni比萨');
140end;
141
142end.
1
2{《HeadFirst设计模式》工厂模式之简单工厂 }
3{ 简单工厂类 }
4{ 编译工具 :Delphi7.0 }
5{ 联系方式 :guzh-0417@163.com }
6
7unit uSimpleFactory;
8
9interface
10
11uses
12 uProducts;
13
14type
15 { 静态工厂 }
16 TSimplePizzaFactory = class(TObject)
17 public
18 class function CreatePizza(Category: Integer): TPizza;
19 end;
20
21implementation
22
23{ TSimplePizzaFactory }
24
25class function TSimplePizzaFactory.CreatePizza(Category: Integer): TPizza;
26begin
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.
35end;
36
37end.
1
2{《HeadFirst设计模式》工厂模式之简单工厂 }
3{ 简单工厂的用户类 }
4{ 编译工具 :Delphi7.0 }
5{ 联系方式 :guzh-0417@163.com }
6
7unit uUserOfSimpleFactory;
8
9interface
10
11uses
12 uProducts, uSimpleFactory;
13
14type
15 { 工厂的用户 }
16 TPizzaStore = class(TObject)
17 public
18 procedure OrderPizza;
19 end;
20
21implementation
22
23{ TOrderPizza }
24
25procedure TPizzaStore.OrderPizza;
26var
27 aPizza: TPizza;
28begin
29 aPizza := TSimplePizzaFactory.CreatePizza(0);
30
31 aPizza.Prepare();
32 aPizza.Bake();
33 aPizza.Cut();
34 aPizza.Box();
35
36 aPizza.Free;
37end;
38
39end.
1
2{《HeadFirst设计模式》工厂模式之简单工厂 }
3{ 客户端 }
4{ 编译工具 :Delphi7.0 }
5{ 联系方式 :guzh-0417@163.com }
6
7program pPizzaStoreTest;
8
9{$APPTYPE CONSOLE}
10
11uses
12 uProducts in 'uProducts.pas',
13 uSimpleFactory in 'uSimpleFactory.pas',
14 uUserOfSimpleFactory in 'uUserOfSimpleFactory.pas';
15
16var
17 aPizzaStore: TPizzaStore;
18
19begin
20 aPizzaStore := TPizzaStore.Create;
21 aPizzaStore.OrderPizza;
22 aPizzaStore.Free;
23
24 Readln;
25end.
运行结果: