模拟类:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * 模板方法模式: 实现一个算法时,整体步骤很固定,只有某些部分 3 * 易变,这是将这些易变部分抽象出来,而先固定好方法执行步骤 4 * 再让子类实现易变的抽象方法 5 */ 6 7 //模拟到银行办业务 8 public abstract class BankTransaction { 9 protected void takeNumber(){ 10 System.out.println("取号"); 11 } 12 13 protected abstract void transact(); 14 15 protected void grade(){ 16 System.out.println("完结评分"); 17 } 18 19 //流程在此方法中进行安排 20 public void process(){ 21 this.takeNumber(); 22 this.transact(); 23 this.grade(); 24 } 25 } 26 27 class Deposit extends BankTransaction{ 28 @Override 29 public void transact() { 30 System.out.println("存款1E"); 31 } 32 } 33 34 class Withdraw extends BankTransaction{ 35 @Override 36 public void transact() { 37 System.out.println("取款"); 38 } 39 }
测试类:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class Test { 2 public static void main(String[] args){ 3 BankTransaction b = new Withdraw(); 4 b.process(); 5 } 6 }