Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---模板方法模式之CoffeineBeverageWithHook
Posted on 2009-07-17 19:37 都市牧羊 阅读(579) 评论(0) 编辑 收藏 举报模板方法模式定义了一个算法骨架,允许子类对算法的某个或某些步骤进行重写(override)。
CoffeineBeverageWithHook
1
2{《HeadFirst设计模式》之模板方法模式 }
3{ 编译工具: Delphi7.0 }
4{ E-Mail : guzh-0417@163.com }
5
6unit uCoffeineBeverageWithHook;
7
8interface
9
10uses
11 SysUtils;
12
13type
14 TCoffeineBeverageWithHook = class(TObject)
15 protected
16 procedure BoilWater;
17 procedure Brew; virtual; abstract;
18 procedure PourInCup;
19 procedure AddCondiments; virtual; abstract;
20 function CustomerWantsCondiments: Boolean; virtual; { 钩子 }
21 public
22 procedure PrepareRecipe; { 模板方法 }
23 end;
24
25 TCoffeeWithHook = class(TCoffeineBeverageWithHook)
26 private
27 function GetUserInput: string;
28 public
29 procedure Brew; override;
30 procedure AddCondiments; override;
31 function CustomerWantsCondiments: Boolean; override;
32 end;
33
34 TTeaWithHook = class(TCoffeineBeverageWithHook)
35 private
36 function GetUserInput: string;
37 public
38 procedure Brew; override;
39 procedure AddCondiments; override;
40 function CustomerWantsCondiments: Boolean; override;
41 end;
42
43implementation
44
45{ TCoffeineBeverageWithHook }
46
47procedure TCoffeineBeverageWithHook.BoilWater;
48begin
49 Writeln('Boiling Water');
50end;
51
52function TCoffeineBeverageWithHook.CustomerWantsCondiments: Boolean;
53begin
54 Result := True;
55end;
56
57procedure TCoffeineBeverageWithHook.PourInCup;
58begin
59 Writeln('Poiling into cup');
60end;
61
62procedure TCoffeineBeverageWithHook.PrepareRecipe;
63begin
64 BoilWater;
65 Brew;
66 PourInCup;
67 if CustomerWantsCondiments then
68 AddCondiments;
69end;
70
71{ TCoffeeWithHook }
72
73procedure TCoffeeWithHook.AddCondiments;
74begin
75 Writeln('Add Sugar and Milk');
76end;
77
78procedure TCoffeeWithHook.Brew;
79begin
80 Writeln('Drip Coffee Through Filter');
81end;
82
83function TCoffeeWithHook.CustomerWantsCondiments: Boolean;
84var
85 Answer: string;
86begin
87 Answer := GetUserInput;
88 if LowerCase(Answer) = 'y' then
89 Result := True
90 else
91 Result := False;
92end;
93
94function TCoffeeWithHook.GetUserInput: string;
95var
96 Answer: string;
97begin
98 Answer := '';
99 Writeln('Would You Like Milk And Sugar With Your Coffee (y / n)? ');
100 Readln(Answer);;
101 if Answer = '' then
102 Result := 'no';
103 Result := Answer;
104end;
105
106{ TTeaWithHook }
107
108procedure TTeaWithHook.AddCondiments;
109begin
110 Writeln('Add Lemon');
111end;
112
113procedure TTeaWithHook.Brew;
114begin
115 Writeln('Steeping the Tea');
116end;
117
118function TTeaWithHook.CustomerWantsCondiments: Boolean;
119var
120 Answer: string;
121begin
122 Answer := GetUserInput;
123 if LowerCase(Answer) = 'y' then
124 Result := True
125 else
126 Result := False;
127end;
128
129function TTeaWithHook.GetUserInput: string;
130var
131 Answer: string;
132begin
133 Answer := '';
134 Writeln('Would You Like Lemon With Your Tea (y / n)? ');
135 Readln(Answer);
136 if Answer = '' then
137 Result := 'no';
138 Result := Answer;
139end;
140
141end.
142
1
2{《HeadFirst设计模式》之模板方法模式 }
3{ 编译工具: Delphi7.0 }
4{ E-Mail : guzh-0417@163.com }
5
6unit uCoffeineBeverageWithHook;
7
8interface
9
10uses
11 SysUtils;
12
13type
14 TCoffeineBeverageWithHook = class(TObject)
15 protected
16 procedure BoilWater;
17 procedure Brew; virtual; abstract;
18 procedure PourInCup;
19 procedure AddCondiments; virtual; abstract;
20 function CustomerWantsCondiments: Boolean; virtual; { 钩子 }
21 public
22 procedure PrepareRecipe; { 模板方法 }
23 end;
24
25 TCoffeeWithHook = class(TCoffeineBeverageWithHook)
26 private
27 function GetUserInput: string;
28 public
29 procedure Brew; override;
30 procedure AddCondiments; override;
31 function CustomerWantsCondiments: Boolean; override;
32 end;
33
34 TTeaWithHook = class(TCoffeineBeverageWithHook)
35 private
36 function GetUserInput: string;
37 public
38 procedure Brew; override;
39 procedure AddCondiments; override;
40 function CustomerWantsCondiments: Boolean; override;
41 end;
42
43implementation
44
45{ TCoffeineBeverageWithHook }
46
47procedure TCoffeineBeverageWithHook.BoilWater;
48begin
49 Writeln('Boiling Water');
50end;
51
52function TCoffeineBeverageWithHook.CustomerWantsCondiments: Boolean;
53begin
54 Result := True;
55end;
56
57procedure TCoffeineBeverageWithHook.PourInCup;
58begin
59 Writeln('Poiling into cup');
60end;
61
62procedure TCoffeineBeverageWithHook.PrepareRecipe;
63begin
64 BoilWater;
65 Brew;
66 PourInCup;
67 if CustomerWantsCondiments then
68 AddCondiments;
69end;
70
71{ TCoffeeWithHook }
72
73procedure TCoffeeWithHook.AddCondiments;
74begin
75 Writeln('Add Sugar and Milk');
76end;
77
78procedure TCoffeeWithHook.Brew;
79begin
80 Writeln('Drip Coffee Through Filter');
81end;
82
83function TCoffeeWithHook.CustomerWantsCondiments: Boolean;
84var
85 Answer: string;
86begin
87 Answer := GetUserInput;
88 if LowerCase(Answer) = 'y' then
89 Result := True
90 else
91 Result := False;
92end;
93
94function TCoffeeWithHook.GetUserInput: string;
95var
96 Answer: string;
97begin
98 Answer := '';
99 Writeln('Would You Like Milk And Sugar With Your Coffee (y / n)? ');
100 Readln(Answer);;
101 if Answer = '' then
102 Result := 'no';
103 Result := Answer;
104end;
105
106{ TTeaWithHook }
107
108procedure TTeaWithHook.AddCondiments;
109begin
110 Writeln('Add Lemon');
111end;
112
113procedure TTeaWithHook.Brew;
114begin
115 Writeln('Steeping the Tea');
116end;
117
118function TTeaWithHook.CustomerWantsCondiments: Boolean;
119var
120 Answer: string;
121begin
122 Answer := GetUserInput;
123 if LowerCase(Answer) = 'y' then
124 Result := True
125 else
126 Result := False;
127end;
128
129function TTeaWithHook.GetUserInput: string;
130var
131 Answer: string;
132begin
133 Answer := '';
134 Writeln('Would You Like Lemon With Your Tea (y / n)? ');
135 Readln(Answer);
136 if Answer = '' then
137 Result := 'no';
138 Result := Answer;
139end;
140
141end.
142
客户端
1
2{《HeadFirst设计模式》之模板方法模式 }
3{ 客户端 }
4{ 编译工具: Delphi7.0 }
5{ E-Mail : guzh-0417@163.com }
6
7program pCoffeineBeverageWithHook;
8
9{$APPTYPE CONSOLE}
10
11uses
12 SysUtils,
13 uCoffeineBeverageWithHook in 'uCoffeineBeverageWithHook.pas';
14
15var
16 CoffeeHook: TCoffeeWithHook;
17 TeaHook : TTeaWithHook;
18
19begin
20 CoffeeHook := TCoffeeWithHook.Create;
21 TeaHook := TTeaWithHook.Create;
22
23 Writeln('Making Coffee');
24 CoffeeHook.PrepareRecipe;
25
26 Writeln('Making Tea');
27 TeaHook.PrepareRecipe;
28
29 FreeAndNil(CoffeeHook);
30 FreeAndNil(TeaHook);
31
32 Readln;
33end.
1
2{《HeadFirst设计模式》之模板方法模式 }
3{ 客户端 }
4{ 编译工具: Delphi7.0 }
5{ E-Mail : guzh-0417@163.com }
6
7program pCoffeineBeverageWithHook;
8
9{$APPTYPE CONSOLE}
10
11uses
12 SysUtils,
13 uCoffeineBeverageWithHook in 'uCoffeineBeverageWithHook.pas';
14
15var
16 CoffeeHook: TCoffeeWithHook;
17 TeaHook : TTeaWithHook;
18
19begin
20 CoffeeHook := TCoffeeWithHook.Create;
21 TeaHook := TTeaWithHook.Create;
22
23 Writeln('Making Coffee');
24 CoffeeHook.PrepareRecipe;
25
26 Writeln('Making Tea');
27 TeaHook.PrepareRecipe;
28
29 FreeAndNil(CoffeeHook);
30 FreeAndNil(TeaHook);
31
32 Readln;
33end.
运行结果: