Template design pattern in JDK, could not find a method defining set of methods to be executed in order

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.

Leave a Comment