1
2 {"Headfirst design mode" Factory mode Abstract Factory}
3 {Product of Abstract factory}
4 {Compilation tool: Delphi7.0}
5 {e-mail: [Email protected]}
6
7unit upizzaingredient;
8
9interface
10
11type
Tdough = Class (TObject)
The end;
14
Tthincrustdough = Class (Tdough)
The end;
17
Tthickcrustdough = Class (Tdough)
The end;
20
21////////////////////////////////
22
Tsauce = Class (TObject)
The end;
25
Tmarinarasauce = Class (Tsauce)
End;
28
Tplumtomatosauce = Class (Tsauce)
The end;
31
32////////////////////////////////
33
Tcheese = Class (TObject)
The end;
36
PNS Treggianocheese = Class (Tcheese)
The end;
39
Tmozzarellacheese = Class (Tcheese)
The end;
42
43//////////////////////////////////
44
Tpepperoni = Class (TObject)
The end;
47
Tslicedpepperoni = Class (Tpepperoni)
The end;
50
51////////////////////////////////////
52
Tclams = Class (TObject)
The end;
55
Tfreshclams = Class (Tclams)
The end;
58
Tfrozenclams = Class (Tclams)
The end;
61
62implementation
63
64end.
1
2 {"Headfirst design mode" Factory mode Abstract Factory}
3 {Abstract Factory}
4 {Compilation tool: Delphi7.0}
5 {e-mail: [Email protected]}
6
7unit upizzaingredientfactory;
8
9interface
10
11uses
Upizzaingredient;
13
14type
Tpizzaingredientfactory = Class (TObject)
Public
+ function Createdough:tdough; Virtual Abstract
function Createsauce:tsauce; Virtual Abstract
function Createcheese:tcheese; Virtual Abstract
function Createpepperoni:tpepperoni; Virtual Abstract
* Function Createclams:tclams; Virtual Abstract
The end;
23
Tnypizzaingredientfactory = Class (Tpizzaingredientfactory)
Public
-function Createdough:tdough; Override
function Createsauce:tsauce; Override
Createcheese:tcheese function; Override
function Createpepperoni:tpepperoni; Override
function Createclams:tclams; Override
The end;
32
Tchicagopizzaingredientfactory = Class (Tpizzaingredientfactory)
Public
function Createdough:tdough; Override
Createsauce:tsauce function; Override
Panax Notoginseng function Createcheese:tcheese; Override
Createpepperoni:tpepperoni function; Override
Createclams:tclams function; Override
The end;
41
42implementation
43
44{Tnypizzaingredientfactory}
45
46function TNYPizzaIngredientFactory.CreateCheese:TCheese;
47begin
The Result: = Treggianocheese.create;
49end;
50
51function TNYPizzaIngredientFactory.CreateClams:TClams;
52begin
The Result: = Tfreshclams.create;
54end;
55
56function TNYPizzaIngredientFactory.CreateDough:TDough;
57begin
The Result: = Tthincrustdough.create;
59end;
60
61function TNYPizzaIngredientFactory.CreatePepperoni:TPepperoni;
62begin
Result: = tslicedpepperoni.create;
64end;
65
66function TNYPizzaIngredientFactory.CreateSauce:TSauce;
67begin
The Result: = Tmarinarasauce.create;
69end;
70
71{Tchicagopizzaingredientfactory}
72
73function TChicagoPizzaIngredientFactory.CreateCheese:TCheese;
74begin
The Result: = Tmozzarellacheese.create;
76end;
77
78function TChicagoPizzaIngredientFactory.CreateClams:TClams;
79begin
Result: = tfrozenclams.create;
81end;
82
83function TChicagoPizzaIngredientFactory.CreateDough:TDough;
84begin
The Result: = Tthickcrustdough.create;
86end;
87
88function TChicagoPizzaIngredientFactory.CreatePepperoni:TPepperoni;
89begin
Result: = tslicedpepperoni.create;
91end;
92
93function TChicagoPizzaIngredientFactory.CreateSauce:TSauce;
94begin
The Result: = Tplumtomatosauce.create;
96end;
97
98end.
1
2 {"Headfirst design mode" Factory mode Abstract Factory}
3 {User of Abstract Factory}
4 {Compilation tool: Delphi7.0}
5 {e-mail: [Email protected]}
6
7unit Upizza;
8
9interface
10
11uses
Upizzaingredient, Upizzaingredientfactory;
13
14type
Tpizza = Class (TObject)
Private
Procedure SetName (const value:string);
Protected
fname:string;
Fdough:tdough;
Fsauce:tsauce;
Fcheese:tcheese;
Fpepperoni:tpepperoni;
Fclams:tclams;
Fingredientfactory:tpizzaingredientfactory;
Public
Constructor Create (aingredientfactory:tpizzaingredientfactory);
Procedure Prepare; Virtual Abstract
Procedure Bake;
Procedure Cut;
Procedure Box;
Property name:string read FName write SetName;
The end;
34
Tcheesepizza = Class (Tpizza)
Public
Panax Notoginseng destructor Destroy; Override
Procedure Prepare; Override
The end;
40
Tclamspizza = Class (Tpizza)
Public
destructor Destroy; Override
Procedure Prepare; Override
The end;
46
47implementation
48
49{Tpizza}
50
51procedure Tpizza.bake;
52begin
Writeln (' Bake for-minutes at 350 ');
54end;
55
56procedure Tpizza.box;
57begin
Writeln (' place pizza in official Pizzastore box ');
59end;
60
61constructor tpizza.create (aingredientfactory:tpizzaingredientfactory);
62begin
Self.fingredientfactory: = aingredientfactory;
64end;
65
66procedure Tpizza.cut;
67begin
Writeln (' cutting the pizza into diagonal slices ');
69end;
70
71procedure tpizza.setname (const value:string);
72begin
FName: = Value;
74end;
75
76{Tcheesepizza}
77
78destructor Tcheesepizza.destroy;
79begin
Fdough.free;
Bayi Fsauce.free;
Fcheese.free;
inherited;
84end;
85
86procedure Tcheesepizza.prepare;
87begin
Writeln (' Preparing ' + Name);
Fdough: = Fingredientfactory.createdough;
Fsauce: = Fingredientfactory.createsauce;
Fcheese: = Fingredientfactory.createcheese;
92end;
93
94{Tclamspizza}
95
96destructor Tclamspizza.destroy;
97begin
98 Fdough.free;
Fsauce.free;
Fcheese.free;
101 Fclams.free;
102 inherited;
103end;
104
105procedure Tclamspizza.prepare;
106begin
107 Writeln (' Preparing ' + Name);
108 Fdough: = Fingredientfactory.createdough;
109 Fsauce: = Fingredientfactory.createsauce;
Fcheese: = Fingredientfactory.createcheese;
111 Fclams: = Fingredientfactory.createclams;
112end;
113
114end.
1
2 {"Headfirst design mode" Factory mode Abstract Factory}
3 {Pizza User}
4 {Compilation tool: Delphi7.0}
5 {e-mail: [Email protected]}
6
7unit Upizzastore;
8
9interface
10
11uses
Upizza, Upizzaingredientfactory;
13
14type
Tpizzastore = Class (TObject)
Protected
Fpizza:tpizza;
Fingredientfactory:tpizzaingredientfactory;
Public
destructor Destroy; Override
-Function Orderpizza (Category:integer): Tpizza;
function Createpizza (Style:integer): Tpizza; Virtual Abstract
The end;
24
Tnypizzastore = Class (Tpizzastore)
Public
function Createpizza (Style:integer): Tpizza; Override
The end;
29
Tchicagopizzastore = Class (Tpizzastore)
to public
function Createpizza (Style:integer): Tpizza; Override
The end;
34
35implementation
36
37{Tpizzastore}
38
39destructor Tpizzastore.destroy;
40begin
Fpizza.free;
Fingredientfactory.free;
inherited;
44end;
45
46function Tpizzastore.orderpizza (Category:integer): Tpizza;
47begin
Fpizza: = Createpizza (Category);
Fpizza.prepare;
Fpizza.bake;
Wuyi Fpizza.cut;
Fpizza.box;
53
The Result: = Fpizza;
55end;
56
57{Tnypizzastore}
58
59function Tnypizzastore.createpizza (Style:integer): Tpizza;
60begin
Fingredientfactory: = tnypizzaingredientfactory.create;
Fpizza: = nil;
The case Style of
0:begin
Fpizza: = Tcheesepizza.create (fingredientfactory);
Fpizza.name: = ' New York Style Cheese Pizza ';
The end;
1:begin
Fpizza: = Tclamspizza.create (fingredientfactory);
Fpizza.name: = ' New York Style clams Pizza ';
The end;
72//2: Other flavors of New York Pizza
73//
The end;
The Result: = Fpizza;
76end;
77
78{Tchicagopizzastore}
79
80function Tchicagopizzastore.createpizza (Style:integer): Tpizza;
81begin
Fingredientfactory: = tchicagopizzaingredientfactory.create;
The case Style of
0:begin
The Result: = Tcheesepizza.create (fingredientfactory);
Result.name: = ' Chicago Style Cheese Pizza ';
The end;
1:begin
The Result: = Tclamspizza.create (fingredientfactory);
Result.name: = ' Chicago Style clams Pizza ';
The end;
92//2: Other flavors of Chicago Pizza
93//
94 Else
The Result: = nil;
The end;
97end;
98
99end.
1
2 {"Headfirst design mode" Factory mode Abstract Factory}
3 {Client}
4 {Compilation tool: Delphi7.0}
5 {e-mail: [Email protected]}
6
7program Ppizzastore;
8
9{$APPTYPE CONSOLE}
10
11uses
Upizzaingredient in ' Upizzaingredient.pas ',
Upizzaingredientfactory in ' Upizzaingredientfactory.pas ',
Upizza in ' Upizza.pas ',
Upizzastore in ' Upizzastore.pas ';
16
17var
Pizzastore:tpizzastore;
Pizza:tpizza;
20
21begin
Pizzastore: = tnypizzastore.create;
Pizza: = Pizzastore.orderpizza (0);
Writeln (' Ethan ordered a ' + pizza.name);
Writeln;
Pizza: = Pizzastore.orderpizza (1);
Writeln (' Ethan ordered a ' + pizza.name);
Pizzastore.free;
29
Writeln;
Writeln (' ********************************** ');
Writeln;
33
Pizzastore: = tchicagopizzastore.create;
Pizza: = Pizzastore.orderpizza (0);
Writeln (' Joel ordered a ' + pizza.name);
Panax Notoginseng Writeln;
Pizza: = Pizzastore.orderpizza (1);
Writeln (' Joel ordered a ' + pizza.name);
Pizzastore.free;
41
READLN;
43end.
Operation Result:
Delphi design mode: "Headfirst design Mode" Delphi Code---Factory mode abstract factory [go]