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;
}
}
/**
* 包含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;
}
}
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;
}
}
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(" Making tea............");
teaHook.prepareRecipe();
System.out.println(" Making coffee.............");
coffeeHook.prepareRecipe();
}
}
public class BeverageWithHookTest {
public static void main(String[] args) {
TeaWithHook teaHook=new TeaWithHook();//创建一杯茶
CoffeeWithHook coffeeHook=new CoffeeWithHook();//创建一杯咖啡
System.out.println(" Making tea............");
teaHook.prepareRecipe();
System.out.println(" Making coffee.............");
coffeeHook.prepareRecipe();
}
}