Template method Pattern – Design Patterns in Java/J2EE

A Template method pattern provides a skeleton for performing any sort of algorithm or an operation, and it allows the sub-classes to re-define part of the logic. Let us directly get into an example to clarify things in a much better manner. For example, if we wish to write a String Decorator class, which decorates the given string by appending some characters at the beginning, left, right and top of the string.

also read:

For that, let us write a class which looks like the following,

StringDecorator.java

public abstract class StringDecorator {

    public final void decorate(){
        // Call all the other methods from here.
    }

    protected boolean isUpperCase(){
        return true;
    }

    protected abstract char getTopCharacter();

    protected abstract char getLeftCharacter();

    protected abstract String getString();

    protected abstract char getRightCharacter();

    protected abstract char getBottomCharacter();
}

The above class defines some abstract methods (for getting the various characters around which the string will be decorated) which is intended to be given implementation by one or more sub-classes. Let us have a deeper look over the decorate() method.

public final void decorate(){
    char topChar = getTopCharacter();
    char leftChar = getLeftCharacter();
    String str = getString();
    char rightChar = getRightCharacter();
    char bottomChar = getBottomCharacter();

    for(int i=0; i<str.length() + 2; i++){
        System.out.print(topChar);
    }
    System.out.println();

    System.out.print(leftChar);
    if (isUpperCase()){
        System.out.print(str.toUpperCase());
    }else{
        System.out.print(str.toLowerCase());
    }

    System.out.print(rightChar);

    System.out.println();
    for(int i=0; i<str.length() + 2; i++){
    System.out.print(bottomChar);
    }
}

The above decorate() method contains the original logic for decorating a string object, which in turns calls the abstract methods to get the various characters for decorating. Since this method executes the core logic flow, this method is often called as the template method. Note that this method is also marked as final, meaning that sub-classes cannot override this method, so that the execution flow doesn’t get changed.

Now, let us see one concrete implementation of the above class. Given below is the code snippet for the same,

SimpleStringDecorator.java

package tips.pattern.template;
public class SimpleStringDecorator extends StringDecorator {
    @Override
    protected char getBottomCharacter() {
        return '#';
    }
    @Override
    protected char getLeftCharacter() {
        return '(';
    }
    @Override
    protected char getRightCharacter() {
        return ')';
    }
    @Override
    protected String getString() {
        return "javabeat.net";
    }
    @Override
    protected char getTopCharacter() {
        return '#';
    }
    public static void main(String[] args) {
        StringDecorator decorator = new SimpleStringDecorator();
        decorator.decorate();
    }
}

See how the above class redefines the abstract methods thereby providing its own set of characters for decorating the string object. If you run the above program, you will see in the console similar to the following,

##############
(JAVABEAT.NET)
##############

To summarize, the method StringDecorator.decorate() is the template method which contains the skeleton of the decoration operation. Note that since the decoration operation depends on getting the various characters, we need a concrete sub-class which can provide this character set and hence we introduce a new class called SimpleStringDecorator.

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*