CMPT 300 Design Patterns
May 2008

Template Method Pattern



<<
lab6.zip >>

Intent
- The Template Method pattern defines the skeleton of an algorithm in an operation while deferring some steps to subclasses. This pattern lets subclasses redefine certain steps of an algorithm without changing the structure of the underlying algorithm.

The template method pattern is useful in the situations where you have a common algorithm that follows the same steps, but various ways of implementing the steps.

In UML:

Template Method Pattern  

In this situation, all algorithms invoke the following steps:
operation1();
operation2();
operation3();

This allows a concrete implementation  of the algorithm to specify how to write the abstract methods operation1() and operation2().

Note that in addition to the templateMethod(), there is another concrete method - operation3() . As we do not want the subclass to change the nature of the algorithm, templateMethod() must be declared as final.  Additional concrete methods are declared as final as well. (However, there is one exception - hooks - which we will  cover shortly.)

A simple code example:
- AbstractTemplate.java

- ConcreteTemplate.java

- TemplateTester.java

Hooks

A hook is a concrete method defined in the abstract class that subclasses may override (if they wish.) By overriding the hook, a client can "hook" into the parent algorithm.
- AbstractTemplateWithHook.java

- ConcreteTemplateWithHook.java

- TemplateTesterWithHook.java

A hook can be more sophisticated if we wish. For example, we could modify the template method such as:

    final void templateMethod() {
        operation1();
        operation2();
        if (hook())
            operation3();
    }

The Template Method in the Java API

The template method is practiced widely in the Java API. The java.util.Arrays class provides a sort() method that internally uses the template method to sort an array of objects. Java applets also practice the template method with extensive use of hooks. The following program illustrates how the JFrame classes uses the template method pattern as well. >> MyFrame.java





The Lab


Consider the following classes for making breakfast:
- Omelette.java

- ScrambledEggs.java

- SunnySide.java

- EatBreakfast.java

This is terrible! There is lots of code duplication, and no abstraction going on anywhere!

Now that you know a bit about the template method pattern, refactor this code and apply the template method pattern discussed above. At  first, you do not need to be concerned with hooks.


Changing Requirements

About the only constant thing with requirements is they are prone to change. We now want to allow each breakfast (scrambled, omelette, sunny side) to decide if they want to spread salt and pepper over the eggs before serving them. Add a hook to your design that allows a specific breakfast implementation to determine if they want salt and pepper or not. (Hint - take a look at the slightly modified version of templateMethod() shown above.)