CSC/ECE 517 Fall 2012/ch2a 2w19 is

From Expertiza_Wiki
Revision as of 05:18, 24 October 2012 by Smbuch (talk | contribs) (Created page with "<big>'''Template Method Pattern'''</big> __TOC__ ==Introduction== ==Problems Solved by this pattern== ==Structure and Protocol== ==Roles and Responsibilities== ==Benefits a...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Template Method Pattern

Introduction

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/>