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

  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.

运行结果:

 
 

Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---模板方法模式之CoffeineBeverageWithHook[转]的更多相关文章

  1. 设计模式之第3章-模板方法模式(Java实现)

    设计模式之第3章-模板方法模式(Java实现) "那个,上次由于我老婆要给我做饭,所以就没有说完就走掉了...这个那个".这次和以前一样,先来开场福利(工厂方法模式已被作者踹下场) ...

  2. Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---工厂模式之简单工厂

    简单工厂:工厂依据传进的参数创建相应的产品. http://www.cnblogs.com/DelphiDesignPatterns/archive/2009/07/24/1530536.html { ...

  3. Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---迭代器模式之DinerMenu[转]

    容器的主要职责有两个:存放元素和浏览元素.根据单一职责原则(SRP)要将二者分开,于是将浏览功能打包封装就有了迭代器. 用迭代器封装对动态数组的遍历:  1  2{<HeadFirst设计模式& ...

  4. Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---策略模式之MiniDuckSimulator[转]

     1  2{<HeadFirst设计模式>之策略模式 }  3{ 本单元中的类为策略类           }  4{ 编译工具: Delphi7.0           }  5{ E- ...

  5. Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---命令模式之RemoteControlTest[转]

      1   2{<HeadFirst设计模式>之命令模式 }   3{ 本单元中的类为命令的接收者      }   4{ 编译工具 :Delphi7.0         }   5{ 联 ...

  6. 设计模式(十四)模板方法模式(Template Pattern)

    一.引言 提到模板,大家肯定不免想到生活中的“简历模板”.“论文模板”.“Word中模版文件”等,在现实生活中,模板的概念就是——有一个规定的格式,然后每个人都可以根据自己的需求或情况去更新它,例如简 ...

  7. 设计模式13:Template Method 模板方法模式(行为型模式)

    Template Method 模板方法模式(行为型模式) 变与不变 变化——是软件永恒的主题,如何管理变化带来的复杂性?设计模式的艺术性和复杂度就在于如何分析,并发现体系中的变化点和稳定点,并使用特 ...

  8. 【设计模式】行为型02模板方法模式(Template Method Patten)

    五一长假,没有出去,不喜欢嘈杂的人群,玩了会游戏发泄了下憋在心底的戾气,手旁大马克杯里是母亲泡的绿茶.点开自己的播放列表,耳机里传来的是理查德克莱德曼的致爱丽丝.自己是个凡人,卑微渺小的活着.不说废话 ...

  9. C#设计模式学习笔记:(13)模板方法模式

    本笔记摘抄自:https://www.cnblogs.com/PatrickLiu/p/7837716.html,记录一下学习过程以备后续查用. 一.引言 今天我们要讲行为型设计模式的第一个模式--模 ...

随机推荐

  1. SSM14-通过AOP实现日志记录

    1.要求使用AOP思想,实现对每一个用户登陆后,将以下信息保存在数据库 1>登陆时间 2>退出时间 3>登录的IP地址 4>访问点URL(访问了那些Controller) 5& ...

  2. [Codeplus 4月赛]最短路

    题意:理论上是给定一张完全图,有边权,在给一些单向边求最短路. 思路: 我充分体会到了我图论的菜. 理论上建图肯定是不能\(n^2\)的,考虑如何优化呢? 将边权异或值二进制替换,最后一遍最短路就行, ...

  3. redis可视化客户端工具TreeNMS

    TreeNMS是一款redis,Memcache可视化客户端工具,采用JAVA开发,实现基于WEB方式对Redis, Memcached数据库进行管理.维护. 功能包括:状态参数监控,NoSQL数据库 ...

  4. python安装requests第三方模块

    2018-08-28 22:04:51 1 .下载到桌面后解压,放到python的目录下 ------------------------------------------------------- ...

  5. 深入浅出Java中的clone克隆方法,写得太棒了!

    作者:张纪刚 blog.csdn.net/zhangjg_blog/article/details/18369201/ Java中对象的创建 clone 顾名思义就是 复制 , 在Java语言中, c ...

  6. mybatis-plus分页查询

    在springboot中整合mybatis-plus 按照官方文档进行的配置:快速开始|mybatis-plus 引入依赖: <!-- 引入mybatisPlus --> <depe ...

  7. scrapy爬虫框架爬取招聘网站

    目录结构 BossFace.py文件中代码: # -*- coding: utf-8 -*-import scrapyfrom ..items import BossfaceItemimport js ...

  8. java_DateTimeFormatter

    日期时间的格式化和解析: public class DateTimeFormatterTest { /** * 时间日期格式化 * @param args */ public static void ...

  9. Java开发系列-时间转换

    获取当前时间戳 // 获取当前的时间戳 long time = new Date().getTime(); 将字符串时间戳转成格式的时间字符串 Long timestrap = Long.parseL ...

  10. rem适配手机

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...