模板方法模式定义了一个算法骨架,允许子类对算法的某个或某些步骤进行重写(override)。 1.CoffeineBeverageWithHook {《HeadFirst设计模式》之模板方法模式 } { 编译工具: Delphi7.0 } { E-Mail : xshlife@163.com } unit uCoffeineBeverageWithHook; interface uses SysUtils; type TCoffeineBeverageWithHook = class(TObject) protected procedure BoilWater; procedure Brew; virtual; abstract; procedure PourInCup; procedure AddCondiments; virtual; abstract; function CustomerWantsCondiments: Boolean; virtual; { 钩子 } public procedure PrepareRecipe; { 模板方法 } end; TCoffeeWithHook = class(TCoffeineBeverageWithHook) private function GetUserInput: string; public procedure Brew; override; procedure AddCondiments; override; function CustomerWantsCondiments: Boolean; override; end; TTeaWithHook = class(TCoffeineBeverageWithHook) private function GetUserInput: string; public procedure Brew; override; procedure AddCondiments; override; function CustomerWantsCondiments: Boolean; override; end; implementation { TCoffeineBeverageWithHook } procedure TCoffeineBeverageWithHook.BoilWater; begin Writeln('Boiling Water'); end; function TCoffeineBeverageWithHook.CustomerWantsCondiments: Boolean; begin Result := True; end; procedure TCoffeineBeverageWithHook.PourInCup; begin Writeln('Poiling into cup'); end; procedure TCoffeineBeverageWithHook.PrepareRecipe; begin BoilWater; Brew; PourInCup; if CustomerWantsCondiments then AddCondiments; end; { TCoffeeWithHook } procedure TCoffeeWithHook.AddCondiments; begin Writeln('Add Sugar and Milk'); end; procedure TCoffeeWithHook.Brew; begin Writeln('Drip Coffee Through Filter'); end; function TCoffeeWithHook.CustomerWantsCondiments: Boolean; var Answer: string; begin Answer := GetUserInput; if LowerCase(Answer) = 'y' then Result := True else Result := False; end; function TCoffeeWithHook.GetUserInput: string; var Answer: string; begin Answer := ''; Writeln('Would You Like Milk And Sugar With Your Coffee (y / n)? '); Readln(Answer);; if Answer = '' then Result := 'no'; Result := Answer; end; { TTeaWithHook } procedure TTeaWithHook.AddCondiments; begin Writeln('Add Lemon'); end; procedure TTeaWithHook.Brew; begin Writeln('Steeping the Tea'); end; function TTeaWithHook.CustomerWantsCondiments: Boolean; var Answer: string; begin Answer := GetUserInput; if LowerCase(Answer) = 'y' then Result := True else Result := False; end; function TTeaWithHook.GetUserInput: string; var Answer: string; begin Answer := ''; Writeln('Would You Like Lemon With Your Tea (y / n)? '); Readln(Answer); if Answer = '' then Result := 'no'; Result := Answer; end; end. 2.客户端 {《HeadFirst设计模式》之模板方法模式 } { 客户端 } { 编译工具: Delphi7.0 } { E-Mail : xshlife@163.com } program pCoffeineBeverageWithHook; {$APPTYPE CONSOLE} uses SysUtils, uCoffeineBeverageWithHook in 'uCoffeineBeverageWithHook.pas'; var CoffeeHook: TCoffeeWithHook; TeaHook : TTeaWithHook; begin CoffeeHook := TCoffeeWithHook.Create; TeaHook := TTeaWithHook.Create; Writeln('Making Coffee '); CoffeeHook.PrepareRecipe; Writeln('Making Tea '); TeaHook.PrepareRecipe; FreeAndNil(CoffeeHook); FreeAndNil(TeaHook); Readln; end. 运行结果: