Monday, 8 January 2018

Design Patterns | Template Method Pattern

Template method defines the steps to execute an algorithm and it can provide the default implementation that might be common for all or some of the subclasses.

In object-oriented programming, a concrete class is created that provides the steps of algorithm design. Steps that are considered invariant are implemented inside the base class. The steps that are considered to be variant are given a default implementation or none at all. These variant steps must be supplied by concrete derived subclasses. Thus the general algorithm is saved in one place but the concrete steps may be changed by the subclasses.



Real life example, the Interview process
Most of the companies have well-defined interview process like the first round will be written followed by second-round technical, third managerial and last one is HR. We can define a template method in the base class to execute the steps in above-defined sequence. However, the behavior of each round may vary from one company to another. Like, Expedia can ask written on the system (coding round) and orange can ask to complete it using pen-paper. So we can override the implementation accordingly.

public abstract class InterviewProcess {

    abstract void written();

    abstract void technical();

    abstract void manager();

    abstract void hrRounds();

    // Template method - Sequence of processes.
    public final void judgeCandidate() {
        written();
        technical();
        manager();
        hrRounds();
    }
}

public class OrangeInterview extends InterviewProcess {

    @Override
    void written() {
        System.out.println("written : Orange!");
    }

    @Override
    void technical() {
        System.out.println("technical : Orange!");
    }

    @Override
    void manager() {
        System.out.println("manager : Orange!");
    }

    @Override
    void hrRounds() {
        System.out.println("hr rounds : Orange!");
    }
}

public class ExpediaInterview extends InterviewProcess {

    @Override
    void written() {
        System.out.println("written : Expedia!");
    }

    @Override
    void technical() {
        System.out.println("technical : Expedia!");
    }

    @Override
    void manager() {
        System.out.println("manager : Expedia!");
    }

    @Override
    void hrRounds() {
        System.out.println("hr rounds : Expedia!");
    }
}

public class TestTemplate {

    public static void main(String[] args) {
        InterviewProcess orangeInterview = new OrangeInterview();
        orangeInterview.judgeCandidate();

        System.out.println("\n");

        InterviewProcess expediaInterview = new ExpediaInterview();
        expediaInterview.judgeCandidate();
    }
}

Output:
written : Orange!
technical : Orange!
manager : Orange!
hr rounds : Orange!

written : Expedia!
technical : Expedia!
manager : Expedia!
hr rounds : Expedia!

Template Method Pattern in Core libraries
1. All non-abstract methods of java.io.InputStream, java.io.OutputStream, java.io.Reader and java.io.Writer.
2. All non-abstract methods of java.util.AbstractList, java.util.AbstractSet and java.util.AbstractMap.
3. javax.servlet.http.HttpServlet, all the doXXX() methods by default sends an HTTP 405 "Method Not Allowed" error to the response. You're free to implement none or any of them.

Few important points about Template method pattern
1. Template method should be final
The step of execution always defined in the template method and they should not change. another programmer can override the behavior of the template method if it is not declared as final.

2. Template method should consist of certain steps whose order is fixed and for some of the methods; implementation differs from base class to subclass.

3. Most of the times, subclasses call methods from superclass however in template pattern, superclass template method calls methods from subclasses.

Usage
While developing Enterprise Application using the Spring Framework, you will encounter several template methods built in the framework. The handleRequestInternal() method of the abstract AbstractController class is one such template method. In Spring applications, when you need to implement some common logic, with some subclass specific logic interleaved with it, use the Template Method pattern to reduce code duplication and code maintenance nightmares in the application.

The template pattern is useful working with auto-generated code. The challenge of working with generated code is that any refinement of the source material will lead to changes in the generated code, which could overwrite hand-written modifications. This may be solved using the Template pattern, by generating abstract code and making hand-written modifications to a concrete subclass or implementation class. When used with code generation, this pattern is sometimes referred to as the Generation Gap pattern.



No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...