Design Pattern: Template Method 模式

來源:互聯網
上載者:User

http://www.35java.com/zhibo/forum.php?mod=viewthread&tid=288&extra=page%3D1

不要將設計模式想得高不可攀,好像高手才會使用的東西,事實上如果您在下手程式之前,能稍稍對程式作個分析規劃,或多或少都會用到一些模式了,模式不是教條,它只是前人的經驗成果,而 Gof 的書則是擇前人之精華持續改進而來罷了。

Template Method模式就是一個很簡單的模式,但可能是使用最廣泛的模式,也許您也一直在使用這樣的模式,看它的 UML 類別結構圖就知道了:

僅僅是抽象類別與具體類別實作的關係而已,有些人常問抽象類別與介面的區別為何,Template Method模式可以提供其中一個答案,例如:

  • AbstractClass.java

public abstract class AbstractClass {
    public void templateMethod() {
        // step by step template to solve something
        // implementor should follow those step
        opStep1();
        opStep2();
        opStep3();
    }

    public abstract void opStep1();
    public abstract void opStep2();
    public abstract void opStep3();
}

  • ConcreteClass.java

public class ConcreteClass extends AbstractClass {
    public abstract void opStep1() {
        // implement the real operation
    }

    public abstract void opStep2() {
        // implement the real operation
    }

    public abstract void opStep3() {
        // implement the real operation
    }
}
對於一些程式而言,我們希望規定一些處理的步驟、流程或骨架,就像是上例中的step1到step3一樣,至於流程中的step1到step3如何實作並不規定,而留給實作的人自行決定,這就是Template Method模式的目的。

抽象類別與介面的差別之一,也正在於抽象類別可以先實作其中一些方法,而介面則是完全僅規定介面,使用Template Method模式就可以看出兩者之間在應用上的一個差別。

僅以step1到step3這樣的操作來看Template Method模式,似乎彰顯示不出其實作骨架,而將實作部份留待子類的實用性,在 Gof書中所舉的例子是與 Factory Method 模式結合的一個例子;通常開啟一個檔案的流程是相似的,例如文字檔或二進位檔,不外乎檢查檔案是否可開啟、讀取檔案、設定顯示等流程,可以使用Template Method模式來規範這個流程:
public abstract class Application {
    // .....

    public void openDocument(String name) {
        // Template Method
        if(!canOpenDocument(name)) { // unable to open file
            // show error message, throw exception
            return;
        }

        Document doc = createDocument(); // Factory Method

        if(doc != null) {
            _docs.addDocument(doc);
            // Template Method
            aboutToOpenDocument(doc);
             doc.open();
             doc.doRead();
        }
    }

    // Factory Method
    public abstract Document createDocument();

    // Template Method
    public abstract boolean canOpenDocument(String name);
    public abstract void aboutToOpenDocument(Document doc);
}

public class MyApplication extends Application {
    // implement Factory Method
    public void Document createDocument() {
        return new MyDocument();
    }

    // implement Template Method
    public void boolean canOpenDocument(String name) {
        // implemented code here
    }

    public void aboutToOpenDocument(Document doc) {
        // implemented code here
    }
}

Factyro Method模式將實際要建立的物件延遲至子類中決定,而TemplateMethod模式則是將流程架構的實作留待子類來解決,事實上在這個例子中,您也可以將createDocument()看作是TemplateMethod模式中的一個方法,從物件建立的角度來看它是Factory Method,而從流程架構的角度來看,它則是TemplateMethod模式的一個方法實作。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.