3

I am reading about Template design pattern. As per my current understanding, Template design pattern can be used when we have an algorithm with defined set of processes(methods) to be done in order. Main players are

1.Abstract Template class providing a template method defining the processes (methods) and the order of execution. Usually this method is made final, so as its behavior is not modified. Few of the processes(methods) mentioned in the template method are provided with default implementation and others depending upon the concrete classes extending the Abstract template class types are left as abstract.

2.Concrete classes extending the Template method. These override the default methods if necessary and provided the implementation for the abstract methods defined in the Abstract Template class.

I tried searching for its implementation in JDK, i looked at java.io classes after reading that these classes implement this pattern. I was not able to find any method defining a set of processes(methods) and the order of execution.

Please provide your valuable inputs.

jaco0646
  • 15,303
  • 7
  • 59
  • 83
nits.kk
  • 5,204
  • 4
  • 33
  • 55
  • I'm afraid I don't fully understand... Do you want examples of the template design pattern in the JDK, or do you want a kind of utility class in the language that lets you implement the template pattern on classes of your own? In this latter case, there's no such utility, so you have to implement your classes that model the template pattern. – fps Feb 22 '16 at 18:06
  • @FedericoPeraltaSchaffner, I understand there is no utility classes or methods to implement design patterns, I wanted examples of Template pattern in JDK. – nits.kk Feb 22 '16 at 18:53

2 Answers2

4

A simple example is java.io.OutputStream.

The template method is
public void write(byte b[], int off, int len).

It calls the abstract method
public abstract void write(int b),
which must be implemented by a subclass of OutputStream.

In this case the invariant portion of the template is the basic error handling that is common to every OutputStream, while the variant portion of the template is the actual writing, which is specific to each concrete implementation.

Your understanding of the pattern is correct; however, it needn't be that complex. Basically, any concrete method which calls an abstract method in the same class is a template method.

jaco0646
  • 15,303
  • 7
  • 59
  • 83
  • 1
    Thanks for the answer. After reading your answer one more point came to my mind. If abstract class calls any abstract methods (or non abstract method with default implementation , may be overridden, may be not by the sub classes) does it is an example of Template design pattern. Abstract classes do have common behavior and any method having calls to the methods defined inside it is an example of Template design pattern, Please correct my understanding if I am wrong. Thanks :) – nits.kk Feb 22 '16 at 18:57
  • 2
    No, a concrete method calling another concrete method in an abstract class is not a template. The variant portion of the template, i.e. the called method, is abstract according to the pattern. – jaco0646 Feb 23 '16 at 00:21
1

To be more specific:

Non-abstract methods calling abstract methods inside their implementation can be categorized as Template Methods.

Template_method define the program skeleton of an algorithm in an operation, defering some steps to subclasses. If you define complete operation as an abstract method, sub-classes will have full control to change the skeleton of the algorithm and hence abstract methods are not classified as Template methods.

e.g. Reader class in IO.

 public int read() throws IOException {
        char cb[] = new char[1];
        if (read(cb, 0, 1) == -1) // this is an abstract method
            return -1;
        else
            return cb[0];
    }

Here

abstract public int read(char cbuf[], int off, int len) throws IOException; is abstract method.

Implementation of this method can be found in BufferedReader

public int read(char cbuf[], int off, int len) throws IOException {

}
Ravindra babu
  • 37,698
  • 11
  • 250
  • 211