head first---------模板方法模式
        浅谈谈模板方法模式:在一个方法中定义一个算法的骨架,而将一些步骤延迟到子类中。模板方法使得子类可以在不改变算法结构的情况下,重新定义算法中的某些步骤。
      模板方法模式中涉及的一个设计原则是:别找我,我会找你。模板方法模式为我们提供了一种代码复用的重用技巧。
    以下为代码的实现:
   package com.clark.templatepattern.abstractclass;
/**
 * 包含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;
}
}


package com.clark.templatepattern;

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;
}

}


package com.clark.templatepattern;

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;
}

}


package com.clark.templatepattern;

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的更多相关文章

  1. 设计模式(九): 从醋溜土豆丝和清炒苦瓜中来学习"模板方法模式"(Template Method Pattern)

    今天是五.四青年节,祝大家节日快乐.看着今天这标题就有食欲,夏天到了,醋溜土豆丝和清炒苦瓜适合夏天吃,好吃不上火.这两道菜大部分人都应该吃过,特别是醋溜土豆丝,作为“鲁菜”的代表作之一更是为大众所熟知 ...

  2. 深入浅出设计模式——模板方法模式(Template Method Pattern)

    模式动机 模板方法模式是基于继承的代码复用基本技术,模板方法模式的结构和用法也是面向对象设计的核心之一.在模板方法模式中,可以将相同的代码放在父类中,而将不同的方法实现放在不同的子类中.在模板方法模式 ...

  3. 设计模式 - 模板方法模式(template method pattern) JFrame 具体解释

    模板方法模式(template method pattern) JFrame 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考模板方法模式(templ ...

  4. 乐在其中设计模式(C#) - 模板方法模式(Template Method Pattern)

    原文:乐在其中设计模式(C#) - 模板方法模式(Template Method Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 模板方法模式(Template Method ...

  5. C#设计模式之十三模板方法模式(Template Method Pattern)【行为型】

    一.引言 “结构型”的设计模式已经写完了,从今天我们开始讲“行为型”设计模式.现在我们开始讲[行为型]设计模式的第一个模式,该模式是[模板方法],英文名称是:Template Method Patte ...

  6. 设计模式 - 模板方法模式(template method pattern) 排序(sort) 具体解释

    模板方法模式(template method pattern) 排序(sort) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考模板方法模式(tem ...

  7. 设计模式 - 模板方法模式(template method pattern) 具体解释

    模板方法模式(template method pattern) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy 模板方法模式(template metho ...

  8. 二十四种设计模式:模板方法模式(Template Method Pattern)

    模板方法模式(Template Method Pattern) 介绍定义一个操作中的算法的骨架,而将一些步骤延迟到子类中.Template Method使得子类可以不改变一个算法的结构即可重定义该算法 ...

  9. 瑞幸咖啡还是星巴克,一杯下午茶让我明白 设计模式--模板方法模式(Template Method Pattern)

    简介 Define the skeleton of an algorithm in an operation,deferring some steps to subclasses.Template M ...

  10. 模板方法模式(Template Method Pattern)——复杂流程步骤的设计

    模式概述 在现实生活中,很多事情都包含几个实现步骤,例如请客吃饭,无论吃什么,一般都包含点单.吃东西.买单等几个步骤,通常情况下这几个步骤的次序是:点单 --> 吃东西 --> 买单. 在 ...

随机推荐

  1. C语言库函数大全及应用实例三

    原文:C语言库函数大全及应用实例三 [编程资料]C语言库函数大全及应用实例三 函数名: ecvt 功 能: 把一个浮点数转换为字符串 用 法: char ecvt(double value, int ...

  2. Linq to Sql:N层应用中的查询(上) : 返回自定义实体

    原文:Linq to Sql:N层应用中的查询(上) : 返回自定义实体 如果允许在UI层直接访问Linq to Sql的DataContext,可以省去很多问题,譬如在处理多表join的时候,我们使 ...

  3. phpstorm集成phpunit

    1.下载phpunit.phar,将该文件放到某个工程中 2.File > Settings > Languages & Frameworks > PHP > PHPU ...

  4. testNg自动化,读取excel的数据

    自己写了一个testng执行excel用例的小程序,主要是运行.xlsx的,需要支持xls可以自己扩展,分享一下.下载地址:http://yun.baidu.com/share/link?sharei ...

  5. selenium2入门 用selenium安装、加载、启用插件(一)

    一:下载 下载地址是:http://docs.seleniumhq.org/download/

  6. SVG 学习(一)

    SVG 意为可缩放矢量图形(Scalable Vector Graphics). SVG 使用 XML 格式定义图像. 什么是SVG? SVG 指可伸缩矢量图形 (Scalable Vector Gr ...

  7. QT5.4 vs2013静态加载插件的sqlite静态编译

    1. 非常多同学在静态编译QT5完毕后, sqlite的驱动老是载入不进去, 原因可能是由于你没有例如以下操作: #include <QtPlugin> Q_IMPORT_PLUGIN(Q ...

  8. Ibatis ISqlMapper工厂类案例

    namespace Model{ public class MapperFactory { //声明一个ISqlMapper接口类型的数据映射器 _mapper,其初始值为null private s ...

  9. OWC11生成统计图案例

    (1)饼状图:----通过修改参数生成不同的走势图, string strCategory = "优良率" + '\t' + "合格率" + '\t' + &q ...

  10. 【学习笔记】锋利的jQuery(二)DOM操作

    一.获取DOM节点 //找祖宗 parent() parents() closest() //找后代 children(); find(); //找兄弟 next()/nextAll() prev() ...