Delphi design mode: "Headfirst design mode" DELPHI2007 Code---Factory method of plant mode [go]

Source: Internet
Author: User


1
2{factory method of "Headfirst design mode" Factory mode}
3{Product Class}
4{compiler tool: Delphi2007 for Win32}
5{Contact information: [Email protected]}
6
7unit uproducts;
8
9interface
10
11type
{Abstract Pizza}
13
Tpizza = Class abstract (TObject)
Strict private
function getname:string;
Strict protected
fname:string;
fdough:string;
fsauce:string;
Public
Procedure Prepare;
Procedure Bake;
Procedure Cut; Virtual
Procedure Box;
Name:string read GetName;
End;
28
{NY Style Pizza}
30
Tnystylecheesepizza = Class (Tpizza)
Public
Constructor Create;
The end;
35
Tnystyleclampizza = Class (Tpizza)
Notoginseng Public
Constructor Create;
The end;
40
Tnystylepepperonipizza = Class (Tpizza)
Public
Constructor Create;
The end;
45
Tnystyleveggiepizza = Class (Tpizza)
Public
Constructor Create;
The end;
50
Wuyi {Chicago Style Pizza}
52
Tchicagostylecheesepizza = Class (Tpizza)
Public
Constructor Create;
Procedure Cut; Override
The end;
58
Tchicagostyleclampizza = Class (Tpizza)
Public
Constructor Create;
Procedure Cut; Override
The end;
64
Tchicagostylepepperonipizza = Class (Tpizza)
Public
Constructor Create;
Procedure Cut; Override
The end;
70
Tchicagostyleveggiepizza = Class (Tpizza)
Public
Constructor Create;
Procedure Cut; Override
The end;
76
77implementation
78
79{Tpizza}
80
81procedure Tpizza.bake;
82begin
Writeln (' Bake for the Minutes at 350 ');
84end;
85
86procedure Tpizza.box;
87begin
Writeln (' place pizza in official Pizzastore box ');
89end;
90
91procedure Tpizza.cut;
92begin
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 ');
Writeln (' Adding sauce ');
106 Writeln (' Adding topping: ');
107end;
108
109{Tnystylecheesepizza}
110
111constructor tnystylecheesepizza.create;
112begin
113 FName: = ' NY Style Sauce and Cheese Pizza ';
Fdough: = ' Thin crust dough ';
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 ';
Fdough: = ' Thin crust dough ';
133 Fsauce: = ' marinara Sauce ';
134end;
135
136{Tnystyleveggieizza}
137
138constructor tnystyleveggiepizza.create;
139begin
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 ';
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
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{factory method of "Headfirst design mode" Factory mode}
3{Factory Class}
4{compiler tool: Delphi2007 for Win32}
5{Contact information: [Email protected]}
6
7unit ufactory;
8
9interface
10
11uses
uproducts;
13
14type
Tpizzastore = Class abstract (TObject)
Strict protected
Fpizza:tpizza;
Public
function Orderpizza (Category:integer): Tpizza;
function Createpizza (Category:integer): Tpizza; Virtual Abstract
The end;
22
Tnypizzastore = Class (Tpizzastore)
Public
function Createpizza (Category:integer): Tpizza; Override
destructor Destroy; Override
End;
28
Tchicagopizzastore = Class (Tpizzastore)
Public
-Function Createpizza (Category:integer): Tpizza; Override
destructor Destroy; Override
The end;
34
35implementation
36
37{Tpizzastore}
38
39function Tpizzastore.orderpizza (Category:integer): Tpizza;
40begin
Fpizza: = Createpizza (Category);
42
Fpizza.prepare;
Fpizza.bake;
Fpizza.cut;
Fpizza.box;
47
The Result: = Fpizza;
49end;
50
51{Tnypizzastore}
52
53function Tnypizzastore.createpizza (Category:integer): Tpizza;
54begin
Case Category of
0:result: = tnystylecheesepizza.create;
1:result: = tnystyleclampizza.create;
2:result: = tnystylepepperonipizza.create;
3:result: = tnystyleveggiepizza.create;
All else
The Result: = nil;
The end;
63end;
64
65destructor Tnypizzastore.destroy;
66begin
Fpizza.free;
Inherited Destroy;
69end;
70
71{Tchicagopizzastore}
72
73function Tchicagopizzastore.createpizza (Category:integer): Tpizza;
74begin
The case Category of
0:result: = tchicagostylecheesepizza.create;
1:result: = tchicagostyleclampizza.create;
2:result: = tchicagostylepepperonipizza.create;
3:result: = tchicagostyleveggiepizza.create;
+ Else
Bayi Result: = nil;
The end;
83end;
84
85destructor Tchicagopizzastore.destroy;
86begin
Fpizza.free;
Inherited Destroy;
89end;
90
91end.
92

1
2{factory method of "Headfirst design mode" Factory mode}
3{Client}
4{compiler tool: Delphi2007 for Win32}
5{Contact information: [Email protected]}
6
7program ppizzastoretest;
8
9{$APPTYPE CONSOLE}
10
11uses
Ufactory in ' Ufactory.pas ',
Uproducts in ' Uproducts.pas ';
14
15var
Anystore:tpizzastore;
Achicagostore:tpizzastore;
Apizza:tpizza;
19
20begin
Anystore: = tnypizzastore.create;
Apizza: = Anystore.orderpizza (0);
Writeln (' Ethan ordered a ' + apizza.name);
Anystore.free;
25
Writeln;
27
Achicagostore: = tchicagopizzastore.create;
Apizza: = Achicagostore.orderpizza (0);
Writeln (' Joel ordered a ' + apizza.name);
Achicagostore.free;
32
READLN;
34end.
35
Operation Result:

Delphi design mode: "Headfirst design mode" DELPHI2007 Code---Factory method of plant mode [go]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.