Template Method Patterns for Design Patterns (Behavioral Patterns)

Keywords: Database MySQL Oracle JDBC

[TOC]

I. Pattern Definition

The template method pattern defines some skeleton methods in an abstract class, and then defers some methods to the inherited class through the method of class inheritance. Template method pattern is a kind of behavioral pattern, and it is a common method. It does not belong to the object behavioral pattern, because it is only implemented through class inheritance.

Template Method Pattern: Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.

II. Model roles

  • AbstractClass
  • ConcreteClass (Implementation Class)

3. Model Analysis

Template method, a method that encapsulates and combines basic methods in an abstract class to form a general algorithm or a general behavior.

Components of the template method:

  • Abstract Method
  • Concrete Method
  • Hook Method

Abstract class code:

public abstract class AbstractClass
{
    public void templateMethod()  //Template method
    {
        primitiveOperation1();
        primitiveOperation2();
        primitiveOperation3();
    }
    public void operation1()    //Basic Method - Specific Method
    {
        //Implementation code
    }
    public abstract void operation2();    //Basic method - abstract method
    public void operation3()    //Basic method - hook method
    {
    }
} 

Specific implementation class code:

public abstract class ConcreteClass
{
   /**
    * Basic method - abstract method
    */
    public abstract void operation2(){
        //Concrete realization
    }    
    
    /**
    * Basic method - hook method
    */
    public void operation3(){
        //Concrete realization
    }
} 

Subclasses do not explicitly invoke methods of parent classes, but implement specific business methods through inheritance methods. That is to say, the parent controls the invocation of subclasses. This mechanism is called Hollywood Principle. The Hollywood Principle is defined as "Don't call us, we'll call you".

IV. Specific examples

Examples of database operations. Database operations are divided into connection, opening, using and closing steps. Now we will use mysql, oracle, db2 and other relational databases to write the database operation tool class. For the use of these different databases, in fact, the code of the connection is different, while the code of other operations is similar, so we can use the template method to reuse the code.

ps: This example comes from Design Patterns A book, slightly changed

Template method

public abstract class DBOperator
{   
//Abstract method
    public abstract void connDB();
    public void openDB()
    {
        System.out.println("Open the database");
    }
    public void useDB()
    {
        System.out.println("Using databases");
    }
    public void closeDB()
    {
        System.out.println("close database");    
    }
    //Template method
   public void process()
   {
    connDB();
    openDB();
    useDB();
    closeDB();
   }
}

mysql database

public class DBOperatorMysql extends DBOperator
{
    public void connDB()
    {
        System.out.println("Use JDBC-ODBC Bridging connection Mysql data base");      
    }
}

Oracle database

public class DBOperatorOracle extends DBOperator
{
    public void connDB()
    {
        System.out.println("Use JDBC-ODBC Bridging connection Oracle data base");     
    }
}

call


class Client
{
    public static void main(String a[])
    {
        DBOperator db1;
        
        db1=new DBOperatorMysql();
        db1.process();
        db1=new DBOperatorOracle();
        db1.process();
    }
}

V. Pattern application scenarios

  • Spring, Struts 2 framework applications, such as framework initialization have applications
    ...

Posted by doofystyle on Sun, 05 May 2019 10:16:38 -0700