Skip to content

Week 10 - Template Method Design Pattern

The Template Method Design Pattern

Classification

  • Behavioral Design Pattern

Pattern Definition

  • Hard Definition: Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure.
  • Easy Definition: Abstract Super class controls the algorithm, but delegates unique parts of the algorithm to sub-classes.

Representations

Mermaid Graph - Template Method Pattern

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
classDiagram
    Client --> SuperClass
    SuperClass <|-- ConcreteSubClass1
    SuperClass <|-- ConcreteSubClass2
    class Client {
        -SuperClass sc1
    }
    class SuperClass {
        <<abstract>>
        +final templateMethod()
        +primitiveOperation1()
        +primitiveOperation2()
        +subclassOperation1()*
        +subclassOperation2()*
        +hook1()
        +hook2()
    }
    class ConcreteSubClass1 {
        +subclassOperation1()
        +subclassOperation2()
        +primitiveOperation2()
    }
    class ConcreteSubClass2 {
        +subclassOperation1()
        +subclassOperation2()
        +hook1()
    }

UML

Real World Usages

  • Order processing algorithms: Consider a Brick-n-Mortar Store VS an Online Store
    • The basic order processing algorithm is: Select Items, Checkout, Pay, and Delivery, but how you select items, checkout, pay and deliver varies depending on the store type and its options.
  • All non-abstract methods of java.io.InputStream, java.io.OutputStream, java.io.Reader and java.io.Writer.
  • All non-abstract methods of java.util.AbstractList, java.util.AbstractSet and java.util.AbstractMap.
    • Remember from the book, get and size are the abstract methods that need to be overridden for the template methods like contains.

Java Code Example

Main Take-Aways from the textbook

  • A template method defines the steps of an algorithm, deferring to subclasses for the implementation of those steps.
  • The Template Method Pattern gives us an important technique for code reuse.
  • The template method’s abstract class may define concrete methods, abstract methods, and hooks.
  • Abstract methods are implemented by subclasses.
  • Hooks are methods that do nothing or default behavior in the abstract class, but may be overridden in the subclass.
  • To prevent subclasses from changing the algorithm in the template method, declare the template method as final.
  • The Hollywood Principle guides us to put decision making in high- level modules that can decide how and when to call low-level modules.
  • You’ll see lots of uses of the Template Method Pattern in real-world code, but (as with any pattern) don’t expect it all to be designed “by the book.”
  • The Strategy and Template Method Patterns both encapsulate algorithms, the first by composition and the other by inheritance.
  • Factory Method is a specialization of Template Method.

Main OO Principles of Template Method Pattern

  • Hollywood Principle: The Super-class calls the methods of its concrete subclasses. The Super-class decides the algorithm order, but the sub-classes decide how to fulfill the steps.
  • Encapsulate what Varies: Encapsulate overall algorithms in superclass and the varying part of the algorithm in sub-classes..
  • Program to Interfaces not Implementations: The super-class is an abstract class with abstract methods and hooks that need or can be implemented by its subclasses. Thus, the Super-class adheres to this principle and is used like an interface in the client.
  • Open Closed Principle: HOOKS!!! They allow the sub-classes to modify the existing algorithm without changing the Super-class code.
Note

Violates the principle of Favor Composition over Inheritance, but it does it for a good reason, to control the algorithm, and provide code reuse.