Design Pattern: Template Method 模式

简介:

  学习是分享和合作式的!

转载请注明出处:http://blog.csdn.net/wdzxl198/article/details/9223275

文章摘自: http://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

目录
相关文章
how is SAP UI5 applyBackendSearchPattern being called
how is SAP UI5 applyBackendSearchPattern being called
how is SAP UI5 applyBackendSearchPattern being called
|
算法 C# Java
使用C# (.NET Core) 实现模板方法模式 (Template Method Pattern)
本文的概念内容来自深入浅出设计模式一书. 项目需求 有一家咖啡店, 供应咖啡和茶, 它们的工序如下: 咖啡: 茶: 可以看到咖啡和茶的制作工序是差不多的, 都是有4步, 其中有两步它们两个是一样的, 另外两步虽然具体内容不一样, 但是都做做的同一类工作.
1324 0