實(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://www.dhdzp.com/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)文章
一步步教會(huì)你使用Java原生指令編譯并運(yùn)行一個(gè)程序
Java是一種廣泛使用的編程語(yǔ)言,具有跨平臺(tái)性和面向?qū)ο蟮奶匦?下面這篇文章主要給大家介紹了關(guān)于使用Java原生指令編譯并運(yùn)行一個(gè)程序的相關(guān)資料,需要的朋友可以參考下2024-07-07
SpringBoot Aop 詳解和多種使用場(chǎng)景解析
aop面向切面編程,是編程中一個(gè)很重要的思想本篇文章主要介紹的是SpringBoot切面Aop的使用和案例,對(duì)SpringBoot Aop相關(guān)知識(shí)感興趣的朋友跟隨小編一起看看吧2021-08-08
Spring如何替換掉默認(rèn)common-logging.jar
這篇文章主要介紹了Spring如何替換掉默認(rèn)common-logging.jar,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
SpringCloud之分布式配置中心Spring Cloud Config高可用配置實(shí)例代碼
這篇文章主要介紹了SpringCloud之分布式配置中心Spring Cloud Config高可用配置實(shí)例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04
Java實(shí)現(xiàn)的日期處理類(lèi)完整實(shí)例
這篇文章主要介紹了Java實(shí)現(xiàn)的日期處理類(lèi),結(jié)合完整實(shí)例形式分析了Java針對(duì)日期的獲取、運(yùn)算、轉(zhuǎn)換等相關(guān)操作技巧,需要的朋友可以參考下2017-09-09
Java數(shù)據(jù)結(jié)構(gòu)--時(shí)間和空間復(fù)雜度
這篇文章主要介紹了java數(shù)據(jù)結(jié)構(gòu)的時(shí)間和空間復(fù)雜度,小編覺(jué)得這篇文寫(xiě)的不錯(cuò),感興趣的朋友可以了解下,希望能夠給你帶來(lái)幫助2021-08-08

