Spring Framework

Spring Projects

Spring Project 1

Template Design Pattern in Spring
Previous Home Next

Introduction: Template is a behavior design pattern i.e used to implement such method which invoked predefine sequence of steps. Implementation some of all the steps can be varies but that sequence can not be varies but that sequence can not be change.

Database operation implemented by JDBC are the candidate for template design pattern because each database operation as following steps:

  1. Connection is created / obtained.
  2. Statement is created.
  3. Query is Executed
  4. Connection close

This sequence can not be altered but individual steps can be implemented in multiple ways.


  1. Connection can be created using DriverManager, It can be obtained from a pool or can be obtained from a directory server.
  2. Statement can be created in multiple ways using Statement, PreparedStatement and CallableStatement interface.
  3. Usualy method which involves the predefine sequence of steps out of which some step can be varies are implemented in such a way that common steps are repeated in multiple implementation.
  4. Template design pattern tries to be remove this problem to facilities definition of common steps only one in a template method and by allowing individual develop to provide the own implementation to the variable steps following example demonstrate the concept of template design pattern.

This is a template method that facilitate of two number can be read from the keyboard from a file or from the network. Result can be displayed on the console, can be saved to a file or can be sent over the network i.e out of these steps two are variable.

abstract class Adder
public void add()
//template method that uses method steps to define the sequence
int numArray[]=getNumber();
int result=numArray[0]+numArray[1];
public abstract int[] getNumber();//stub method representing.
public void manageResult(int r);//variable step.
Previous Home Next