Design Patterns Series - Part Two: Template Method

Design Patterns Series – Part Two: Template Method

A typical template method pattern consists of the abstract base class which defines one or more template methods (usually set as final) and some abstract methods which the subclass has to implement. Template method defines common algorithm where some of the steps are implemented by subclasses.

A Template method pattern provides a skeleton for performing any sort of algorithm or an operation, and it allows the sub-classes to re-define part of the logic

This is one of the easiest design patterns and has a very simple UML model:

AmazonKindle.java

package com.softwarepassion.templatemethodpattern;


/**
 * Defines a template for assembling Amazon Kindle Devices
 *
 * @author kris
 * @version 31-01-2011
 */

public abstract class AmazonKindle {

    public AmazonKindle(){
       
    }
    /**
     * Example of Template Method design pattern
     */

    public final void assembleAmazonKindle(){
        createKindleCase();
        setDisplay();
        addModem();
        printSpecs();
    }
   
    public abstract void  createKindleCase();
    public abstract void setDisplay();
    public abstract void addModem();

    private void printSpecs() {
        System.out.println("Printing Amazon Kindle specs..");
    }
}

AmazonKindle6WiFi.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


package com.softwarepassion.templatemethodpattern;

/**
 * Concrete implementation of {@code AmazonKindle}
 * with 6" screen and WiFi modem
 * assembled in a standard way using a template
 * @author kris
 * @version 31-01-2011
 */

public class AmazonKindle6WiFi extends AmazonKindle{

    @Override
    public void createKindleCase() {
        System.out.println("creates a kindle case for 6" kindles");
    }

    @Override
    public void setDisplay() {
        System.out.println("
adds a 6" display");
    }

    @Override
    public void addModem() {
        System.out.println("adds a WiFi modem");
    }

}

AmazonKindle6_3G.java

package com.softwarepassion.templatemethodpattern;

/**
 * Concrete implementation of {@code AmazonKindle}
 * with 6" screen and 3G modem
 * assembled in a standard way using a template
 *
 * @author kris
 * @version 31-01-2011
 */

public class AmazonKindle6_3G extends AmazonKindle{

    @Override
    public void createKindleCase() {
        System.out.println("creates a kindle case for 6" kindles");
    }

    @Override
    public void setDisplay() {
        System.out.println("
adds a 6" display");
    }

    @Override
    public void addModem() {
        System.out.println("adds a 3G modem");
    }
}

Main.java for simple testing..

public class Main {

    /**
     * @param args the command line arguments
     */

    public static void main(String[] args) {
        System.out.println("Assembling amazon kindles");
        //assembling wifi kindle
        AmazonKindle kindle = new AmazonKindle6WiFi();
        Main main = new Main();
        main.assembleAmazonKindle(kindle);
        //assembling 3g kindle
        kindle = new AmazonKindle6_3G();
        main.assembleAmazonKindle(kindle);
    }
    public void assembleAmazonKindle(AmazonKindle kindle){
        kindle.assembleAmazonKindle();
    }

}

Leave a Reply