CSC/ECE 517 Fall 2012/ch2a 2w19 is

From Expertiza_Wiki
Jump to navigation Jump to search

Template Method Pattern

Introduction

A template is a previously set format that other applications use as a starting point, instead of redefining the format each time<ref>http://www.oodesign.com/template-method-pattern.html</ref>. As you can imagine, Software Engineering contains many such patterns in order to develop software and applications. One such pattern is the Template Method Pattern<ref>http://en.wikipedia.org/wiki/Template_method_pattern</ref>. The Template Method Pattern is a behavioral design pattern that defines an algorithm skeleton in a base class, with abstract definitions that derived classes can override to provide concrete behavior. The skeleton is called the template method and the subclasses or derived classes implement the algorithm, sometimes with minor modifications.

In object-oriented design, the base class defines the template by stating the steps of the algorithm using abstract methods that its derived classes implement in a more concrete manner. In this manner, the template method pattern stores the algorithm in one place while other classes implement it, which is good object-oriented(OO) design. Also, the designer can decide which of the algorithm steps are to be standard and which of these are to be customizable<ref>http://sourcemaking.com/design_patterns/template_method</ref>. These standard steps are implemented concretely in the derived classes using the abstract base class's definitions and the customizable steps are either implemented in the derived classes, or not at all. These customizable steps serve as placeholders, or hooks<ref>http://en.wikipedia.org/wiki/Hook_method#Hook_methods</ref>, that the derived classes can supply or not, as they choose. So basically it is used when programmers want to fix the order of operations used in a method, but allow some amount of flexibility such that the subclasses can implement some of these operations in their own manner<ref>http://userpages.umbc.edu/~tarr/dp/lectures/Template.pdf</ref>.

The template method pattern thus, is fundamentally a code reuse technique. It is used whenever programmers want to localize common behavior in subclasses and place it in a super class to avoid code duplication. Hence it is an example of code refactoring.

Problems Solved by this pattern

Template Method pattern is a type of a behavioral design pattern which defines a skeleton for an algorithm which cannot be implemented in that class due to calls to methods that are abstract but can be implemented in the child classes. The template method cannot be overridden in the child classes as it is defined as a final method in the parent class.

Suppose we have a portal where we allow users to book tickets to travel from source to destination. The problem we face here is that, we want all users to use a standard way of booking tickets and travelling. They could travel by bus, train or flight but we want them to follow a certain number of steps in the same order before they can see the confirmation page. We do not want one user to pay first and then select a date and time and a flight and the other user to do this the other way round. However, the mode of transport for the travel may be different for every user and the source and destination may also be different for each of them.

This is the ideal situation in which we would use the Template Method pattern. The parent class will have a template method defined with an algorithm skeleton that says that the steps to be followed will be select the mode of transport, then select the source, then destination and then the date of travel and finally mode of payment.

However, the methods defining the selection of these functionalities can be implemented in different ways depending on what the subclass is. These methods in the parent class are kept abstract but the template method in the parent is made final. The template method calls the functions in a certain order.

Let us show this with a concrete code.

public class Portal
{
   public final void TicketBooking()
   {
      TransportMode();
      if(int i < 4)
      SourceAndDate();
      else
      DestinationAndDate();
      Payment();
 }

 abstract public MethodA();
 abstract public MethodB();
 abstract public MethodC();
}
public class Feedback_response extends Response
{
  TransportMode() {
  //some definition
  }

  SourceAndDate() {
  //some definition
  }

  DestinationAndDate() {
  //some definition
  }

  Payment() {
  //some definition
  }
}

Each of the sub classes inheriting from this class can then provide a concrete definition to these abstract methods in a way they but the method template is final and must be used in the same way.

Structure and Protocol

File:Http://java.dzone.com/sites/all/files/template method.png

Roles and Responsibilities

Benefits and Liabilities

Benefits

Liabilities

Comparison with other design patterns

Comparison with Adapter Design Pattern

