Thursday, March 3, 2011

Template Method Pattern

Template method defines the skeleton of Algorithm(steps of algorithm) in a method, differing some steps to subclasses. Template method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.
The objective is to ensure that algorithm's structure stays unchanged, while subclasses provide some part of implementation.

This pattern is all about creating a template for an algorithm. Here Template is nothing but a method; more specifically it is a method that defines an algorithm as a set of steps.
Let us look at an example :

abstract class Generalization {
// 1. Standardize the skeleton of an algorithm in a
//"template" method
public final void findSolution() {
// 2. Common implementations of individual steps are defined
//in base class
protected void stepOne() {
System.out.println( "Generalization.stepOne (Common)" );
// 3. Steps requiring particular impls are "placeholders"
//in the base class
abstract protected void stepTwo();
abstract protected void stepThr();
// 4. This is hook method (concrete method) which
//provides default implementation, but
//allows subclasses to override if necessary.
protected void hook() {
System.out.println( "This is default
implementation of hook method"
class SpecializationOne extends Generalization {
// 5. Derived classes can override placeholder methods
protected void stepThr() {
System.out.println( "SpecializationOne.stepThr" );
protected void stepTwo() {
System.out.println( "SpecializationOne.stepTwo" );
class SpecializationTwo extends Generalization{
// 5. Derived classes can override placeholder methods
protected void stepTwo() {
System.out.println( "SpecializationTwo .stepTwo" );
protected void stepThr() {
System.out.println( "SpecializationTwo.stepThr" );
protected void hook() {
System.out.println( "SpecializationTwo.hook" );

Hook is a method that is declared in abstract class, but only given empty or default implementation. This gives the subclasses the ability to hook into the algorithm at various points, if they wish. Subclass is also free to ignore the hook method.
When to use abstract method and when to use hooks?
Use abstract methods when your subclass must provide the implementation. Choose hook when it is optional for subclasses to implement it.

Hollywood Priciple:

Don't call us we will call you.
Hollywood Priciple allows us to prevent dependency rot.
With the hollywood priciple, we allow low level components to hook themselves into a system. But high level components decide when they are needed and how... In other words high level components give 'don't call us, we will call you' treatment to low level components.
The connection between Hollywood Principle and Template Method Pattern is probably somewhat apparent.
In our above example Generalization is our high-level component. It has control over the sequence of methods to be called and calls on the subclasses only when they are needed for implementation of a method.
Client will depend on Generalization rather than concrete SpecializationOne or SpecializationTwo which reduces dependencies in overall system.
The subclasses never call abstract class directly without being 'called' first.

Difference between Dependency Inversion Principle and Hollywood Principle
Dependency Inversion principle tells us to avoid use of concrete classes and instead work as much as possible with abstractions.
The Hollywood princple is a technique for building frameworks or components so that lower level components can be hooked into computation but without creating dependencies between lower level components and higher level layers.
So they both have goal of decoupling but Dependency Inversion Principle makes a strong and general statement about how to avoid dependencies in design.
The Hollywood principle gives us a technique for creating designs that allow low level structures to interoperate while preventing other classes from becoming too dependent on them.

Advantages of Using The Template Method Design Pattern
1. No code duplication between the classes
2. Inheritance and Not Composition
Whenever a design pattern uses inheritance as a key element instead of composition, you need to consider the reason. To understand the reason, you need to fully understand the principle of favoring composition over inheritance as a general principle in good OOP. The principle's established because of certain advantages of composition over inheritance, especially the composition advantage of not breaking encapsulation. However, Gamma, Helm, Johnson and Vlissides (GoF) note that inheritance also has certain advantages. One such advantage is that when using subclasses where some but not all operations are overridden, modifying the reused implementation is easier. Because the Template Method design pattern does exactly that—uses some but not all operations that can be overridden to achieve flexibility—it incorporates a key advantage of inheritance over composition.
3. By taking advantage of polymorphism the superclass automatically calls the methods of the correct subclasses.

Comparision of different Patterns:
Template Method Pattern : Subclasses decide how to implement steps in an algorithm.
Strategy Pattern : Encapsulates interchangable behaviours and use delegation to decide which behaviour to use.
Factory Method: Subclasses decide which concrete classes to create.

No comments:

Post a Comment