<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE document PUBLIC "-//CNX//DTD CNXML 0.5 plus MathML//EN" "http://cnx.rice.edu/cnxml/0.5/DTD/cnxml_mathml.dtd">
<document xmlns="http://cnx.rice.edu/cnxml" xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="id11729958">
  <name>Template Design Pattern</name>
  <metadata>
  <md:version>1.2</md:version>
  <md:created>2008/07/04 00:44:40 GMT-5</md:created>
  <md:revised>2008/07/17 15:32:35.945 GMT-5</md:revised>
  <md:authorlist>
      <md:author id="swong">
      <md:firstname>Stephen</md:firstname>
      
      <md:surname>Wong</md:surname>
      <md:email>swong@rice.edu</md:email>
    </md:author>
  </md:authorlist>

  <md:maintainerlist>
    <md:maintainer id="prat">
      <md:firstname>Alex</md:firstname>
      
      <md:surname>Tribble</md:surname>
      <md:email>prat@rice.edu</md:email>
    </md:maintainer>
    <md:maintainer id="swong">
      <md:firstname>Stephen</md:firstname>
      
      <md:surname>Wong</md:surname>
      <md:email>swong@rice.edu</md:email>
    </md:maintainer>
    <md:maintainer id="dxnguyen">
      <md:firstname>Dung</md:firstname>
      <md:othername>X.</md:othername>
      <md:surname>Nguyen</md:surname>
      <md:email>dxnguyen@rice.edu</md:email>
    </md:maintainer>
  </md:maintainerlist>
  
  <md:keywordlist>
    <md:keyword>design pattern</md:keyword>
    <md:keyword>template</md:keyword>
    <md:keyword>template design pattern</md:keyword>
  </md:keywordlist>

  <md:abstract>Illustrates and explains the workings of the template design pattern.</md:abstract>
</metadata>
  <content>
    <section id="id-276424345148">
      <name>Template Design Pattern</name>
      <para id="id3632328">The Template Design Pattern is perhaps one of the most widely used and useful design pattern. It is used to set up the outline or skeleton of an algorithm, leaving the details to specific implementations later. This way, subclasses can override parts of the algorithm without changing its overall structure.</para>
      <para id="id11862347">This is particularly useful for separating the variant and the invariant behavior, minimizing the amount of code to be written. The invariant behavior is placed in the abstract class (<term>template</term>) and then any subclasses that inherit it can override the abstract methods and implement the specifics needed in that context. In the body of <code>TemplateMethod()</code> (see UML diagram below), there are calls to <code>operation1()</code> and <code>operation2()</code>. What it is that <code>operation1()</code> and <code>operation2()</code> do are defined by the subclasses which override them.</para>
      <figure id="templatediagram">
        <name>Template Design Pattern</name>
        <media type="image/png" src="template.png">
          <param name="height" value="339"/>
          <param name="width" value="499"/>
        </media>
        <caption>The Template design pattern in action: a template class and two subclasses of it.</caption>
      </figure>
      <para id="id7381880">A common example of this is when writing a program that will handle data using various sorting algorithms. The <code>AbstractClass</code> would have a method called <code>Sort()</code> (analogous to <code>TemplateMethod()</code> — the invariant behavior) which when called, would use the helper methods in the class, which are specified by any class that inherits from it. The helper methods in this case might be <code>compare()</code> (compares two objects and returns the one that is "higher") and <code>sortPass()</code> (performs one iteration of a particular sorting algorithm) The interesting thing is that the usual control structure of object calls and relations is reversed. It is the parent class that calls the method in the subclass, a behavior which Richard E. Sweet refers to as the "Hollywood Principle" — "Don't call us, we'll call you." The Template Design Pattern is of particular use in the <cnxn document="m14727">Factory Design Pattern</cnxn>.</para>
    </section>
  </content>
</document>