Characteristics Adapter Design Pattern Template Method Design Pattern Explanation
Design Pattern Category Structural Pattern Behavioral Pattern In Structural patterns, the focus is on relationships between objects and how they together form larger components whereas in Behavioral patterns, the focus is on communication between objects and what each object does <ref name='StructuralBehavioral'>Design Patterns: Structural and Behavioral http://twu.seanho.com/09spr/cmpt166/lectures/35-patterns.pdf </ref>
Purpose of Use Enables the use of two or more unrelated interfaces, together, as one unit. This can be done by converting one (or more) of the interfaces to adapt to the type of interface that is required by the client. thus, adapter pattern acts as a Wrapper, thus, also called, Wrapper Pattern. <ref name=>JAVA DESIGN PATTERNS http://www.allapplabs.com/java_design_patterns/adapter_pattern.htm </ref> This pattern is used to define and maintain an algorithm directed towards how you want the flow of execution to be. This template method is defined in the parent class and made final, so that all children have to follow the same flow. Purpose of Structural pattern is to act as a wrapper to make different interfaces ready for the client interface as against that of a Behavioral pattern is to define a common algorithm defining method as final in the parents class to act as a template for all the child classes to decide the flow of control.
Example public class SquarePeg {
    public void insert(String str) {
      System.out.println("SquarePeg insert(): " + str);
    }
  }


public class RoundPeg {

    public void insertIntoHole(String msg) {
      System.out.println("RoundPeg insertIntoHole(): " + msg);
    }
  }
public class PegAdapter extends SquarePeg {
    private RoundPeg roundPeg;
    public PegAdapter(RoundPeg peg)  {this.roundPeg = peg;}
    public void insert(String str)  {roundPeg.insertIntoHole(str);}
  }
public class Response

public final void import_response() {

   template_method() { 
     MethodA();
     if(int i < 4)
     MethodB();
     else
     MethodC();
}

abstract public MethodA(); abstract public MethodB(); abstract public MethodC(); }

public class Feedback_response extends Response {

 MethodA() {
 //some definition
 }
 MethodB() {
 //some definition
 }
 MethodC() {
 //some definition
 }

}

In the template method pattern the Response class is the parent class with a final method import_response() which is the template defining the algorithm with the flow of method calls required. The methods, MethodA(), MethodB() and MethodC() are defined as abstract in the parent class and definitions are provided for these in the child class. Any class that extends Response class now has to follow the same algorithm as described in the method import_response().

Comparison with Strategy Design Pattern

