Tuesday, February 3, 2015

Template Method Design Pattern

Design Pattern > Behavioral Design Pattern > Template Method Design Pattern

In some scenarios we want to write classes that follow certain algorithm structure but at the same allow different classes to define their own implementation of steps of algorithm. Template method pattern is helpful in these scenarios.

As per GOF the intent of template method pattern is to "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."

It's not necessory to define all the steps of an algorithm. it depends of your requirements whether you wnat to use default step/operation of given algorithm from super class or want to define some or all operations/steps in your sub class.

There is a template method in super class which declare the order of algorithm operations/steps and sub classes need to define/override these operations( either some or all).At runtime template method calls the sub class operations in an order.

Key Terms :
Abstract Class - 

  • Class with template method
  • Subclasses can not override it's template method
  • Subclasses need to override it's operations declared in template method


Note - Super class need not to be abstract. in that case we need to call a hooks.The hooks are generally empty methods that are called in superclass (and does nothing because are empty), but can be implemented in subclasses.

Concrete Class - 

  •        Sub class that override operations declared in template method

Client Class - 

  • Create subclass instance that refers to superclass
  • calls template method of superclass to execute the algorithm


Java Implementation :

In this example an algorithm is defined to book a ticket in super class BookTicket and there are two subclasse BookTicketOnline and BookTicketIvr ( on phone call )


/**
 * An abstract class with template method
 */
package com.kmingle.template;

public abstract class BookMovieTicket {

public abstract void login();

public abstract void selectSource();

public abstract void selectDestination();

public void enterDetails(){
System.out.println("personal details entered from system");
}

public abstract void makePayment();

public final void bookTicket(){
login();
selectSource();
selectDestination();
enterDetails();
makePayment();
}
}


/**
 * Concrete Class
 */
package com.kmingle.template;

public class BookTicketOnline extends BookMovieTicket {

/* (non-Javadoc)
* @see com.kmingle.template.BookMovieTicket#login()
*/
@Override
public void login() {

System.out.println("online logged in");

}

/* (non-Javadoc)
* @see com.kmingle.template.BookMovieTicket#selectSource()
*/
@Override
public void selectSource() {

System.out.println("online Source selected");

}

/* (non-Javadoc)
* @see com.kmingle.template.BookMovieTicket#selectDestination()
*/
@Override
public void selectDestination() {

System.out.println("online Destination selected");

}

/* (non-Javadoc)
* @see com.kmingle.template.BookMovieTicket#makePayment()
*/
@Override
public void makePayment() {

System.out.println("online payment done");

}

}


/**
 * Concrete Class
 */
package com.kmingle.template;

public class BookTicketIvr extends BookMovieTicket {

/* (non-Javadoc)
* @see com.kmingle.template.BookMovieTicket#login()
*/
@Override
public void login() {

System.out.println("Ivr logged in");
}

/* (non-Javadoc)
* @see com.kmingle.template.BookMovieTicket#selectSource()
*/
@Override
public void selectSource() {

System.out.println("Ivr source selected");

}

/* (non-Javadoc)
* @see com.kmingle.template.BookMovieTicket#selectDestination()
*/
@Override
public void selectDestination() {

System.out.println("Ivr destination selected");

}

/* (non-Javadoc)
* @see com.kmingle.template.BookMovieTicket#makePayment()
*/
@Override
public void makePayment() {

System.out.println("Ivr payment done");

}

}

/**
 * Client Class
 */
package com.kmingle.template;

public class TemplateMethodDemo {

public static void main(String[] args) {

System.out.println("Book ticket online");
BookMovieTicket book_ticket_online = new BookTicketOnline();
book_ticket_online.bookTicket();

System.out.println("\nBook ticket through Phone(Ivr)");
BookMovieTicket book_ticket_ivr = new BookTicketIvr();
book_ticket_ivr.bookTicket();

}

}


OutPut :

Book ticket online
online logged in
online Source selected
online Destination selected
personal details entered from system
online payment done

Book ticket through Phone(Ivr)
Ivr logged in
Ivr source selected
Ivr destination selected
personal details entered from system
Ivr payment done

No comments:

Total Pageviews