閱讀314 返回首頁    go 阿裏雲 go 技術社區[雲棲]


Design Pattern: Template Method 模式

  學習是分享和合作式的!

轉載請注明出處:https://blog.csdn.net/wdzxl198/article/details/9223275

文章摘自: https://www.riabook.cn/doc/designpattern/

不要將設計模式想得高不可攀,好像高手才會使用的東西,事實上如果您在下手程式之前,能稍稍對程式作個分析規劃,或多或少都會用到一些模式了,模式不是教條,它隻是前人的經驗成果,而 Gof 的書則是擇前人之精華持續改進而來罷了。
Template Method模式就是一個很簡單的模式,但可能是使用最廣泛的模式,也許您也一直在使用這樣的模式,看它的 UML 類別結構圖就知道了:

TemplateMethod

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

  • AbstractClass.java
   1: public abstract class AbstractClass { 
   2:     public void templateMethod() { 
   3:         // step by step template to solve something 
   4:         // implementor should follow those step 
   5:         opStep1(); 
   6:         opStep2(); 
   7:         opStep3(); 
   8:     } 
   9:  
  10:     public abstract void opStep1(); 
  11:     public abstract void opStep2(); 
  12:     public abstract void opStep3(); 
  13: } 
  • ConcreteClass.java
   1: public class ConcreteClass extends AbstractClass { 
   2:     public abstract void opStep1() { 
   3:         // implement the real operation 
   4:     } 
   5:  
   6:     public abstract void opStep2() { 
   7:         // implement the real operation 
   8:     } 
   9:  
  10:     public abstract void opStep3() { 
  11:         // implement the real operation 
  12:     } 
  13: }
對於一些程式而言,我們希望規定一些處理的步驟、流程或骨架,就像是上例中的step1到step3一樣,至於流程中的step1到step3如何實作並不規定,而留給實作的人自行決定,這就是Template Method模式的目的。
抽象類別與介麵的差別之一,也正在於抽象類別可以先實作其中一些方法,而介麵則是完全僅規定接口,使用Template Method模式就可以看出兩者之間在應用上的一個差別。
僅以step1到step3這樣的操作來看Template Method模式,似乎彰顯示不出其實作骨架,而將實作部份留待子類的實用性,在 Gof 書中所舉的例子是與 Factory Method 模式 結合的一個例子;通常開啟一個檔案的流程是相似的,例如文字檔或二進位檔,不外乎檢查檔案是否可開啟、讀取檔案、設定顯示等流程,可以使用 Template Method模式來規範這個流程: 
   1: public abstract class Application {
   2:     // .....
   3:     public void openDocument(String name) {
   4:         // Template Method
   5:         if(!canOpenDocument(name)) { // unable to open file
   6:             // show error message, throw exception
   7:             return;
   8:         }
   9:         Document doc = createDocument(); // Factory Method
  10:         if(doc != null) {
  11:             _docs.addDocument(doc);
  12:             // Template Method
  13:             aboutToOpenDocument(doc);
  14:              doc.open();
  15:              doc.doRead();
  16:         }
  17:     }
  18:     // Factory Method
  19:     public abstract Document createDocument();
  20:     // Template Method
  21:     public abstract boolean canOpenDocument(String name);
  22:     public abstract void aboutToOpenDocument(Document doc);
  23:  }
  24:  public class MyApplication extends Application {
  25:     // implement Factory Method
  26:     public void Document createDocument() {
  27:         return new MyDocument();
  28:     }
  29:     // implement Template Method
  30:     public void boolean canOpenDocument(String name) {
  31:         // implemented code here
  32:     }
  33:     public void aboutToOpenDocument(Document doc) {
  34:         // implemented code here
  35:     }
  36:  }

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

Edit by Atlas,

Time:08:00

最後更新:2017-04-03 16:48:33

  上一篇:go SQL Server 2008安裝的時提示“重啟計算機失敗”怎麼辦?
  下一篇:go HDFS基礎概念