Characteristics Strategy Design Pattern Template Method Design Pattern Explanation
Design Pattern Category Behavioral Pattern Behavioral Pattern In Behavioral patterns, the focus is on communication between objects and what each object does. Thus, both these patterns enable user to define an algorithm of some form which determines the flow of control of the program. <ref name='Behavioral'>Strategy Pattern http://content.gpwiki.org/index.php/Strategy_pattern </ref>
Purpose of Use The algorithm defining the flow of control in a Strategy pattern can be changed even after a class that uses that algorithm has been finalized. Thus, the algorithm per say is not final and can be changed. In fact, the algorithm defining function may be declared as part of an interface and left to classes implementing that interface to implement. This template method is defined in the parent class and made final, so that all children have to follow the same flow. Thus, the algorithm defining flow fo control in this cannot cannot be changed under any circumstances. Once defined it is fixed for all children. Purpose of behavioral pattern is to define a common algorithm defining the flow of control of the program, to act as a template for all the child classes to decide their flow of control.
Example interface myInterface {
 int findSum(int x, inty, int z);
 }
 class A implements myInterface {
 public int findSum(int x, int y, int z) {
 //some definition
 }
 class B implements myInterface {
 public int findSum(int x, int y, int z) {
 //some definition
 }
public class Response

public final void import_response() {

   template_method() { 
     MethodA();
     if(int i < 4)
     MethodB();
     else
     MethodC();
}

abstract public MethodA(); abstract public MethodB(); abstract public MethodC(); }

public class Feedback_response extends Response {

 MethodA() {
 //some definition
 }
 MethodB() {
 //some definition
 }
 MethodC() {
 //some definition
 }

}

In the Strategy pattern as described, an interface myInterface has a declaration to the defining algorithm method. All the classes that implement this interface must provide a definition to this method findSum. Thus, each implementing class will provide its own way of defining the flow of control. Thus, this pattern although being a behavioral pattern, is different from template method pattern, in fact almost contrast to it.

In the template method pattern the Response class is the parent class with a final method import_response() which is the template defining the algorithm with the flow of method calls required. The methods, MethodA(), MethodB() and MethodC() are defined as abstract in the parent class and definitions are provided for these in the child class. Any class that extends Response class now has to follow the same algorithm as described in the method import_response().

Comparison with Proxy Design Pattern

Characteristics Proxy Design Pattern Template Method Design Pattern Explanation
Design Pattern Category Structural Pattern Behavioral Pattern In Structural pattern, the focus is on creating ease of design by identifying a simple way to realize relationships between entities. In Behavioral patterns, on the other hand, the focus is on communication between objects and what each object does. Thus, both these patterns enable user to define an algorithm of some form which determines the flow of control of the program. <ref name='Behavioral'>Strategy Pattern http://content.gpwiki.org/index.php/Strategy_pattern </ref>
Purpose of Use The algorithm defining the flow of control in a Strategy pattern can be changed even after a class that uses that algorithm has been finalized. Thus, the algorithm per say is not final and can be changed. In fact, the algorithm defining function may be declared as part of an interface and left to classes implementing that interface to implement. This template method is defined in the parent class and made final, so that all children have to follow the same flow. Thus, the algorithm defining flow of control in this cannot cannot be changed under any circumstances. Once defined it is fixed for all children. Purpose of behavioral pattern is to define a common algorithm defining the flow of control of the program, to act as a template for all the child classes to decide their flow of control. Purpose of the Proxy pattern is use a less complex object in place of the actual object until such a time that the actual object can be created. The less complex object is known as the proxy for the actual object.
Example inteface myInterface {

public abstract void display(); }

class classA implements myInterface {

private void loadImage() { System.out.println("Loading in classA"); }

public void display() { system.out.println("Displaying in classA"); } }

class classB implements myInterface {

private void loadProxyImage(final Sting FILENAME) { filename = FILENAME; }

public void display() { if(image == null) {

  image = new classA(filename);

}

   image.display();

} }

public class Response

public final void import_response() {

   template_method() { 
     MethodA();
     if(int i < 4)
     MethodB();
     else
     MethodC();
}

abstract public MethodA(); abstract public MethodB(); abstract public MethodC(); }

public class Feedback_response extends Response {

 MethodA() {
 //some definition
 }
 MethodB() {
 //some definition
 }
 MethodC() {
 //some definition
 }

}

In the Strategy pattern as described, an interface myInterface has a declaration to the defining algorithm method. All the classes that implement this interface must provide a definition to this method findSum. Thus, each implementing class will provide its own way of defining the flow of control. Thus, this pattern although being a behavioral pattern, is different from template method pattern, in fact almost contrast to it.

In the template method pattern the Response class is the parent class with a final method import_response() which is the template defining the algorithm with the flow of method calls required. The methods, MethodA(), MethodB() and MethodC() are defined as abstract in the parent class and definitions are provided for these in the child class. Any class that extends Response class now has to follow the same algorithm as described in the method import_response().

Conclusion

Behavioral design patterns, generally identify the communication between different objects and classes and how this can be made most efficiently. The behavioral design pattern, Template Method pattern, is one of the most important patterns in certain situations like in cases where we need a static algorithm defined for some purpose. Whenever we do not want the child classes to make a decision about the flow of the algorithm or we do not want the child class interfering with the parents decision, it would be ideal to use this design pattern. It also gives you a very easy way to maintain your code and make changes to it in the future as everything needs to be done only in one place. In conclusion, Template Method pattern can be explained as easily as defining a skeleton of an algorithm in an operation, deferring some steps to sub classes.

References

<references/>