《Head First 设计模式》学习笔记——模板方法模式
钩子的还有一个使用方法,是让子类能够有机会对模板方法中某些即将发生的(或刚刚发生的)步骤做出反应。
//声明为抽象类,子类必须实现其操作
public abstract class CaffeineBeverage
{ //声明为final,不希望子类覆盖这种方法
final void prepareRecipe()
{
boilWater();
brew();
pourInCup();
addCondiments();
} //声明为抽象类,子类必须实现其操作
abstract void brew();
abstract void addCondiments(); void boilWater()
{
System.out.println("Boiling water");
} void pourInCup()
{
System.out.println("Pouring into cup");
}
} //extends继承
public class Tea extends CaffeineBeverage {
//须要定义抽象方法
public void brew() {
System.out.println("Steeping the tea");
}
public void addCondiments() {
System.out.println("Adding Lemon");
}
}
public abstract class CaffeineBeverageWithHook {
void prepareRecipe() {
boilWater();
brew();
pourInCup();
if (customerWantsCondiments()) {
addCondiments();
}
}
abstract void brew();
abstract void addCondiments();
void boilWater() {
System.out.println("Boiling water");
}
void pourInCup() {
System.out.println("Pouring into cup");
}
//定义了一个缺省实现,子类能够覆盖它。但不见得一定要这么做
boolean customerWantsCondiments() {
return true;
}
}
public class CoffeeWithHook extends CaffeineBeverageWithHook {
public void brew() {
System.out.println("Dripping Coffee through filter");
}
public void addCondiments() {
System.out.println("Adding Sugar and Milk");
}
//覆盖了这种方法。提供了自己的功能
public boolean customerWantsCondiments() {
//让用户输入对调料的决定
String answer = getUserInput();
if (answer.toLowerCase().startsWith("y")) {
return true;
} else {
return false;
}
}
private String getUserInput() {
String answer = null;
System.out.print("Would you like milk and sugar with your coffee (y/n)? ");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
try {
answer = in.readLine();
} catch (IOException ioe) {
System.err.println("IO error trying to read your answer");
}
if (answer == null) {
return "no";
}
return answer;
}
}
《Head First 设计模式》学习笔记——模板方法模式的更多相关文章
- Java设计模式学习记录-模板方法模式
前言 模板方法模式,定义一个操作中算法的骨架,而将一些步骤延迟到子类中.使得子类可以不改变一个算法的结构即可重新定义该算法的某些特定步骤. 模板方法模式 概念介绍 模板方法模式,其实是很好理解的,具体 ...
- C#学习笔记-模板方法模式
题目:同学摘抄老师给的试卷并给出自己的对应的答案. 实现: static void Main(string[] args) { Console.WriteLine("学生甲抄的试卷:&quo ...
- 设计模式学习之模板方法模式(TemplateMethod,行为型模式)(9)
一.什么是模板方法模式 Template Method模式也叫模板方法模式,是行为模式之一,它把具有特定步骤算法中的某些必要的处理委让给抽象方法,通过子类继承对抽象方法的不同实现改变整个算法的行为. ...
- Java-马士兵设计模式学习笔记-命令模式
一.概述 命令模式 二.代码 1.Client.java public class Client { public void request(Server server){ server.addCom ...
- Java-马士兵设计模式学习笔记-桥接模式
一.概述 1.桥接模式的应用情况:(1)两个维度扩展(2)排列组合 二.代码 1.Gift.java public class Gift { protected GiftImpl giftImpl; ...
- Java-马士兵设计模式学习笔记-工厂模式-抽象工厂模式
一.概述 1.抽象工厂:当情况是需要产生一系列产品,若需更换产品,则要求一系列产品一起换,且要控制一系列产品的产生过程,此时可考虑抽象工厂模式.例:小明装修屋子,把电视.冰箱都替换掉,他这次需要把电视 ...
- Java-马士兵设计模式学习笔记-工厂模式-简单工厂
一.概述 1.目标:要控制任意类型交通工具的生产模式 2.目标有两层意思(1)任意类型 (2)生产模式,所以对应的,要这两个层面上抽象(Movable,VehicleFactory),利用接口,实现多 ...
- Java-马士兵设计模式学习笔记-策略模式-模拟 Comparator接口
续上一篇 <Java 模拟 Comparable接口> 一.Teacher类及Student类的比较大小方式是不固定的,比如老师除了比较职称外,还可比较工龄大小,年龄大小等.则定义Com ...
- Java-马士兵设计模式学习笔记-迭代器模式-模仿Collectin ArrayList LinckedList
Java Iterator模式 Java Iterator模式, 模仿Collectin ArrayList LinckedList 一.有如下几个类 1.接口Collection.java 2.接口 ...
随机推荐
- Qt学习之路:自定义Model三篇,自定义委托等等
http://devbean.blog.51cto.com/448512/d-8/p-2
- [ASP.NET]以iTextSharp手绘表格并产生PDF下载
原文 [ASP.NET]以iTextSharp手繪表格並產生PDF下載 大家使用iTextSharp的機緣都不太一樣, 由於單位Crystal Report的License數量有限主管要我去找一個免費 ...
- jsp页面中格式化为小数点两位
<td align="center"> <% String avgnum = ""; if(request.getAttribute(&qu ...
- 在JavaScript中也玩变量类型强行转换
<script language="javascript"> var str = '100'; var num = Number(100); a ...
- 基于visual Studio2013解决算法导论之003雇佣问题
题目 雇用问题 解决代码及点评 #include <stdio.h> #include <stdlib.h> #include <malloc.h> #in ...
- Android 程式开发:(廿一)消息传递 —— 21.3 使用Intent发送短信
使用SmsManager类,可以在自己编写的程序内部发送短信,而不需要调用系统的短信应用. 然而,有的时候调用系统内置的短信应用会更加方便. 这时,需要使用一个MIME类型为vnd.android-d ...
- Mac中MacPorts安装和使用
文章转载至http://www.zikercn.com/node/8 星期四, 06/07/2012 - 19:02 - 张慧敏 MacPorts简单介绍 MacPorts,以前叫做DarwinPor ...
- UVA 11292 - The Dragon of Loowater (water)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=sh ...
- 不是什么时候都可以用栈来声明对象并使用(自动释放)——Delphi里到处都是编译器魔法,并且自动帮助实例化界面元素指针
一直都喜欢这样显示窗口,因为简单高效: void MainWidget::ShowMyWindow() { MyWidget form(this); form.resize(,); form.exec ...
- Redis bio
还是一个很小的模块. bio就是background io的意思,既然要background,就要创建线程,创建几个线程呢?有几种类型的io,就创建几个线程.同一种类型的job需要排队,所以存放各自的 ...