Understanding Template Method Pattern

The Template Method Pattern defines the skeleton of an algorithm in a method, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure

Template Method Design Pattern

Below are the classes which makes Tea and Coffee we can make a template method to generalize it

package com.taher.javahunter.template.method.one;
/**
 *
 * @author taher
 */
public class Coffee {

    void prepareReciepe() {
        boilWater();
        brewCoffeeGrinds();
        addSugarAndMilk();
        pourInCup();
    }

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

    public void brewCoffeeGrinds() {
        System.out.println("Dripping Coffee through filter");
    }

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

    public void pourInCup() {
        System.out.println("Pouring into cup");
    }
}
package com.taher.javahunter.template.method.one;

/**
 *
 * @author taher
 */
public class Tea {

    void prepareReciepe() {
        boilWater();
        steepTeaBag();
        addLemon();
        pourInCup();
    }

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

    public void steepTeaBag() {
        System.out.println("Steeping the Tea");
    }

    public void addLemon() {
        System.out.println("Adding Lemon");
    }

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

In Below one final method reveals the template method

package com.taher.javahunter.template.method.two;

/**
 *
 * @author taher
 */
public abstract class CaffeineBeverage {

    final void prepareReciepe() {
        boilWater();
        brew();
        addCondiments();
        pourInCup();
    }

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

    public abstract void brew();

    public abstract void addCondiments();

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

package com.taher.javahunter.template.method.two;

/**
 *
 * @author taher
 */
public class Tea extends CaffeineBeverage{

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

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

package com.taher.javahunter.template.method.two;

/**
 *
 * @author taher
 */
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");
    }
    
}

Hooked on template method – A hook is a method that is declared in the abstract class, but only given an empty or default implementation. This gives subclasses the ability to “hook into” the algorithm at various points, if they wish; a subclass is also free to ignore the hook.

In below class customerWantsCondiments() is a hook method

package com.taher.javahunter.template.method.two;

/**
 *
 * @author taher
 */
public abstract class CaffeineBeverageWithHook {

    final void prepareReciepe() {
        boilWater();
        brew();        
        pourInCup();
        if(customerWantsCondiments()){
            addCondiments();
        }
    }

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

    public abstract void brew();

    public abstract void addCondiments();

    public void pourInCup() {
        System.out.println("Pouring into cup");
    }
    public boolean customerWantsCondiments(){
        return true;
    }
}
Advertisements
This entry was posted in Design Pattern and tagged . Bookmark the permalink.

2 Responses to Understanding Template Method Pattern

  1. Evie says:

    I am regular visitor, how are you everybody?
    This paragraph posted at this site is really good.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s