Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---模板方法模式之CoffeineBeverageWithHook[转]

2020-12-13 15:43

阅读:592

标签:des   blog   http   io   ar   os   sp   div   on   

  模板方法模式定义了一个算法骨架,允许子类对算法的某个或某些步骤进行重写(override)。

 

soscw.com,搜素材
  1soscw.com,搜素材
  2soscw.com,搜素材{《HeadFirst设计模式》之模板方法模式 }
  3soscw.com,搜素材{ 编译工具: Delphi7.0              }
  4soscw.com,搜素材{ E-Mail : guzh-0417@163.com      }
  5soscw.com,搜素材
  6soscw.com,搜素材unit uCoffeineBeverageWithHook;
  7soscw.com,搜素材
  8soscw.com,搜素材interface
  9soscw.com,搜素材
 10soscw.com,搜素材uses
 11soscw.com,搜素材  SysUtils;
 12soscw.com,搜素材
 13soscw.com,搜素材type
 14soscw.com,搜素材  TCoffeineBeverageWithHook = class(TObject)
 15soscw.com,搜素材  protected
 16soscw.com,搜素材    procedure BoilWater;
 17soscw.com,搜素材    procedure Brew; virtual; abstract;
 18soscw.com,搜素材    procedure PourInCup;
 19soscw.com,搜素材    procedure AddCondiments; virtual; abstract;
 20soscw.com,搜素材    function CustomerWantsCondiments: Boolean; virtual; { 钩子 }
 21soscw.com,搜素材  public
 22soscw.com,搜素材    procedure PrepareRecipe; { 模板方法 }
 23soscw.com,搜素材  end;
 24soscw.com,搜素材
 25soscw.com,搜素材  TCoffeeWithHook = class(TCoffeineBeverageWithHook)
 26soscw.com,搜素材  private
 27soscw.com,搜素材    function GetUserInput: string;
 28soscw.com,搜素材  public
 29soscw.com,搜素材    procedure Brew; override;
 30soscw.com,搜素材    procedure AddCondiments; override;
 31soscw.com,搜素材    function CustomerWantsCondiments: Boolean; override;
 32soscw.com,搜素材  end;
 33soscw.com,搜素材
 34soscw.com,搜素材  TTeaWithHook = class(TCoffeineBeverageWithHook)
 35soscw.com,搜素材  private
 36soscw.com,搜素材    function GetUserInput: string;
 37soscw.com,搜素材  public
 38soscw.com,搜素材    procedure Brew; override;
 39soscw.com,搜素材    procedure AddCondiments; override;
 40soscw.com,搜素材    function CustomerWantsCondiments: Boolean; override;
 41soscw.com,搜素材  end;
 42soscw.com,搜素材
 43soscw.com,搜素材implementation
 44soscw.com,搜素材
 45soscw.com,搜素材{ TCoffeineBeverageWithHook }
 46soscw.com,搜素材
 47soscw.com,搜素材procedure TCoffeineBeverageWithHook.BoilWater;
 48soscw.com,搜素材begin
 49soscw.com,搜素材  Writeln(‘Boiling Water‘);
 50soscw.com,搜素材end;
 51soscw.com,搜素材
 52soscw.com,搜素材function TCoffeineBeverageWithHook.CustomerWantsCondiments: Boolean;
 53soscw.com,搜素材begin
 54soscw.com,搜素材  Result := True;
 55soscw.com,搜素材end;
 56soscw.com,搜素材
 57soscw.com,搜素材procedure TCoffeineBeverageWithHook.PourInCup;
 58soscw.com,搜素材begin
 59soscw.com,搜素材  Writeln(‘Poiling into cup‘);
 60soscw.com,搜素材end;
 61soscw.com,搜素材
 62soscw.com,搜素材procedure TCoffeineBeverageWithHook.PrepareRecipe;
 63soscw.com,搜素材begin
 64soscw.com,搜素材  BoilWater;
 65soscw.com,搜素材  Brew;
 66soscw.com,搜素材  PourInCup;
 67soscw.com,搜素材  if CustomerWantsCondiments then
 68soscw.com,搜素材    AddCondiments;
 69soscw.com,搜素材end;
 70soscw.com,搜素材
 71soscw.com,搜素材{ TCoffeeWithHook }
 72soscw.com,搜素材
 73soscw.com,搜素材procedure TCoffeeWithHook.AddCondiments;
 74soscw.com,搜素材begin
 75soscw.com,搜素材  Writeln(‘Add Sugar and Milk‘);
 76soscw.com,搜素材end;
 77soscw.com,搜素材
 78soscw.com,搜素材procedure TCoffeeWithHook.Brew;
 79soscw.com,搜素材begin
 80soscw.com,搜素材  Writeln(‘Drip Coffee Through Filter‘);
 81soscw.com,搜素材end;
 82soscw.com,搜素材
 83soscw.com,搜素材function TCoffeeWithHook.CustomerWantsCondiments: Boolean;
 84soscw.com,搜素材var
 85soscw.com,搜素材  Answer: string;
 86soscw.com,搜素材begin
 87soscw.com,搜素材  Answer := GetUserInput;
 88soscw.com,搜素材  if LowerCase(Answer) = ‘y‘ then
 89soscw.com,搜素材    Result := True
 90soscw.com,搜素材  else
 91soscw.com,搜素材    Result := False;
 92soscw.com,搜素材end;
 93soscw.com,搜素材
 94soscw.com,搜素材function TCoffeeWithHook.GetUserInput: string;
 95soscw.com,搜素材var
 96soscw.com,搜素材  Answer: string;
 97soscw.com,搜素材begin
 98soscw.com,搜素材  Answer := ‘‘;
 99soscw.com,搜素材  Writeln(‘Would You Like Milk And Sugar With Your Coffee (y / n)? ‘);
