设计模式Template Method,核心理念是工作流程逻辑到抽象超类的集中化,一些具体方法实现交给子类处理
/**
* Template Method模式
* 超类集中业务逻辑,而子类集中于实现基本超作
* @author paul
* @date 2006-8-9
*/
public abstract class CaffeineBeverage {
/**
* 业务逻辑流程,final确保不被子类 改变
* prepareRecipe void
* 2006-8-9 16:00:57
*/
final void prepareRecipe() {
boilWater();
brew();
pourInCup();
addCondiments();
}
/**
* 模板方法,有子类集体实现
* 受保护的方法,调用者不用关心实现的细节
* brew void
* 2006-8-9 16:02:17
*/
protected abstract void brew();
abstract void addCondiments();
void boilWater() {
System.out.println( " Boiling water " );
}
void pourInCup() {
System.out.println( " Pouring into cup " );
}
}
public class Coffee extends CaffeineBeverage {
public void brew() {
System.out.println( " Dripping Coffee through filter " );
}
public void addCondiments() {
System.out.println( " Adding Sugar and Milk " );
}
}
public class Tea extends CaffeineBeverage {
public void brew() {
System.out.println( " Steeping the tea " );
}
public void addCondiments() {
System.out.println( " Adding Lemon " );
}
}
* Template Method模式
* 超类集中业务逻辑,而子类集中于实现基本超作
* @author paul
* @date 2006-8-9
*/
public abstract class CaffeineBeverage {
/**
* 业务逻辑流程,final确保不被子类 改变
* prepareRecipe void
* 2006-8-9 16:00:57
*/
final void prepareRecipe() {
boilWater();
brew();
pourInCup();
addCondiments();
}
/**
* 模板方法,有子类集体实现
* 受保护的方法,调用者不用关心实现的细节
* brew void
* 2006-8-9 16:02:17
*/
protected abstract void brew();
abstract void addCondiments();
void boilWater() {
System.out.println( " Boiling water " );
}
void pourInCup() {
System.out.println( " Pouring into cup " );
}
}
public class Coffee extends CaffeineBeverage {
public void brew() {
System.out.println( " Dripping Coffee through filter " );
}
public void addCondiments() {
System.out.println( " Adding Sugar and Milk " );
}
}
public class Tea extends CaffeineBeverage {
public void brew() {
System.out.println( " Steeping the tea " );
}
public void addCondiments() {
System.out.println( " Adding Lemon " );
}
}
为了获得更大的灵活性,扩展性,可以用Strategy设计模式替代Tmeplate Method模式
把模板方法(交给子类实现的东西)转移到一个接口中
/**
* Strategu模式,把公用方法抽象成接口
* @author paul
* @date 2006-8-9
*/
public abstract class Beverage {
/**
* 业务逻辑的实现,final确保不被子类 改变
* prepareRecipe void
* 2006-8-9 16:00:57
*/
final void prepareRecipe() {
boilWater();
brew();
pourInCup();
addCondiments();
}
private ActionInDrinking action;
public void brew() {
action.brew();
}
public void addCondiments(){
this .action.addCondiments();
}
// protected abstract void brew();
// abstract void addCondiments();
void boilWater() {
System.out.println( " Boiling water " );
}
void pourInCup() {
System.out.println( " Pouring into cup " );
}
public ActionInDrinking getAction() {
return action;
}
public void setAction(ActionInDrinking action) {
this .action = action;
}
}
/
public interface ActionInDrinking {
public void brew();
public void addCondiments();
}
public class TeaDrink implements ActionInDrinking {
public TeaDrink() {
super ();
// TODO Auto-generated constructor stub
}
public void brew() {
// TODO Auto-generated method stub
// steeping tea
}
public void addCondiments() {
// TODO Auto-generated method stub
}
}
* Strategu模式,把公用方法抽象成接口
* @author paul
* @date 2006-8-9
*/
public abstract class Beverage {
/**
* 业务逻辑的实现,final确保不被子类 改变
* prepareRecipe void
* 2006-8-9 16:00:57
*/
final void prepareRecipe() {
boilWater();
brew();
pourInCup();
addCondiments();
}
private ActionInDrinking action;
public void brew() {
action.brew();
}
public void addCondiments(){
this .action.addCondiments();
}
// protected abstract void brew();
// abstract void addCondiments();
void boilWater() {
System.out.println( " Boiling water " );
}
void pourInCup() {
System.out.println( " Pouring into cup " );
}
public ActionInDrinking getAction() {
return action;
}
public void setAction(ActionInDrinking action) {
this .action = action;
}
}
/
public interface ActionInDrinking {
public void brew();
public void addCondiments();
}
public class TeaDrink implements ActionInDrinking {
public TeaDrink() {
super ();
// TODO Auto-generated constructor stub
}
public void brew() {
// TODO Auto-generated method stub
// steeping tea
}
public void addCondiments() {
// TODO Auto-generated method stub
}
}