package com.factory.commonality;<br />/**<br /> * 一個去銀行貸款的例子,貸款前銀行要檢查你的背景資訊:銀行賬戶、信用卡、已有貸款、股票......<br /> * 因為每個銀行都會檢查這些資訊,所以這裡可以使用template method模式。<br /> */<br />public class TemplateMethod {<br />public static void main(String[] args) {<br />ChinaConstructionBankLoanApp client1 = new ChinaConstructionBankLoanApp(<br />"Judy");<br />System.out.println("/nCheck client " + client1.getName());<br />client1.check();<br />EverBrightBankLoanApp client2 = new EverBrightBankLoanApp("Mark");<br />System.out.println("/nCheck client " + client2.getName());<br />client2.check();<br />}<br />}<br />abstract class CheckBackground {</p><p>// 下面五個為模板方法,需要由子類來實現的。<br />public abstract void checkBank();<br />public abstract void checkCredit();<br />public abstract void checkLoan();<br />public abstract void checkStock();<br />public abstract void checkIncome();<br />// 調用上面5個方法<br />public void check() {<br />checkBank();<br />checkCredit();<br />checkLoan();<br />checkStock();<br />checkIncome();<br />}<br />}<br />// 建行。<br />class ChinaConstructionBankLoanApp extends CheckBackground {<br />private String name;<br />public ChinaConstructionBankLoanApp(String name) {<br />this.name = name;<br />}<br />public String getName() {<br />return name;<br />}<br />// 建行對背景資訊的檢查。<br />public void checkBank() {<br />System.out.println("check bank...");<br />}<br />public void checkCredit() {<br />System.out.println("check credit...");<br />}<br />public void checkLoan() {<br />System.out.println("check other loan...");<br />}<br />public void checkStock() {<br />System.out.println("check stock values...");<br />}<br />public void checkIncome() {<br />System.out.println("check family income...");<br />}<br />}<br />// 光大。<br />class EverBrightBankLoanApp extends CheckBackground {<br />private String name;<br />public EverBrightBankLoanApp(String name) {<br />this.name = name;<br />}<br />public String getName() {<br />return name;<br />}<br />// 對背景資訊的檢查。<br />public void checkBank() {<br />System.out.println("check bank...");<br />}<br />public void checkCredit() {<br />System.out.println("check credit...");<br />}<br />public void checkLoan() {<br />System.out.println("check other loan...");<br />}<br />public void checkStock() {<br />System.out.println("check stock values...");<br />}<br />public void checkIncome() {<br />System.out.println("check family income...");<br />}<br />}<br />
運行結果:
Check client Judy
check bank...
check credit...
check other loan...
check stock values...
check family income...
Check client Mark
check bank...
check credit...
check other loan...
check stock values...
check family income...
不多說了,程式多讀幾遍就懂了~~~^_^