Template method pattern with Example in java

Template method pattern in java with Example



Template method pattern is one of the most widely used design patterns. Your first question will be where and what places we need the template method design pattern , answer is to encapsulate the algorithms.

Need of the template method pattern :

1. When we need the skeleton or protocol of the algorithm and its steps but with differed implementation of some tasks.
2. When we want to generalize the common part of different operations and but want the sequence or structure same.


How we achieve it with Template method pattern.

Let see what exactly the Template method pattern does.

The template method patterns defines one method which has the algorithm steps and also allows subclasses to provide the implementation for one or more types.

Lets understand it step by step:



1. Suppose we have two types of files and we want to review the two files. First file is excel file and another is word document.

2. Both the classes use functions as following

Excel File operations :
1. openFile
2. readExcelFile
3. showResult
4. closeFile

Word File Operations :

1. openFile
2. readWordFile
3. showResult
4. closeFile

3. In above point we can see that both operations we have same steps and some common methods. Here we can see the duplicate code. So we have two task to remove the duplicate code and maintain the steps to review the file.

4. For above purpose lets create one abstract class Reviewer and have the steps defined inside it. We need one method to which call as Template method and some abstract methods and some common operational methods.

public abstract class Reviewer {
    final void reviewProcess(){
        openFile();
 readFile();
        showResult();
        closeFile();

    }
    abstract void readFile();
    void openFile(){
         System.out.println(“Opening the file”);
    }
    
void closeFile(){
         System.out.println(“Closing the file”);
} 
void showResult(){
         System.out.println(“Showing the result”);
}

}

5. In above code snippet we can see that reviewProcess method is declared final as we want these steps to be unchanged as these steps represent sequence in the algorithm. We have declared readFile as abstract other methods as implemented in abstract class. So any class inheriting the class Reviewer don’t have to worry about these methods. They need to implement the abstract method.
public class WordReviewer extends Reviewer{
    public void readFile(){
        System.out.println(“Reading the word file”);
    }

}

Now what is the advantages of the above steps :

1. Reviewer class has the control over the process and it has algorithm.
2. It increases chances of being reused at many places.
3. This template method provides plug where another type of file reviewer can easily be plugged into.


Hooks on Template Method :

Hook is also very useful part in the Template Method Pattern :

What is the HOOK ???

A hook is the method which is declared in abstract class but the implementation is either empty or default.

Why is the Need of a Hook?

Well for subclasses it is like trump card and gives them ability to hook into the algorithms at various points and another good news is if they don’t want to use the hook it is OKAY.

It basically gives subclass some options to play with such as to implement optional part, act or react on certain steps of an algorithm.


Example of Template Method pattern.

1. Skeleton of an algorithm in base class template method WorkSchedule
public abstract class WorkSchedule {
    
    public final void threeHourOperaton(){
        startTask();
        hourOneTask();
        hourTwoTask();
        hourThreeTask();
    }
    protected abstract void startTask();
    protected abstract void hourOneTask();
    protected abstract void hourTwoTask();
    protected abstract void hourThreeTask();
    
}  

2. Implementation of steps which are included in base class.
public class CleaningOperation extends WorkSchedule{
    public  void startTask(){
        System.out.println("Cleaning opertion started");
    }
    public void hourOneTask(){
        System.out.println("Cleaning step 1");
    }
    public  void hourTwoTask(){
        System.out.println("Cleaning step 2");
    }
    public  void hourThreeTask(){
        System.out.println("Cleaning step 2");
    }
}

public class Maintenance extends WorkSchedule{
    public  void startTask(){
        System.out.println("Maintenance task started");
    }
    public void hourOneTask(){
        System.out.println("Maintenance step 1");
    }
    public  void hourTwoTask(){
        System.out.println("Maintenance step 2");
    }
    public  void hourThreeTask(){
        System.out.println("Maintenance step 3");
    }
}


3. Implementation of template method design pattern in java
public class WorkScheduler {
    
    public static void main(String[] args){
        WorkSchedule cleaningWork = new CleaningOperation();
        WorkSchedule maintenance = new Maintenance();
        cleaningWork.threeHourOperaton();
        maintenance.threeHourOperaton();
    }
    
    
}


Thus you have seen the template method design pattern with java example.
Share on Google Plus

About Pranav

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment