设计模式 - 模板方法模式(template method pattern) 具体解释
模板方法模式(template method pattern) 详细解释
本文地址: http://blog.csdn.net/caroline_wendy
模板方法模式(template method pattern): 在一个方法中定义一个算法的骨架, 而将一些步骤延迟到子类中.
模板方法使得子类能够在不改变算法结构的情况下, 又一次定义算法中的某些步骤.
模板方法能够进行挂钩(hook), 钩子(hook)是一种被声明在抽象类中的方法, 但仅仅有空的或者默认的实现.
钩子的存在, 能够让子类有能力对算法的不同点进行挂钩.
抽象类的框架:
/**
* @time 2014年6月18日
*/
package template_method; /**
* @author C.L.Wang
*
*/
public abstract class AbstractClass { final void templateMethod() {
primitiveOperation1();
primitiveOperation2();
concreteOperation();
hook();
} abstract void primitiveOperation1(); abstract void primitiveOperation2(); final void concreteOperation() { } void hook() {}
}
面向对象原则:
好莱坞原则: 别调用我们, 我们会调用你.
详细方法:
1. 抽象类(abstract class), 包括模板方法(template method), 抽象操作(abstract operation),
详细操作(concrete operation), 和钩子(hook).
/**
* @time 2014年6月18日
*/
package template_method; /**
* @author C.L.Wang
*
*/
public abstract class CaffeineBeverage { final 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;
} }
2. 详细类(concrete class), 继承(extend) 抽象类(abstract class).
/**
* @time 2014年6月18日
*/
package template_method; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader; /**
* @author C.L.Wang
*
*/
public class CoffeeWithHook extends CaffeineBeverage { /* (non-Javadoc)
* @see template_method.CaffeineBeverage#brew()
*/
@Override
void brew() {
// TODO Auto-generated method stub
System.out.println("Dripping Coffee through filter");
} /* (non-Javadoc)
* @see template_method.CaffeineBeverage#addCondiments()
*/
@Override
void addCondiments() {
// TODO Auto-generated method stub
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.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 ioe) {
System.out.println("IO error trying to read your answer");
} if (answer == null) {
return "no";
} return answer;
} } /**
* @time 2014年6月18日
*/
package template_method; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader; /**
* @author C.L.Wang
*
*/
public class TeaWithHook extends CaffeineBeverage { /* (non-Javadoc)
* @see template_method.CaffeineBeverage#brew()
*/
@Override
void brew() {
// TODO Auto-generated method stub
System.out.println("Steeping the tea");
} /* (non-Javadoc)
* @see template_method.CaffeineBeverage#addCondiments()
*/
@Override
void addCondiments() {
// TODO Auto-generated method stub
System.out.println("Adding Lemon");
} public boolean customerWantsCondiments() { String answer = getUserInput(); if (answer.toLowerCase().startsWith("y")) {
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 ioe) {
System.out.println("IO error trying to read your answer");
} if (answer == null) {
return "no";
} return answer;
} }
3. 測试类, 包括钩子(hook)操作.
/**
* @time 2014年6月18日
*/
package template_method; /**
* @author C.L.Wang
*
*/
public class BeverageTestDrive { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
TeaWithHook teaHook = new TeaWithHook();
CoffeeWithHook coffeeHook = new CoffeeWithHook(); System.out.println("\nMaking tea...");
teaHook.prepareRecipe(); System.out.println("\nMaking coffee...");
coffeeHook.prepareRecipe();
} }
4. 输出:
Making tea...
Boiling water
Steeping the tea
Pouring into cup
Would you like lemon with your tea (y/n)? y
Adding Lemon Making coffee...
Boiling water
Dripping Coffee through filter
Pouring into cup
Would you like milk and sugar with your coffee (y/n)?
n
设计模式 - 模板方法模式(template method pattern) 具体解释的更多相关文章
- 设计模式 - 模板方法模式(template method pattern) JFrame 具体解释
模板方法模式(template method pattern) JFrame 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考模板方法模式(templ ...
- 设计模式 - 模板方法模式(template method pattern) 排序(sort) 具体解释
模板方法模式(template method pattern) 排序(sort) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考模板方法模式(tem ...
- 乐在其中设计模式(C#) - 模板方法模式(Template Method Pattern)
原文:乐在其中设计模式(C#) - 模板方法模式(Template Method Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 模板方法模式(Template Method ...
- 二十四种设计模式:模板方法模式(Template Method Pattern)
模板方法模式(Template Method Pattern) 介绍定义一个操作中的算法的骨架,而将一些步骤延迟到子类中.Template Method使得子类可以不改变一个算法的结构即可重定义该算法 ...
- 模板方法模式(Template Method Pattern)——复杂流程步骤的设计
模式概述 在现实生活中,很多事情都包含几个实现步骤,例如请客吃饭,无论吃什么,一般都包含点单.吃东西.买单等几个步骤,通常情况下这几个步骤的次序是:点单 --> 吃东西 --> 买单. 在 ...
- 设计模式(九): 从醋溜土豆丝和清炒苦瓜中来学习"模板方法模式"(Template Method Pattern)
今天是五.四青年节,祝大家节日快乐.看着今天这标题就有食欲,夏天到了,醋溜土豆丝和清炒苦瓜适合夏天吃,好吃不上火.这两道菜大部分人都应该吃过,特别是醋溜土豆丝,作为“鲁菜”的代表作之一更是为大众所熟知 ...
- java设计模式 模板方法模式Template Method
设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了可重用代码.让代码更容易被他人理解.保证代码可靠性.毫无疑问,设计模式于己 ...
- 模板方法模式(Template Method Pattern)
模板方法模式是一种基于继承的代码复用技术,定义一个操作中的算法的骨架,而将步骤延迟到子类中.模板方法使得子类可以不改变一个算法的结构即可重定义算法的某些特定步骤. 模式中的角色 抽象类(Abstrac ...
- 使用 C# (.NET Core) 实现模板方法模式 (Template Method Pattern)
本文的概念内容来自深入浅出设计模式一书. 项目需求 有一家咖啡店, 供应咖啡和茶, 它们的工序如下: 咖啡: 茶: 可以看到咖啡和茶的制作工序是差不多的, 都是有4步, 其中有两步它们两个是一样的, ...
随机推荐
- [CodeForces-332E]Binary Key
题目大意: 给你两个字符串p和s,让你求出一个字典序尽量小的长度为k的01串密钥,能将p转化为s. 密钥的工作方式如下: 第i位是0,表示这一位无用: 第i位是1,表示这一位有用. 若密钥的长度比s短 ...
- [转]Eclipse 项目转移到Android Studio遇到的问题
1.Android Studio直接导入项目是copy原项目的,无法纳入代码管控 解决方案: 英文地址:http://developer.android.com/sdk/installing/migr ...
- Codeforces Round #348 (VK Cup 2016 Round 2, Div. 2 Edition) C. Little Artem and Matrix 模拟
C. Little Artem and Matrix 题目连接: http://www.codeforces.com/contest/669/problem/C Description Little ...
- jsonp和jsonpcallback的使用
1. jsonp.jsonpCallback jsonp跨域时可以自定义的两个参数 2. jsonp: 回掉函数名的参数名,默认callback,服务端通过它来获取到回掉函数名 3. jsonpCa ...
- VS2012项目中使用CocoStudio相关文件的设置
开发环境说明: win7 vs2012 coco2d-x 3.0 alpha1 cocos2d-x 3.0 alpha 1搭配CocoStudio使用,效果更佳.CocoStudio包含了游戏开发 ...
- oc(object-c)知识汇总/总结/区别对比(持续更新)
1.判断某个类是否实现了某方法: A *a =[[A alloc] autorelease]; if([a respondsToSelector:@selector(methodName)]){ // ...
- SDRAM interface slashes pin count
Many designs need deep buffering but don't require ultrahigh-memory bandwidth. Examples include imag ...
- 关于目前自己iOS项目使用的插件
1. VVDocumenter-Xcode 规范注释生成器. 2. RTImageAssets 本项目是一个 Xcode 插件,用来生成 @3x 的图片资源对应的 @2x 和 @1x 版本,只要拖拽高 ...
- 【spring boot】spring boot中使用@RestController不起作用,不返回json,依旧去找访问接口的请求地址对应的页面
问题描述: spring boot中使用@RestController不起作用,不返回json,依旧去找访问接口的请求地址对应的页面 表现结果: 1>使用postman测试接口,表现为返回是40 ...
- git和SVN的差别
1)GIT是分布式的.SVN不是: 这 是GIT和其它非分布式的版本号控制系统,比如SVN.CVS等.最核心的差别.优点是跟其它同事不会有太多的冲突.自己写的代码放在自己电脑上,一段时间后再提交.合并 ...