码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2014-11-05 16:30:15      阅读:208      评论:0      收藏:0      [点我收藏+]

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

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

 

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

 

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

 

运行结果:

bubuko.com,布布扣

 

 
 

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

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

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!