Java资源网

| JAVA基础 | 环境配置 | JDBC | 线程技术 | Socket编程 | JavaMail | JAVA与XML | 设计模式 | 技术新闻 | Java认证 | 程序人生 软件下载
| JSP&Servlet | Spring | Struts | Hibernate | JBuilder | Eclipse | WebService | EJB技术 | J2ME开发 | 应用服务器 | JXTA | Ajax
Articles search文章搜索
   关键字:
   类 别:
       
New download 最新下载
· [组件]HTML Parser 1.5
· [教程]WebSphere Studio应用教程
· [组件]JDom 1.0
· [工具]Junit3.8.1
· [教程]EJB编程及J2EE系统架构和设计
· [教程]EJB教程
· [教程]J2EE Tutorial中文版
· [教程]Java编程思想2(英文)
· [教程]java编程思想(完整版)
· [教程]Java网络编程
New articles 最新文章
· 设计移动 Web 服务
· 解析XML的时候完全忽略DTD
· 理解XML Schema XML Schema 初步
· 标签库的深入研究
· 提升JSP应用程序的七大绝招
· 如何使用JDOM对XML文件进行操作
· 处理XML字符串中特殊字符
· 利用Digester把XML转换成为Java对象
· 使用WebService 和RMI远程协作
· 使用Axis开发Web Service程序
Articles top 热门文章
· Eclipse基础--plugin插件安装(6644)
· eclipse+tomcat+lomboz的安装配置说明(4774)
· Java程序员就业前景(4584)
· Windows下JAVA环境变量的设置祥解(3788)
· Tomcat下JSP、Servlet和JavaBean环境的配置(3716)
· 使用links方式安装Eclipse插件(3698)
· 一个老程序员的心理话(3533)
· linux下jdk的安装与配置(3459)
· 初学者入门:Structs中基本配置入门(3334)
· Eclipse 运行命令行参数大全(3084)
您的位置:首页>>设计模式>>[Behavioralnbsp;Patterns]nbsp;Thenbsp;Templatenbsp;Pattern
[Behavioralnbsp;Patterns]nbsp;Thenbsp;Templatenbsp;Pattern
2007-04-13   来源:www.javaresearch.org  作者:未知

 Intent 


Define the skeleton of an algorithm in an operation, deferring some steps to client subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure. 

 Problem 


Two different components have significant similarities, but demonstrate no reuse of common interface or implementation. If a change common to both components becomes necessary, duplicate effort must be expended. 

 Structure 



Steps and sequence:
1. Standardize the skeleton of an algorithm in a base class "template" method
2. Common implementations of individual steps are defined in the base class
3. Steps requiring peculiar implementations are "placeholders" in base class
4. Derived classes can override placeholder methods
5. Derived classes can override implemented methods
6. Derived classes can override and "call back to" base class methods
  1. abstract class Generalization {
  2.    // 1. Standardize the skeleton of an algorithm in a "template" method
  3.    public void findSolution() {
  4.       stepOne();
  5.       stepTwo();
  6.       stepThr();
  7.       stepFor();
  8.    }
  9.    // 2. Common implementations of individual steps are defined in base class
  10.    protected void stepOne() { System.out.println( "Generalization.stepOne" ); }
  11.    // 3. Steps requiring peculiar impls are "placeholders" in the base class
  12.    abstract protected void stepTwo();
  13.    abstract protected void stepThr();
  14.    protected void stepFor() { System.out.println( "Generalization.stepFor" ); }
  15. }
  16. abstract class Specialization extends Generalization {
  17.    // 4. Derived classes can override placeholder methods
  18.    // 1. Standardize the skeleton of an algorithm in a "template" method
  19.    protected void stepThr() {
  20.       step3_1();
  21.       step3_2();
  22.       step3_3();
  23.    }
  24.    // 2. Common implementations of individual steps are defined in base class
  25.    protected void step3_1() { System.out.println( "Specialization.step3_1" ); }
  26.    // 3. Steps requiring peculiar impls are "placeholders" in the base class
  27.    abstract protected void step3_2();
  28.    protected void step3_3() { System.out.println( "Specialization.step3_3" ); }
  29. }
  30. class Realization extends Specialization {
  31.    // 4. Derived classes can override placeholder methods
  32.    protected void stepTwo() { System.out.println( "Realization   .stepTwo" ); }
  33.    protected void step3_2() { System.out.println( "Realization   .step3_2" ); }
  34.    // 5. Derived classes can override implemented methods
  35.    // 6. Derived classes can override and "call back to" base class methods
  36.    protected void stepFor() {
  37.       System.out.println( "Realization   .stepFor" );
  38.       super.stepFor();
  39. }  }
  40. class TemplateMethodDemo {
  41.    public static void main( String[] args ) {
  42.       Generalization algorithm = new Realization();
  43.       algorithm.findSolution();
  44. }  }
  45. // Generalization.stepOne
  46. // Realization   .stepTwo
  47. // Specialization.step3_1
  48. // Realization   .step3_2
  49. // Specialization.step3_3
  50. // Realization   .stepFor
  51. // Generalization.stepFor

 When to use 


Whenever you write a parent class where you leave one or more of themethods to be implemented by derived classes, you are in essence using theTemplate pattern. The Template pattern formalizes the idea of defining analgorithm in a class, but leaving some of the details to be implemented insubclasses. In other words, if your base class is an abstract class, as oftenhappens in these design patterns, you are using a simple form of the Templatepattern.

  --相关文章--
· 面向对象编程,我的思想 (2007-04-13)
· 面向对象的思维方式 (2007-04-13)
· 通过Javanbsp;Swing看透MVC设计模式 (2007-04-13)
· 适配器模式(Adapternbsp;Pattern) (2007-04-13)
· 追MM与Java的23种设计模式 (2007-04-13)
· 责任链模式(Chainnbsp;ofnbsp;Responsibility) (2007-04-13)

版权所有©2005-2006 JAVA资源网 渝ICP备05007591号 虚拟主机 | 关于我们 | 联系方式 | 广告业务 | 网站地图 | 友情链接