Skip to content Skip to navigation

Connexions

You are here: Home » Content » Template Design Pattern

Navigation

Content Actions

  • Download module PDF
  • Add to ...
    Add the module to:
    • My Favorites
    • A lens
    • An external social bookmarking service
    • My Favorites (What is 'My Favorites'?)
      'My Favorites' is a special kind of lens which you can use to bookmark modules and collections directly in Connexions. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need a Connexions account to use 'My Favorites'.
    • A lens (What is a lens?)

      Definition of a lens

      Lenses

      A lens is a custom view of Connexions content. You can think of it as a fancy kind of list that will let you see Connexions through the eyes of organizations and people you trust.

      What is in a lens?

      Lens makers point to Connexions materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

      Who can create a lens?

      Any individual Connexions member, a community, or a respected organization.

    • External bookmarks
  • E-mail the author

Recently Viewed

This feature requires Javascript to be enabled.

Template Design Pattern

Module by: Stephen Wong

Summary: Illustrates and explains the workings of the template design pattern.

Template Design Pattern

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.

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 (template) and then any subclasses that inherit it can override the abstract methods and implement the specifics needed in that context. In the body of TemplateMethod() (see UML diagram below), there are calls to operation1() and operation2(). What it is that operation1() and operation2() do are defined by the subclasses which override them.

Figure 1: The Template design pattern in action: a template class and two subclasses of it.
Template Design Pattern
Template Design Pattern (template.png)

A common example of this is when writing a program that will handle data using various sorting algorithms. The AbstractClass would have a method called Sort() (analogous to TemplateMethod() — 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 compare() (compares two objects and returns the one that is "higher") and sortPass() (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 Factory Design Pattern.

Comments, questions, feedback, criticisms?

Send feedback