行为模式: Template Method(模板方法)
2013-02-07 21:03
393 查看
abstract class Generalization { // 1. Standardize the skeleton of an algorithm in a "template" method public void findSolution() { stepOne(); stepTwo(); stepThr(); stepFor(); } // 2. Common implementations of individual steps are defined in base class protected void stepOne() { System.out.println( "Generalization.stepOne" ); } // 3. Steps requiring peculiar impls are ";placeholders" in the base class abstract protected void stepTwo(); abstract protected void stepThr(); protected void stepFor() { System.out.println( "Generalization.stepFor" ); } } abstract class Specialization extends Generalization { // 4. Derived classes can override placeholder methods // 1. Standardize the skeleton of an algorithm in a "template" method protected void stepThr() { step3_1(); step3_2(); step3_3(); } // 2. Common implementations of individual steps are defined in base class protected void step3_1() { System.out.println( "Specialization.step3_1" ); } // 3. Steps requiring peculiar impls are "placeholders" in the base class abstract protected void step3_2(); protected void step3_3() { System.out.println( "Specialization.step3_3" ); } } class Realization extends Specialization { // 4. Derived classes can override placeholder methods protected void stepTwo() { System.out.println( "Realization .stepTwo" ); } protected void step3_2() { System.out.println( "Realization .step3_2" ); } // 5. Derived classes can override implemented methods // 6. Derived classes can override and "call back to" base class methods protected void stepFor() { System.out.println( "Realization .stepFor" ); super.stepFor(); } } class TemplateMethodDemo { public static void main( String[] args ) { Generalization algorithm = new Realization(); algorithm.findSolution(); } }
Generalization.stepOneRealization .stepTwoSpecialization.step3_1Realization .step3_2Specialization.step3_3Realization .stepForGeneralization.stepFor
List of Template Method examples
相关文章推荐
- 设计模式二十三:template method(模板方法)——类行为模式
- 行为设计模式---模板方法模式(Template Method)
- 行为类模式(十):模板方法(Template Method)
- 行为模式 - 模板方法(Template Method)
- 【设计模式】行为模式之Template Method模板方法
- 【设计模式基础】行为模式 - 6 - 模板方法(Template Method)
- 【设计模式学习笔记二十四】【行为模式】【模板方法(Template Method)】
- 模板方法模式(Template Method)
- 模板方法模式(Template Method)
- 设计模式系列之Template Method(模板方法模式)
- 设计模式心得: 模板方法模式(template method pattern)
- 大话设计模式之模板方法模式(Template Method Pattern)
- 《JAVA与模式》之模板方法模式(行为)
- 用最简单的例子理解模板方法模式(Template Method Pattern)
- Java设计模式透析之 —— 模板方法(Template Method)
- C#设计模式系列:模板方法模式(Template Method)
- C# 设计模式-模板方法(Template Method)
- 模板方法(Template Method)模式
- 设计模式拾荒之模板方法模式( Template Method Pattern ): 与工厂方法模式之间的情侣关系
- 浅谈Java设计模式(十四)模板方法模式(Template Method)