Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---模板方法模式之CoffeineBeverageWithHook[转]
2020-12-13 15:43
标签:des blog http io ar os sp div on 模板方法模式定义了一个算法骨架,允许子类对算法的某个或某些步骤进行重写(override)。 运行结果: Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---模板方法模式之CoffeineBeverageWithHook[转] 标签:des blog http io ar os sp div on 原文地址:http://www.cnblogs.com/0x2D-0x22/p/4076317.html![]()
1
2
{《HeadFirst设计模式》之模板方法模式 }
3
{ 编译工具: Delphi7.0 }
4
{ E-Mail : guzh-0417@163.com }
5
6
unit uCoffeineBeverageWithHook;
7
8
interface
9
10
uses
11
SysUtils;
12
13
type
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
43
implementation
44
45
{ TCoffeineBeverageWithHook }
46
47
procedure TCoffeineBeverageWithHook.BoilWater;
48
begin
49
Writeln(‘Boiling Water‘);
50
end;
51
52
function TCoffeineBeverageWithHook.CustomerWantsCondiments: Boolean;
53
begin
54
Result := True;
55
end;
56
57
procedure TCoffeineBeverageWithHook.PourInCup;
58
begin
59
Writeln(‘Poiling into cup‘);
60
end;
61
62
procedure TCoffeineBeverageWithHook.PrepareRecipe;
63
begin
64
BoilWater;
65
Brew;
66
PourInCup;
67
if CustomerWantsCondiments then
68
AddCondiments;
69
end;
70
71
{ TCoffeeWithHook }
72
73
procedure TCoffeeWithHook.AddCondiments;
74
begin
75
Writeln(‘Add Sugar and Milk‘);
76
end;
77
78
procedure TCoffeeWithHook.Brew;
79
begin
80
Writeln(‘Drip Coffee Through Filter‘);
81
end;
82
83
function TCoffeeWithHook.CustomerWantsCondiments: Boolean;
84
var
85
Answer: string;
86
begin
87
Answer := GetUserInput;
88
if LowerCase(Answer) = ‘y‘ then
89
Result := True
90
else
91
Result := False;
92
end;
93
94
function TCoffeeWithHook.GetUserInput: string;
95
var
96
Answer: string;
97
begin
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;
104
end;
105
106
{ TTeaWithHook }
107
108
procedure TTeaWithHook.AddCondiments;
109
begin
110
Writeln(‘Add Lemon‘);
111
end;
112
113
procedure TTeaWithHook.Brew;
114
begin
115
Writeln(‘Steeping the Tea‘);
116
end;
117
118
function TTeaWithHook.CustomerWantsCondiments: Boolean;
119
var
120
Answer: string;
121
begin
122
Answer := GetUserInput;
123
if LowerCase(Answer) = ‘y‘ then
124
Result := True
125
else
126
Result := False;
127
end;
128
129
function TTeaWithHook.GetUserInput: string;
130
var
131
Answer: string;
132
begin
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;
139
end;
140
141
end.
142
![]()
1
2
{《HeadFirst设计模式》之模板方法模式 }
3
{ 客户端 }
4
{ 编译工具: Delphi7.0 }
5
{ E-Mail : guzh-0417@163.com }
6
7
program pCoffeineBeverageWithHook;
8
9
{$APPTYPE CONSOLE}
10
11
uses
12
SysUtils,
13
uCoffeineBeverageWithHook in ‘uCoffeineBeverageWithHook.pas‘;
14
15
var
16
CoffeeHook: TCoffeeWithHook;
17
TeaHook : TTeaWithHook;
18
19
begin
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;
33
end.

文章标题:Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---模板方法模式之CoffeineBeverageWithHook[转]
文章链接:http://soscw.com/index.php/essay/35353.html