1
2 {《HeadFirst设计模式》工厂模式之抽象工厂 }
3 { 抽象工厂的产品 }
4 { 编译工具:Delphi7.0 }
5 { E-Mail :guzh-0417@163.com }
6
7unit uPizzaIngredient;
8
9interface
10
11type
12 TDough = class(TObject)
13 end;
14
15 TThinCrustDough = class(TDough)
16 end;
17
18 TThickCrustDough = class(TDough)
19 end;
20
21 ////////////////////////////////
22
23 TSauce = class(TObject)
24 end;
25
26 TMarinaraSauce = class(TSauce)
27 end;
28
29 TPlumTomatoSauce = class(TSauce)
30 end;
31
32 ////////////////////////////////
33
34 TCheese = class(TObject)
35 end;
36
37 TReggianoCheese = class(TCheese)
38 end;
39
40 TMozzarellaCheese = class(TCheese)
41 end;
42
43 //////////////////////////////////
44
45 TPepperoni = class(TObject)
46 end;
47
48 TSlicedPepperoni = class(TPepperoni)
49 end;
50
51 ////////////////////////////////////
52
53 TClams = class(TObject)
54 end;
55
56 TFreshClams = class(TClams)
57 end;
58
59 TFrozenClams = class(TClams)
60 end;
61
62implementation
63
64end.
1
2 {《HeadFirst设计模式》工厂模式之抽象工厂 }
3 { 抽象工厂 }
4 { 编译工具:Delphi7.0 }
5 { E-Mail :guzh-0417@163.com }
6
7unit uPizzaIngredientFactory;
8
9interface
10
11uses
12 uPizzaIngredient;
13
14type
15 TPizzaIngredientFactory = class(TObject)
16 public
17 function CreateDough: TDough; virtual; abstract;
18 function CreateSauce: TSauce; virtual; abstract;
19 function CreateCheese: TCheese; virtual; abstract;
20 function CreatePepperoni: TPepperoni; virtual; abstract;
21 function CreateClams: TClams; virtual; abstract;
22 end;
23
24 TNYPizzaIngredientFactory = class(TPizzaIngredientFactory)
25 public
26 function CreateDough: TDough; override;
27 function CreateSauce: TSauce; override;
28 function CreateCheese: TCheese; override;
29 function CreatePepperoni: TPepperoni; override;
30 function CreateClams: TClams; override;
31 end;
32
33 TChicagoPizzaIngredientFactory = class(TPizzaIngredientFactory)
34 public
35 function CreateDough: TDough; override;
36 function CreateSauce: TSauce; override;
37 function CreateCheese: TCheese; override;
38 function CreatePepperoni: TPepperoni; override;
39 function CreateClams: TClams; override;
40 end;
41
42implementation
43
44{ TNYPizzaIngredientFactory }
45
46function TNYPizzaIngredientFactory.CreateCheese: TCheese;
47begin
48 Result := TReggianoCheese.Create;
49end;
50
51function TNYPizzaIngredientFactory.CreateClams: TClams;
52begin
53 Result := TFreshClams.Create;
54end;
55
56function TNYPizzaIngredientFactory.CreateDough: TDough;
57begin
58 Result := TThinCrustDough.Create;
59end;
60
61function TNYPizzaIngredientFactory.CreatePepperoni: TPepperoni;
62begin
63 Result := TSlicedPepperoni.Create;
64end;
65
66function TNYPizzaIngredientFactory.CreateSauce: TSauce;
67begin
68 Result := TMarinaraSauce.Create;
69end;
70
71{ TChicagoPizzaIngredientFactory }
72
73function TChicagoPizzaIngredientFactory.CreateCheese: TCheese;
74begin
75 Result := TMozzarellaCheese.Create;
76end;
77
78function TChicagoPizzaIngredientFactory.CreateClams: TClams;
79begin
80 Result := TFrozenClams.Create;
81end;
82
83function TChicagoPizzaIngredientFactory.CreateDough: TDough;
84begin
85 Result := TThickCrustDough.Create;
86end;
87
88function TChicagoPizzaIngredientFactory.CreatePepperoni: TPepperoni;
89begin
90 Result := TSlicedPepperoni.Create;
91end;
92
93function TChicagoPizzaIngredientFactory.CreateSauce: TSauce;
94begin
95 Result := TPlumTomatoSauce.Create;
96end;
97
98end.
1
2 {《HeadFirst设计模式》工厂模式之抽象工厂 }
3 { 抽象工厂的用户 }
4 { 编译工具:Delphi7.0 }
5 { E-Mail :guzh-0417@163.com }
6
7unit uPizza;
8
9interface
10
11uses
12 uPizzaIngredient, uPizzaIngredientFactory;
13
14type
15 TPizza = class(TObject)
16 private
17 procedure SetName(const Value: string);
18 protected
19 FName: string;
20 FDough: TDough;
21 FSauce: TSauce;
22 FCheese: TCheese;
23 FPepperoni: TPepperoni;
24 FClams: TClams;
25 FIngredientFactory: TPizzaIngredientFactory;
26 public
27 constructor Create(aIngredientFactory: TPizzaIngredientFactory);
28 procedure Prepare; virtual; abstract;
29 procedure Bake;
30 procedure Cut;
31 procedure Box;
32 property Name: string read FName write SetName;
33 end;
34
35 TCheesePizza = class(TPizza)
36 public
37 destructor Destroy; override;
38 procedure Prepare; override;
39 end;
40
41 TClamsPizza = class(TPizza)
42 public
43 destructor Destroy; override;
44 procedure Prepare; override;
45 end;
46
47implementation
48
49{ TPizza }
50
51procedure TPizza.Bake;
52begin
53 Writeln('Bake for 25 minutes at 350');
54end;
55
56procedure TPizza.Box;
57begin
58 Writeln('Place pizza in official PizzaStore box');
59end;
60
61constructor TPizza.Create(aIngredientFactory: TPizzaIngredientFactory);
62begin
63 Self.FIngredientFactory := aIngredientFactory;
64end;
65
66procedure TPizza.Cut;
67begin
68 Writeln('Cutting the pizza into diagonal slices');
69end;
70
71procedure TPizza.SetName(const Value: string);
72begin
73 FName := Value;
74end;
75
76{ TCheesePizza }
77
78destructor TCheesePizza.Destroy;
79begin
80 FDough.Free;
81 FSauce.Free;
82 FCheese.Free;
83 inherited;
84end;
85
86procedure TCheesePizza.Prepare;
87begin
88 Writeln('Preparing ' + Name);
89 FDough := FIngredientFactory.CreateDough;
90 FSauce := FIngredientFactory.CreateSauce;
91 FCheese := FIngredientFactory.CreateCheese;
92end;
93
94{ TClamsPizza }
95
96destructor TClamsPizza.Destroy;
97begin
98 FDough.Free;
99 FSauce.Free;
100 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;
110 FCheese := FIngredientFactory.CreateCheese;
111 FClams := FIngredientFactory.CreateClams;
112end;
113
114end.
1
2 {《HeadFirst设计模式》工厂模式之抽象工厂 }
3 { Pizza的用户 }
4 { 编译工具:Delphi7.0 }
5 { E-Mail :guzh-0417@163.com }
6
7unit uPizzaStore;
8
9interface
10
11uses
12 uPizza, uPizzaIngredientFactory;
13
14type
15 TPizzaStore = class(TObject)
16 protected
17 FPizza: TPizza;
18 FIngredientFactory: TPizzaIngredientFactory;
19 public
20 destructor Destroy; override;
21 function OrderPizza(Category: Integer): TPizza;
22 function CreatePizza(Style: Integer): TPizza; virtual; abstract;
23 end;
24
25 TNYPizzaStore = class(TPizzaStore)
26 public
27 function CreatePizza(Style: Integer): TPizza; override;
28 end;
29
30 TChicagoPizzaStore = class(TPizzaStore)
31 public
32 function CreatePizza(Style: Integer): TPizza; override;
33 end;
34
35implementation
36
37{ TPizzaStore }
38
39destructor TPizzaStore.Destroy;
40begin
41 FPizza.Free;
42 FIngredientFactory.Free;
43 inherited;
44end;
45
46function TPizzaStore.OrderPizza(Category: Integer): TPizza;
47begin
48 FPizza := CreatePizza(Category);
49 FPizza.Prepare;
50 FPizza.Bake;
51 FPizza.Cut;
52 FPizza.Box;
53
54 Result := FPizza;
55end;
56
57{ TNYPizzaStore }
58
59function TNYPizzaStore.CreatePizza(Style: Integer): TPizza;
60begin
61 FIngredientFactory := TNYPizzaIngredientFactory.Create;
62 FPizza := nil;
63 case Style of
64 0: begin
65 FPizza := TCheesePizza.Create(FIngredientFactory);
66 FPizza.Name := 'New York Style Cheese Pizza';
67 end;
68 1: begin
69 FPizza := TClamsPizza.Create(FIngredientFactory);
70 FPizza.Name := 'New York Style Clams Pizza';
71 end;
72 //2: 其它风味的 New York Pizza
73 //
74 end;
75 Result := FPizza;
76end;
77
78{ TChicagoPizzaStore }
79
80function TChicagoPizzaStore.CreatePizza(Style: Integer): TPizza;
81begin
82 FIngredientFactory := TChicagoPizzaIngredientFactory.Create;
83 case Style of
84 0: begin
85 Result := TCheesePizza.Create(FIngredientFactory);
86 Result.Name := 'Chicago Style Cheese Pizza';
87 end;
88 1: begin
89 Result := TClamsPizza.Create(FIngredientFactory);
90 Result.Name := 'Chicago Style Clams Pizza';
91 end;
92 //2: 其它风味的 Chicago Pizza
93 //
94 else
95 Result := nil;
96 end;
97end;
98
99end.
1
2 {《HeadFirst设计模式》工厂模式之抽象工厂 }
3 { 客户端 }
4 { 编译工具:Delphi7.0 }
5 { E-Mail :guzh-0417@163.com }
6
7program pPizzaStore;
8
9{$APPTYPE CONSOLE}
10
11uses
12 uPizzaIngredient in 'uPizzaIngredient.pas',
13 uPizzaIngredientFactory in 'uPizzaIngredientFactory.pas',
14 uPizza in 'uPizza.pas',
15 uPizzaStore in 'uPizzaStore.pas';
16
17var
18 PizzaStore: TPizzaStore;
19 Pizza: TPizza;
20
21begin
22 PizzaStore := TNYPizzaStore.Create;
23 Pizza := PizzaStore.OrderPizza(0);
24 Writeln('Ethan ordered a ' + Pizza.Name);
25 Writeln;
26 Pizza := PizzaStore.OrderPizza(1);
27 Writeln('Ethan ordered a ' + Pizza.Name);
28 PizzaStore.Free;
29
30 Writeln;
31 Writeln(' ********************************** ');
32 Writeln;
33
34 PizzaStore := TChicagoPizzaStore.Create;
35 Pizza := PizzaStore.OrderPizza(0);
36 Writeln('Joel ordered a ' + Pizza.Name);
37 Writeln;
38 Pizza := PizzaStore.OrderPizza(1);
39 Writeln('Joel ordered a ' + Pizza.Name);
40 PizzaStore.Free;
41
42 Readln;
43end.
运行结果: