CHAPTER 6

image

Template Method Patterns

GoF Definition: Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. The template method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure.

Concept

In a template method, we define the minimum or essential structure of an algorithm. Then we defer some functionalities (responsibilities) to the subclasses. As a result, we can redefine certain steps of an algorithm by keeping the key structure fixed for that algorithm.

Real-Life Example

Suppose we want to make pizza. The basic mechanism is the same, but extra materials are added based upon the customer’s choice—whether he/she wants a vegetarian pizza or a non-vegetarian pizza.

Computer World Example

For an engineering student, in general, most of the subjects in the first semester are common for all concentrations. Later, additional papers are added in his/her course based on his/her specialization (Computer Science, Electronics, etc.).

Illustration

Here we have tried to implement our example with a similar concept. The parts of the program are described by the following structure in the solution. We have implemented a simple program to design engineering courses to illustrate the template method pattern. We are assuming that all engineering students need to pass mathematics and soft skills in their initial semesters to obtain their final degree. Later, some special papers will be added to their course based on their concentration (e.g., computer science or electronics).

UML Class Diagram

9781484218013_unFig06-01.jpg

Package Explorer view

High-level structure of the parts of the program is as follows:

9781484218013_unFig06-02.jpg

Implementation

// BasicEngineering.java

package engineering.papers;

public abstract class BasicEngineering
{
    // Papers() in the template method
    public void Papers()
    {
        //Common Papers:
        Math();
        SoftSkills();
        //Specialized Paper:
        SpecialPaper();
    }
    //Non-Abstract method Math(), SoftSkills() are //already implemented by Template class
    private void Math()
    {
        System.out.println("Mathematics");
    }
    private void SoftSkills()
    {
        System.out.println("SoftSkills");
    }
    //Abstract method SpecialPaper() must be implemented in derived classes
    public abstract void SpecialPaper();
}

// ComputerScience.java

package engineering.papers;

public class ComputerScience extends BasicEngineering
{
        @Override
        public void SpecialPaper()
    {
           System.out.println("Object Oriented Programming");
    }
}

// Electronics.java

package engineering.papers;

public class Electronics extends BasicEngineering
{
        @Override
    public void SpecialPaper()
    {
                 System.out.println("Digital Logic and Circuit Theory");
    }
}

// TemplateMethodPatternEx.java
package template.method.pattern.Demo;
import engineeringPapers.*;

class TemplateMethodPatternEx
{
    public static void main(String[] args)
    {
        System.out.println("***Template Method Pattern Demo*** ");

        BasicEngineering bs = new ComputerScience();
        System.out.println("Computer Sc  Papers:");
        bs.Papers();
        System.out.println();
        bs = new Electronics();
        System.out.println("Electronics Papers:");
        bs.Papers();

    }
}

Output

9781484218013_unFig06-03.jpg

Note

  1. “Reuse of code” is the fundamental aim of this method. This is why, in general, we can see the use of this pattern in many class libraries.
  2. GoF suggests that we explicitly decide which operations should be hook operations (we have freedom—we may or may not override the methods) and which operations should be abstract operations (we do not have a choice—overriding is a must in this case) during the development of a template method.

What is the major precaution we should take for implementing this method?

We need to minimize the number of incomplete/abstract operations. (In Java, remember: an abstract method does not have a body). Otherwise, each of the subclasses needs to override them and the overall process will lose the effectiveness of this design pattern.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.235.8