1
2{《HeadFirst设计模式》工厂模式之工厂方法 }
3{ 产品类 }
4{ 编译工具 :Delphi2007 for win32 }
5{ 联系方式 :guzh-0417@163.com }
6
7unit uProducts;
8
9interface
10
11type
12 { abstract Pizza }
13
14 TPizza = class abstract(TObject)
15 strict private
16 function GetName: string;
17 strict protected
18 FName : string;
19 FDough: string;
20 FSauce: string;
21 public
22 procedure Prepare;
23 procedure Bake;
24 procedure Cut; virtual;
25 procedure Box;
26 property Name: string read GetName;
27 end;
28
29 { NY Style Pizza }
30
31 TNYStyleCheesePizza = class(TPizza)
32 public
33 constructor Create;
34 end;
35
36 TNYStyleClamPizza = class(TPizza)
37 public
38 constructor Create;
39 end;
40
41 TNYStylePepperoniPizza = class(TPizza)
42 public
43 constructor Create;
44 end;
45
46 TNYStyleVeggiePizza = class(TPizza)
47 public
48 constructor Create;
49 end;
50
51 { Chicago Style Pizza }
52
53 TChicagoStyleCheesePizza = class(TPizza)
54 public
55 constructor Create;
56 procedure Cut; override;
57 end;
58
59 TChicagoStyleClamPizza = class(TPizza)
60 public
61 constructor Create;
62 procedure Cut; override;
63 end;
64
65 TChicagoStylePepperoniPizza = class(TPizza)
66 public
67 constructor Create;
68 procedure Cut; override;
69 end;
70
71 TChicagoStyleVeggiePizza = class(TPizza)
72 public
73 constructor Create;
74 procedure Cut; override;
75 end;
76
77implementation
78
79{ TPizza }
80
81procedure TPizza.Bake;
82begin
83 Writeln('Bake for 25 minutes at 350');
84end;
85
86procedure TPizza.Box;
87begin
88 Writeln('Place pizza in official PizzaStore box');
89end;
90
91procedure TPizza.Cut;
92begin
93 Writeln('Cutting the pizza into diaginal slices');
94end;
95
96function TPizza.GetName: string;
97begin
98 Result := FName;
99end;
100
101procedure TPizza.Prepare;
102begin
103 Writeln('Preparing ' + FName);
104 Writeln('Tossing dough');
105 Writeln('Adding sauce');
106 Writeln('Adding topping:');
107end;
108
109{ TNYStyleCheesePizza }
110
111constructor TNYStyleCheesePizza.Create;
112begin
113 FName := 'NY Style Sauce and Cheese Pizza';
114 FDough := 'Thin Crust Dough';
115 FSauce := 'Marinara Sauce';
116end;
117
118{ TNYStyleClamPizza }
119
120constructor TNYStyleClamPizza.Create;
121begin
122 FName := 'NY Style Clam Pizza';
123 FDough := 'Thin Crust Dough';
124 FSauce := 'Marinara Sauce';
125end;
126
127{ TNYStylePepperoniPizza }
128
129constructor TNYStylePepperoniPizza.Create;
130begin
131 FName := 'NY Style Pepperoni Pizza';
132 FDough := 'Thin Crust Dough';
133 FSauce := 'Marinara Sauce';
134end;
135
136{ TNYStyleVeggieizza }
137
138constructor TNYStyleVeggiePizza.Create;
139begin
140 FName := 'NY Style Veggie Pizza';
141 FDough := 'Thin Crust Dough';
142 FSauce := 'Marinara Sauce';
143end;
144
145{ TChicagoStyleCheesePizza }
146
147constructor TChicagoStyleCheesePizza.Create;
148begin
149 FName := 'Chicago Style Deep Dish Cheese Pizza';
150 FDough := 'Extra Thick Crust Dough';
151 FSauce := 'Plum Tomato Sauce';
152end;
153
154procedure TChicagoStyleCheesePizza.Cut;
155begin
156 Writeln('Cutting the pizza into square slices');
157end;
158
159{ TChicagoStyleClamPizza }
160
161constructor TChicagoStyleClamPizza.Create;
162begin
163 FName := 'Chicago Style Clam Pizza';
164 FDough := 'Extra Thick Crust Dough';
165 FSauce := 'Plum Tomato Sauce';
166end;
167
168procedure TChicagoStyleClamPizza.Cut;
169begin
170 Writeln('Cutting the pizza into square slices');
171end;
172
173{ TChicagoStylePepperoniPizza }
174
175constructor TChicagoStylePepperoniPizza.Create;
176begin
177 FName := 'Chicago Style Pepperoni Pizza';
178 FDough := 'Extra Thick Crust Dough';
179 FSauce := 'Plum Tomato Sauce';
180end;
181
182procedure TChicagoStylePepperoniPizza.Cut;
183begin
184 Writeln('Cutting the pizza into square slices');
185end;
186
187{ TChicagoStyleVeggiePizza }
188
189constructor TChicagoStyleVeggiePizza.Create;
190begin
191 FName := 'Chicago Style Veggie Pizza';
192 FDough := 'Extra Thick Crust Dough';
193 FSauce := 'Plum Tomato Sauce';
194end;
195
196procedure TChicagoStyleVeggiePizza.Cut;
197begin
198 Writeln('Cutting the pizza into square slices');
199end;
200
201end.
202
1
2{《HeadFirst设计模式》工厂模式之工厂方法 }
3{ 工厂类 }
4{ 编译工具 :Delphi2007 for win32 }
5{ 联系方式 :guzh-0417@163.com }
6
7unit uFactory;
8
9interface
10
11uses
12 uProducts;
13
14type
15 TPizzaStore = class abstract(TObject)
16 strict protected
17 FPizza: TPizza;
18 public
19 function OrderPizza (Category: Integer): TPizza;
20 function CreatePizza(Category: Integer): TPizza; virtual; abstract;
21 end;
22
23 TNYPizzaStore = class(TPizzaStore)
24 public
25 function CreatePizza(Category: Integer): TPizza; override;
26 destructor Destroy; override;
27 end;
28
29 TChicagoPizzaStore = class(TPizzaStore)
30 public
31 function CreatePizza(Category: Integer): TPizza; override;
32 destructor Destroy; override;
33 end;
34
35implementation
36
37{ TPizzaStore }
38
39function TPizzaStore.OrderPizza(Category: Integer): TPizza;
40begin
41 FPizza := CreatePizza(Category);
42
43 FPizza.Prepare;
44 FPizza.Bake;
45 FPizza.Cut;
46 FPizza.Box;
47
48 Result := FPizza;
49end;
50
51{ TNYPizzaStore }
52
53function TNYPizzaStore.CreatePizza(Category: Integer): TPizza;
54begin
55 case Category of
56 0: Result := TNYStyleCheesePizza.Create;
57 1: Result := TNYStyleClamPizza.Create;
58 2: Result := TNyStylePepperoniPizza.Create;
59 3: Result := TNYStyleVeggiePizza.Create;
60 else
61 Result := nil;
62 end;
63end;
64
65destructor TNYPizzaStore.Destroy;
66begin
67 FPizza.Free;
68 inherited Destroy;
69end;
70
71{ TChicagoPizzaStore }
72
73function TChicagoPizzaStore.CreatePizza(Category: Integer): TPizza;
74begin
75 case Category of
76 0: Result := TChicagoStyleCheesePizza.Create;
77 1: Result := TChicagoStyleClamPizza.Create;
78 2: Result := TChicagoStylePepperoniPizza.Create;
79 3: Result := TChicagoStyleVeggiePizza.Create;
80 else
81 Result := nil;
82 end;
83end;
84
85destructor TChicagoPizzaStore.Destroy;
86begin
87 FPizza.Free;
88 inherited Destroy;
89end;
90
91end.
92
1
2{《HeadFirst设计模式》工厂模式之工厂方法 }
3{ 客户端 }
4{ 编译工具 :Delphi2007 for win32 }
5{ 联系方式 :guzh-0417@163.com }
6
7program pPizzaStoreTest;
8
9{$APPTYPE CONSOLE}
10
11uses
12 uFactory in 'uFactory.pas',
13 uProducts in 'uProducts.pas';
14
15var
16 aNYStore: TPizzaStore;
17 aChicagoStore: TPizzaStore;
18 aPizza: TPizza;
19
20begin
21 aNYStore := TNYPizzaStore.Create;
22 aPizza := aNYStore.OrderPizza(0);
23 Writeln('Ethan ordered a ' + aPizza.Name);
24 aNYStore.Free;
25
26 Writeln;
27
28 aChicagoStore := TChicagoPizzaStore.Create;
29 aPizza := aChicagoStore.OrderPizza(0);
30 Writeln('Joel ordered a ' + aPizza.Name);
31 aChicagoStore.Free;
32
33 Readln;
34end.
35
运行结果: