實(shí)例講解JAVA 模板方法模式
在講述這個(gè)模式之前,我們先看一個(gè)案例:抄題目:兩個(gè)學(xué)生將老師出的題目抄寫(xiě)在紙上,并且寫(xiě)出答案
先看一個(gè)比較笨的寫(xiě)法
public class TestPaperA { public void testQuestion1(){ System.out.println("1+1等于幾? a.1 b.2 c.3 d.4"); System.out.println("答案:b"); } public void testQuestion2(){ System.out.println("1*1等于幾? a.1 b.2 c.3 d.4"); System.out.println("答案:a"); } public void testQuestion3(){ System.out.println("1/1等于幾? a.1 b.2 c.3 d.4"); System.out.println("答案:a"); } } public class TestPaperB { public void testQuestion1(){ System.out.println("1+1等于幾? a.1 b.2 c.3 d.4"); System.out.println("答案:c"); } public void testQuestion2(){ System.out.println("1*1等于幾? a.1 b.2 c.3 d.4"); System.out.println("答案:a"); } public void testQuestion3(){ System.out.println("1/1等于幾? a.1 b.2 c.3 d.4"); System.out.println("答案:d"); } } public class Test { public static void main(String[] args) { System.out.println("學(xué)生甲抄的試卷:"); TestPaperA studentA= new TestPaperA(); studentA.testQuestion1(); studentA.testQuestion2(); studentA.testQuestion3(); System.out.println("學(xué)生乙抄的試卷:"); TestPaperB studentB= new TestPaperB(); studentB.testQuestion1(); studentB.testQuestion2(); studentB.testQuestion3(); } }
輸出結(jié)果:
學(xué)生甲抄的試卷:
1+1等于幾? a.1 b.2 c.3 d.4
答案:b
1*1等于幾? a.1 b.2 c.3 d.4
答案:a
1/1等于幾? a.1 b.2 c.3 d.4
答案:a
學(xué)生乙抄的試卷:
1+1等于幾? a.1 b.2 c.3 d.4
答案:c
1*1等于幾? a.1 b.2 c.3 d.4
答案:a
1/1等于幾? a.1 b.2 c.3 d.4
答案:d
可以看出,學(xué)生甲和學(xué)生乙除了答案不一樣,抄的題目都一樣,抄題目的過(guò)程容易出錯(cuò),而且如果老師改了題目,那么兩個(gè)學(xué)生都需要把題目改掉
怎么優(yōu)化?我們先來(lái)一個(gè)初步優(yōu)化:學(xué)過(guò)繼承的都會(huì)想到,把公共部分放到父類(lèi)中,子類(lèi)繼承父類(lèi)后,自然就擁有了公共部分
public class TestPaper { public void testQuestion1(){ System.out.println("1+1等于幾? a.1 b.2 c.3 d.4"); } public void testQuestion2(){ System.out.println("1*1等于幾? a.1 b.2 c.3 d.4"); } public void testQuestion3(){ System.out.println("1/1等于幾? a.1 b.2 c.3 d.4"); } } public class TestPaperA extends TestPaper{ @Override public void testQuestion1(){ super.testQuestion1(); System.out.println("答案:b"); } @Override public void testQuestion2(){ super.testQuestion2(); System.out.println("答案:a"); } @Override public void testQuestion3(){ super.testQuestion3(); System.out.println("答案:a"); } } public class TestPaperB extends TestPaper{ @Override public void testQuestion1(){ super.testQuestion1(); System.out.println("答案:c"); } @Override public void testQuestion2(){ super.testQuestion2(); System.out.println("答案:a"); } @Override public void testQuestion3(){ super.testQuestion3(); System.out.println("答案:d"); } }
測(cè)試類(lèi)同上
我們看這個(gè)初步優(yōu)化,發(fā)現(xiàn)還是有重復(fù)的部分,比如super.testQuestion1()和System.out.println("答案”)
我們既然用了繼承,并且肯定這個(gè)繼承有意義,就應(yīng)該要成為子類(lèi)的模板,所有重復(fù)的代碼都應(yīng)該要上升到父類(lèi)去,而不是讓每個(gè)子類(lèi)都去重復(fù)。
對(duì)于“抄題目”這個(gè)例子來(lái)說(shuō),除了學(xué)生的答案會(huì)有不同的結(jié)果,其他全部都是一樣的。繼續(xù)優(yōu)化:
public abstract class TestPaper { public void testQuestion1(){ System.out.println("1+1等于幾? a.1 b.2 c.3 d.4"); System.out.println("答案:"+answer1()); } public void testQuestion2(){ System.out.println("1*1等于幾? a.1 b.2 c.3 d.4"); System.out.println("答案:"+answer2()); } public void testQuestion3(){ System.out.println("1/1等于幾? a.1 b.2 c.3 d.4"); System.out.println("答案:"+answer3()); } public abstract String answer1(); public abstract String answer2(); public abstract String answer3(); } public class TestPaperA extends TestPaper{ @Override public String answer1() { return "b"; } @Override public String answer2() { return "a"; } @Override public String answer3() { return "a"; } } public class TestPaperB extends TestPaper{ @Override public String answer1() { return "c"; } @Override public String answer2() { return "a"; } @Override public String answer3() { return "d"; } } public class Test { public static void main(String[] args) { System.out.println("學(xué)生甲抄的試卷:"); TestPaper studentA= new TestPaperA(); studentA.testQuestion1(); studentA.testQuestion2(); studentA.testQuestion3(); System.out.println("學(xué)生乙抄的試卷:"); TestPaper studentB= new TestPaperB(); studentB.testQuestion1(); studentB.testQuestion2(); studentB.testQuestion3(); } }
輸出結(jié)果:
學(xué)生甲抄的試卷:
1+1等于幾? a.1 b.2 c.3 d.4
答案:b
1*1等于幾? a.1 b.2 c.3 d.4
答案:a
1/1等于幾? a.1 b.2 c.3 d.4
答案:a
學(xué)生乙抄的試卷:
1+1等于幾? a.1 b.2 c.3 d.4
答案:c
1*1等于幾? a.1 b.2 c.3 d.4
答案:a
1/1等于幾? a.1 b.2 c.3 d.4
答案:d
結(jié)果和之前一模一樣,但簡(jiǎn)潔了很多。此時(shí)要有更多的學(xué)生來(lái)答卷,只不過(guò)是在試卷的木板上填寫(xiě)選擇題的選項(xiàng)答案,這是每個(gè)人的試卷唯一不同(誰(shuí)說(shuō)的,名字也不同,但這樣的做法的確是對(duì)試卷的最大復(fù)用)
下面介紹模板方法模式:http://chabaoo.cn/article/189195.htm
模板方法模式:定義一個(gè)操作中的算法的骨架,而將一些步驟延遲到子類(lèi)中。模板方法使得子類(lèi)可以不改變一個(gè)算法的結(jié)構(gòu)即可重定義該算法的某些特定步驟。
AbstractClass是抽象類(lèi),其實(shí)也就是一抽象模板,定義并實(shí)現(xiàn)了一個(gè)模板方法。
這個(gè)模板方法一般是一個(gè)具體方法。它給出了一個(gè)頂級(jí)邏輯的骨架,而邏輯的組成步驟在相應(yīng)的
抽象操作中,推遲到子類(lèi)實(shí)現(xiàn)。
public abstract class AbstractClass { //一些抽象行為,放到子類(lèi)去實(shí)現(xiàn) public abstract void primitiveOperation1(); public abstract void primitiveOperation2(); //模板方法,給出了邏輯的骨架,而邏輯的組成是一些相應(yīng)的抽象操作,它們都推遲到子類(lèi)去實(shí)現(xiàn) public void templateMethod(){ primitiveOperation1(); primitiveOperation2(); } }
ConcreteClass實(shí)現(xiàn)父類(lèi)所定義的一個(gè)或多個(gè)抽象方法。每一個(gè)AbstractClass都可以有任意多個(gè)ConcreteClass與之對(duì)應(yīng),而每一個(gè)ConcreteClass都可以給出這些抽象方法(也就是頂級(jí)邏輯的組成步驟)的不同實(shí)現(xiàn),從而使得頂級(jí)邏輯的實(shí)現(xiàn)各不相同。
public class ConcreteClassA extends AbstractClass{ @Override public void primitiveOperation1() { //具體類(lèi)A方法1實(shí)現(xiàn),與ConcreteClassB不同的方法實(shí)現(xiàn) } @Override public void primitiveOperation2() { //具體類(lèi)A方法2實(shí)現(xiàn),與ConcreteClassB不同的方法實(shí)現(xiàn) } } public class ConcreteClassB extends AbstractClass{ @Override public void primitiveOperation1() { //具體類(lèi)B方法1實(shí)現(xiàn),與ConcreteClassA不同的方法實(shí)現(xiàn) } @Override public void primitiveOperation2() { //具體類(lèi)B方法2實(shí)現(xiàn),與ConcreteClassA不同的方法實(shí)現(xiàn) } }
測(cè)試代碼
public class Test { public static void main(String[] args) { AbstractClass c = null; c = new ConcreteClassA(); c.templateMethod(); c = new ConcreteClassB(); c.templateMethod(); } }
模板方法模式是通過(guò)把不變行為搬移到超類(lèi),去除子類(lèi)中的重復(fù)代碼來(lái)體現(xiàn)它的優(yōu)勢(shì)。
模板方法模式就是提供了一個(gè)很好的代碼復(fù)用平臺(tái)。因?yàn)橛袝r(shí)候,我們會(huì)遇到由一系列步驟構(gòu)成的過(guò)程需要執(zhí)行。這個(gè)過(guò)程從高層次上看是相同的,但有些步驟的實(shí)現(xiàn)可能不同。這時(shí)候,我們通常就應(yīng)該要考慮用模板方法模式了。
當(dāng)不變的和可變的行為在方法的子類(lèi)實(shí)現(xiàn)中混合在一起的時(shí)候,不變的行為就會(huì)在子類(lèi)中重復(fù)出現(xiàn)。我們通過(guò)模板方法模式把這些行為搬移到單一的地方,這樣就幫助子類(lèi)擺脫重復(fù)的不變行為的糾纏
以上就是實(shí)例講解JAVA 模板方法模式的詳細(xì)內(nèi)容,更多關(guān)于JAVA 模板方法模式的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
java.lang.InterruptedException異常的問(wèn)題解決
本文主要介紹了java.lang.InterruptedException異常的問(wèn)題解決,這種異常通常意味著 Jenkins 任務(wù)在執(zhí)行過(guò)程中被中斷,這可能會(huì)導(dǎo)致任務(wù)失敗或中止,下面就來(lái)介紹一下解決方法,感興趣的可以了解一下2024-07-07MyBatis二級(jí)緩存實(shí)現(xiàn)關(guān)聯(lián)刷新
本文主要介紹了MyBatis二級(jí)緩存實(shí)現(xiàn)關(guān)聯(lián)刷新,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01解決idea中Terminal終端無(wú)法執(zhí)行GIT命令+Terminal 中文亂碼問(wèn)題
這篇文章主要介紹了解決idea中Terminal終端無(wú)法執(zhí)行GIT命令+Terminal 中文亂碼問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07JavaWeb之Servlet注冊(cè)頁(yè)面的實(shí)現(xiàn)示例
注冊(cè)頁(yè)面是很多網(wǎng)站都會(huì)是使用的到,本文主要介紹了JavaWeb之Servlet注冊(cè)頁(yè)面的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04淺談異常結(jié)構(gòu)圖、編譯期異常和運(yùn)行期異常的區(qū)別
下面小編就為大家?guī)?lái)一篇淺談異常結(jié)構(gòu)圖、編譯期異常和運(yùn)行期異常的區(qū)別。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-09-09詳解IntelliJ IDEA 中如何配置多個(gè)jdk版本即(1.7和1.8兩個(gè)jdk都可用)
這篇文章主要介紹了詳解IntelliJ IDEA 中如何配置多個(gè)jdk版本即(1.7和1.8兩個(gè)jdk都可用),非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-11-11SpringCloud中使用webclient(get和post)請(qǐng)求微服務(wù)接口數(shù)據(jù)
在SpringCloud項(xiàng)目中使用WebClient調(diào)用微服務(wù)時(shí),涉及配置WebClient、發(fā)起get和post請(qǐng)求等操作,如請(qǐng)求頭設(shè)置、服務(wù)地址配置、數(shù)據(jù)轉(zhuǎn)換處理、異常處理等,避免在循環(huán)中使用WebClient請(qǐng)求、路徑設(shè)置細(xì)節(jié)以及數(shù)據(jù)返回處理技巧,本文旨在幫助理解和應(yīng)用WebClient進(jìn)行微服務(wù)調(diào)用2024-10-10