100soscw.com,搜素材  Readln(Answer);;
101soscw.com,搜素材  if Answer = ‘‘ then
102soscw.com,搜素材    Result := ‘no‘;
103soscw.com,搜素材  Result := Answer;
104soscw.com,搜素材end;
105soscw.com,搜素材
106soscw.com,搜素材{ TTeaWithHook }
107soscw.com,搜素材
108soscw.com,搜素材procedure TTeaWithHook.AddCondiments;
109soscw.com,搜素材begin
110soscw.com,搜素材  Writeln(‘Add Lemon‘);
111soscw.com,搜素材end;
112soscw.com,搜素材
113soscw.com,搜素材procedure TTeaWithHook.Brew;
114soscw.com,搜素材begin
115soscw.com,搜素材  Writeln(‘Steeping the Tea‘);
116soscw.com,搜素材end;
117soscw.com,搜素材
118soscw.com,搜素材function TTeaWithHook.CustomerWantsCondiments: Boolean;
119soscw.com,搜素材var
120soscw.com,搜素材  Answer: string;
121soscw.com,搜素材begin
122soscw.com,搜素材  Answer := GetUserInput;
123soscw.com,搜素材  if LowerCase(Answer) = ‘y‘ then
124soscw.com,搜素材    Result := True
125soscw.com,搜素材  else
126soscw.com,搜素材    Result := False;
127soscw.com,搜素材end;
128soscw.com,搜素材
129soscw.com,搜素材function TTeaWithHook.GetUserInput: string;
130soscw.com,搜素材var
131soscw.com,搜素材  Answer: string;
132soscw.com,搜素材begin
133soscw.com,搜素材  Answer := ‘‘;
134soscw.com,搜素材  Writeln(‘Would You Like Lemon With Your Tea (y / n)? ‘);
135soscw.com,搜素材  Readln(Answer);
136soscw.com,搜素材  if Answer = ‘‘ then
137soscw.com,搜素材    Result := ‘no‘;
138soscw.com,搜素材  Result := Answer;
139soscw.com,搜素材end;
140soscw.com,搜素材
141soscw.com,搜素材end.
142soscw.com,搜素材 

 

soscw.com,搜素材
 1soscw.com,搜素材
 2soscw.com,搜素材{《HeadFirst设计模式》之模板方法模式 }
 3soscw.com,搜素材{ 客户端                           }
 4soscw.com,搜素材{ 编译工具: Delphi7.0              }
 5soscw.com,搜素材{ E-Mail : guzh-0417@163.com      }
 6soscw.com,搜素材
 7soscw.com,搜素材program pCoffeineBeverageWithHook;
 8soscw.com,搜素材
 9soscw.com,搜素材{$APPTYPE CONSOLE}
10soscw.com,搜素材
11soscw.com,搜素材uses
12soscw.com,搜素材  SysUtils,
13soscw.com,搜素材  uCoffeineBeverageWithHook in ‘uCoffeineBeverageWithHook.pas‘;
14soscw.com,搜素材
15soscw.com,搜素材var
16soscw.com,搜素材  CoffeeHook: TCoffeeWithHook;
17soscw.com,搜素材  TeaHook   : TTeaWithHook;
18soscw.com,搜素材  
19soscw.com,搜素材begin
20soscw.com,搜素材  CoffeeHook := TCoffeeWithHook.Create;
21soscw.com,搜素材  TeaHook    := TTeaWithHook.Create;
22soscw.com,搜素材
23soscw.com,搜素材  Writeln(‘Making Coffeesoscw.com,搜素材‘);
24soscw.com,搜素材  CoffeeHook.PrepareRecipe;
25soscw.com,搜素材
26soscw.com,搜素材  Writeln(‘Making Teasoscw.com,搜素材‘);
27soscw.com,搜素材  TeaHook.PrepareRecipe;
28soscw.com,搜素材
29soscw.com,搜素材  FreeAndNil(CoffeeHook);
30soscw.com,搜素材  FreeAndNil(TeaHook);
31soscw.com,搜素材
32soscw.com,搜素材  Readln;
33soscw.com,搜素材end.

 

运行结果:

soscw.com,搜素材

 

 
 

Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---模板方法模式之CoffeineBeverageWithHook[转]

标签:des   blog   http   io   ar   os   sp   div   on   

原文地址:http://www.cnblogs.com/0x2D-0x22/p/4076317.html


评论


亲,登录后才可以留言!