2

From what I understand, Template method is nothing but ordinary method that calls virtual or abstract methods defined in child class. Am I right, or is there something else important about this pattern that I miss?

abstract class Foo {
  public void IamTemplateMethod() { // which will be called in child class object
    method1(); // because this...
    method2(); // ...or this method was called in me
  }
  public virtual void method1() { ... } // to be overriden in child class
  public abstract void method2() { ... } // to be defined in child class
}

If I am right, are there any other common ways to implement Template method?

Jan Turoň
  • 31,451
  • 23
  • 125
  • 169

3 Answers3

11

Yes. Most patterns are nothing special, but just smart approaches that seems to suit certain situations well, but still using normal OO principles (inheritance, polymorphism, abstraction etc.).

What the template method is saying is that sometimes, you need to do some common logic, with some sub-class specific logic interleaved with it. So the specific logic that you want to leave for each sub-class is defined as an abstract / virtual method that is left for the concrete class to implement, while the common business logic goes around it.

If you want to make sure that the common logic is not overridden you can also mark the template method not to be overridden (with the final keyword in Java for example), so you ensure that the common code you want to be always executed is always enforced, while allowing the sub-class to override the bits you want it to.

Think of it like a document template. The headings, footer and common elements will be there fixed and always the same, and then the specific details of what the specific document is being used for fill the blanks in between.

jbx
  • 21,365
  • 18
  • 90
  • 144
  • ah - finally I understand why to use `final` keyword, thanks! :-) – Jan Turoň Feb 25 '14 at 13:56
  • Cool :). You can also use it with classes (not to allow anyone to override them) and with variables (in which case their value will not be allowed to change once they are initialised, they will be immutable) – jbx Feb 25 '14 at 13:57
2

Template pattern provide a common sequence following for all the children of that method. So Template Pattern defines a final method which tells the sequence of execution.

abstract class Foo {
    public void final initilialize(){
        method1();
        method2();
        method3();
    }
    public void method1(){...}
    public void method2(){...}
    public void method3(){...}
}

Now child classes can extend Foo class. And Reference can be created as :

Foo obj1=new child();

For more information look into http://www.tutorialspoint.com/design_pattern/template_pattern.htm

Prateek
  • 342
  • 1
  • 3
  • 15
1

Example from HeadFirst book.

You have to brew tea and coffee. In general they both have the same steps like boiling water and pour into cup but also they have some different steps e.g adding condiments - for tea we can add lemon, for coffee we can add milk. So we create a general (template) class CaffeineBeverage which is abstract:

public abstract class CaffeineBeverage {
    public final void prepareRecipe() {
        boilWater();
        brew();
        pourInCup();
        addCondiments();
    }

    abstract void brew();

    abstract void addCondiments();

    void boilWater() {
        System.out.println("Boiling water");
    }

    void pourInCup() {
        System.out.println("Pouring into cup");
    }

Notice that pourInCup and boilWater are not abstract because they are exactly the same for both tea and coffee.

Now we create Tea class which extends our general CaffeineBeverage class and we define behaviour for e.g adding condiments:

public class Tea extends CaffeineBeverage {
    @Override
    public void brew() {
        System.out.println("Steeping the tea");
    }

    @Override
    public void addCondiments() {
        System.out.println("Adding Lemon");
    }
}

and Coffee class, which also extends CaffeineBeverage class:

public class Coffee extends CaffeineBeverage {
    @Override
    public void brew() {
        System.out.println("Dripping Coffee through filter");
    }

    @Override
    public void addCondiments() {
        System.out.println("Adding Sugar and Milk");
    }
}

and we can create test class for template method design pattern:

public class CaffeineBeverageTest {
    public static void main(String[] args){
        Tea tea = new Tea();
        tea.prepareRecipe();

        Coffee coffee = new Coffee();
        coffee.prepareRecipe();
    }
}

so as being said - create abstract class and methods inside. Use abstract methods for different behavior and normal methods with body for common behaviour.

Michu93
  • 5,058
  • 7
  • 47
  • 80