head first--------------------template method pattern
/**
* 包含hook(钩子)的制作饮料的抽象类
* @author Administrator
*
*/
public abstract class BeverageWithHook {
//相当于模板方法templateMethod()---用于封装一个算法的骨架
public final void prepareRecipe(){
boilWater();
brew();
pourInCup();
if(customerWantsCondiments()){
addCondiments();
}
}
//第一步:煮沸水
public void boilWater(){
System.out.println("Boiling Water...");
}
//第二步:把沸水冲泡咖啡或者茶
public abstract void brew();
//第三步:将饮料倒进杯子
public void pourInCup(){
System.out.println("Pouring water into cup");
}
//第四步:往饮料内加入适当的调料
public abstract void addCondiments();
//钩子方法,一种用于声明在抽象类中的方法,但是只有空的或者默认的实现
public boolean customerWantsCondiments(){
return true;
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import com.clark.templatepattern.abstractclass.BeverageWithHook;
/**
* 制作咖啡的饮料类
* @author Administrator
*
*/
public class CoffeeWithHook extends BeverageWithHook {
@Override
public void brew() {
System.out.println("Dripping Coffee through filter");
}
@Override
public void addCondiments() {
System.out.println("Adding Sugar and Milk");
}
public boolean customerWantsCondiments(){
if("y".toLowerCase().equals(getUserInput())){
return true;
}else{
return false;
}
}
private String getUserInput(){
String answer=null;
System.out.println("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 e) {
e.printStackTrace();
}
if(answer==null){
return "no";
}
return answer;
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import com.clark.templatepattern.abstractclass.BeverageWithHook;
/**
* 制作茶的饮料类
* @author Administrator
*
*/
public class TeaWithHook extends BeverageWithHook {
@Override
public void brew() {
System.out.println("Dripping Tea through filter");
}
@Override
public void addCondiments() {
System.out.println("Adding Lemon");
}
public boolean customerWantsCondiments(){
if("y".toLowerCase().equals(getUserInput())){
return true;
}else{
return false;
}
}
private String getUserInput(){
String answer=null;
System.out.println("Would you like Lemon with your tea(y/n)?");
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
try {
answer=in.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if(answer==null){
return "no";
}
return answer;
}
}
public class BeverageWithHookTest {
public static void main(String[] args) {
TeaWithHook teaHook=new TeaWithHook();//创建一杯茶
CoffeeWithHook coffeeHook=new CoffeeWithHook();//创建一杯咖啡
System.out.println("\nMaking tea............");
teaHook.prepareRecipe();
System.out.println("\nMaking coffee.............");
coffeeHook.prepareRecipe();
}
}
head first--------------------template method pattern的更多相关文章
- 设计模式(九): 从醋溜土豆丝和清炒苦瓜中来学习"模板方法模式"(Template Method Pattern)
今天是五.四青年节,祝大家节日快乐.看着今天这标题就有食欲,夏天到了,醋溜土豆丝和清炒苦瓜适合夏天吃,好吃不上火.这两道菜大部分人都应该吃过,特别是醋溜土豆丝,作为“鲁菜”的代表作之一更是为大众所熟知 ...
- 深入浅出设计模式——模板方法模式(Template Method Pattern)
模式动机 模板方法模式是基于继承的代码复用基本技术,模板方法模式的结构和用法也是面向对象设计的核心之一.在模板方法模式中,可以将相同的代码放在父类中,而将不同的方法实现放在不同的子类中.在模板方法模式 ...
- 设计模式 - 模板方法模式(template method pattern) JFrame 具体解释
模板方法模式(template method pattern) JFrame 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考模板方法模式(templ ...
- 乐在其中设计模式(C#) - 模板方法模式(Template Method Pattern)
原文:乐在其中设计模式(C#) - 模板方法模式(Template Method Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 模板方法模式(Template Method ...
- C#设计模式之十三模板方法模式(Template Method Pattern)【行为型】
一.引言 “结构型”的设计模式已经写完了,从今天我们开始讲“行为型”设计模式.现在我们开始讲[行为型]设计模式的第一个模式,该模式是[模板方法],英文名称是:Template Method Patte ...
- 设计模式 - 模板方法模式(template method pattern) 排序(sort) 具体解释
模板方法模式(template method pattern) 排序(sort) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考模板方法模式(tem ...
- 设计模式 - 模板方法模式(template method pattern) 具体解释
模板方法模式(template method pattern) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy 模板方法模式(template metho ...
- 二十四种设计模式:模板方法模式(Template Method Pattern)
模板方法模式(Template Method Pattern) 介绍定义一个操作中的算法的骨架,而将一些步骤延迟到子类中.Template Method使得子类可以不改变一个算法的结构即可重定义该算法 ...
- 瑞幸咖啡还是星巴克,一杯下午茶让我明白 设计模式--模板方法模式(Template Method Pattern)
简介 Define the skeleton of an algorithm in an operation,deferring some steps to subclasses.Template M ...
- 模板方法模式(Template Method Pattern)——复杂流程步骤的设计
模式概述 在现实生活中,很多事情都包含几个实现步骤,例如请客吃饭,无论吃什么,一般都包含点单.吃东西.买单等几个步骤,通常情况下这几个步骤的次序是:点单 --> 吃东西 --> 买单. 在 ...
随机推荐
- Codeforces 527C Glass Carving(Set)
意甲冠军 片w*h玻璃 其n斯普利特倍 各事业部为垂直或水平 每个分割窗格区域的最大输出 用两个set存储每次分割的位置 就能够比較方便的把每次分割产生和消失的长宽存下来 每次分割后剩下 ...
- Appium根据xpath获取控件实例随笔
如文章<Appium基于安卓的各种FindElement的控件定位方法实践>所述,Appium拥有众多获取控件的方法.其中一种就是根据控件所在页面的XPATH来定位控件. 本文就是尝试通过 ...
- YII相关资料(干货)
Sites 网站 yiifeed:Yii 最新动态都在这里 yiigist:Yii 专用的 Packages my-yii:Yii 学习资料和新闻 Docs 文档 Yii Framework 2.0 ...
- curl_redir_exec()函数
function curl_redir_exec($ch,$debug="") { static $curl_loops = 0; static $curl_max_loops = ...
- C# FileSystemWatcher 监视磁盘文件
C# FileSystemWatcher 监视磁盘文件变更 简化需求:有一个简化了的需求是这样的:有一个拍照程序在运行,一旦抓拍之后则将图片文件存储至某目录,然后图片要上传至远程服务器并update数 ...
- JS基础——事件绑定
上一篇博客JS事件对象中,老师问JS事件处理和VB中的事件处理有什么联系?先来解决一下这个问题.举个VB.net中事件处理的样例(JS敲久了,VB习惯的都不熟悉了,看来得常常回想了): 1.事件处理V ...
- QTP知识总结(一)
QTP知识总结(一) (2010-12-22 16:30:41) 转载▼ 标签: 杂谈 分类: QTP File menu Process guidance management,View > ...
- ps入门教程:选择工具、移动工具、索套工具的使用
本节课程主要内容:1.学习矩形选择工具.椭圆选择工具.移动工具.多边形套索工具.套索工具.磁性套索工具和魔术 棒选择工具.2.用套索和磁性套索,实现对人物照片的抠图.----------------- ...
- sql材料分级统计及汇总案例参考
--第一步:根据系统编号.列.单价分组求和 select CLBH,DJ,sum(SL) as SL,sum(JE) as JE,Lie into #TempSZCMX from #ShouZhiCu ...
- java中数据库通用层
/** * 数据库通用类 * */ public class ConnDB { /** * 获取数据库连接对象 * @return 数据库连接对象 * */ public static Connect ...