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

Structure and Protocol

Roles and Responsibilities

Benefits and 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 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().

References

<references/>