Monday, February 1, 2010

The Template Method Pattern

Note: CLICK IMAGE TO ENLARGE.

About
:The Template Pattern uses the inheritance beautifully to take advantage of code reuse. No composition is used because of the strict nature of the circumstances where it should be used. The subclasses are bound to implement the abstract behavior of superclass and give the implementation in their own way and yet the sequence of algorithm is made unchangeable by making the method, of superclass which has the algorithm, final.

Definition : Defines the skeleton of an algorithm in a method, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.

When to use it: When you have a fixed algorithm to execute from a superclass and which must not be changed in sequence but should behave differently in subclass specific way then its good to use this pattern for
1)integrity reason, of the algorithm's sequence
2)code reuse, if there are many subclasses which all depend on superclass implementation of certain functions.

How to use it : In this pattern, a superclass will define a method, which has a fixed sequence in calling some methods, we call it template method or an algorithm. Superclass will have certain methods whose implementation is given by superclass itself and some methods which are defined as abstract, which must be implemented by the subclasses. Define this template method as final in superclass so that, subclasses can not change the sequence of algorithm and still contribute subclass-specific way by giving their own implementation of abstract methods. So the client will call the final method of subclass, inherited from superclass, and get the implementation of the algorithm in the subclass-specific way.

Example : The diagram 1.1 shows the initial design where two different classes has an algorithm which has same sequence of methods but each having its implementation of those methods in their own way. Here, the same code is scattered across the classes.



The diagram 1.2 shows the Template Method Pattern, where we take out the common behavior of subclasses into superclass creating a common point of change which will reflect to all the subclasses. We define the subclass-specific methods in superclass as abstract thus making mandatory for subclass to give implementation. Also, we create a new method, Template Method(on which the name is given to this pattern), by calling the methods in the sequence by which we want our common algorithm to be executed. And , defining it as final so that we are sure that no subclass can change the sequence and just give its own implementation.


Conclusion : To conclude, the sequence,which must be same throughout the subclasses, is defined by the superclass and some part of the algorithm is executed by subclasses such design is a very good example of secure algorithm ensuring fixed sequence but with added touch of subclasses who give implementation differently.

No comments:

Post a Comment