Design Patterns (12) - Template Patterns (Template)

Defining the skeleton of an algorithm in an operation, while deferring some steps to subclasses, template methods allow subclasses to redefine certain steps of the algorithm without changing the structure of the algorithm.

The popular understanding is: to complete a thing, there are a fixed number of steps, but each step is different according to the object, and the implementation details are different; you can define a general method to complete the thing in the parent class, according to the completion event. The required steps call the implementation method of each step. The concrete implementation of each step is done by subclasses.

 

For example, our cooking can be divided into three steps (1) preparation of ingredients (2) specific cooking (3) serving the dishes to the guests, these three steps are the skeleton of the algorithm; however, the ingredients and methods needed to make different dishes, And how to dress it up for guests to enjoy is different. This is a different implementation detail.

  Let's implement the code as follows

 a. Let’s first write an abstract cooking parent class:  

public abstract class DodishTemplate {    
    /**
     * Specific whole process
     */
    protected void dodish(){
        this.preparation();
        this.doing();
        this.carriedDishes();
    }
    /**
     * Preparation
     */
    public abstract void preparation();
    /**
     * cooking
     */
    public abstract void doing();
    /**
     * serve
     */
    public abstract void carriedDishes ();
}

 

b. Come down and make two tomato scrambled eggs (EggsWithTomato) and braised pork (Bouilli) to implement the abstract method in the parent class

/**
 * Scrambled eggs with tomatoes
 * @author aries
 */
public class EggsWithTomato extends DodishTemplate{

    @Override
    public void preparation() {
        System.out.println("Wash and cut tomatoes, beat eggs.");
    }

    @Override
    public void doing() {
        System.out.println("Pour the eggs into the pot, then pour in the tomatoes and fry together.");
    }

    @Override
    public void carriedDishes() {
        System.out.println("Put the fried Xihong Temple eggs into a plate and serve them to the guests.");
    }

}

 

/**
 * Braised pork
 * @author aries
 *
 */
public class Bouilli extends DodishTemplate{

    @Override
    public void preparation() {
        System.out.println("Cut pork and potatoes.");
    }

    @Override
    public void doing() {
        System.out.println("Put the cut pork into the pot and fry for a while, then pour in the potatoes and fry and stew.");
    }

    @Override
    public void carriedDishes() {
        System.out.println("Put the prepared braised pork in a bowl and serve it to the guests.");
    }

}

 Test class:

 

public class App {
    public static void main(String[] args) {
        DodishTemplate eggsWithTomato = new EggsWithTomato();
        eggsWithTomato.dodish();
        
        System.out.println("-----------------------------");
        
        Boiled DodishTemplate = new Boiled();
        boiled.dodish();
    }
}

 

 

Advantages of template pattern:

 (1) The implementation of specific details is defined in the subclass, and the subclass defines the detailed processing algorithm without changing the overall structure of the algorithm.

 (2) The basic technology of code reuse is particularly important in database design.

 (3) There is a reverse control structure, in which a parent class invokes the operation of its subclass, and the subclass extends the parent class to add new behaviors, which conforms to the "opening and closing principle".

insufficient:

    Each different implementation needs to define a subclass, which will lead to an increase in the number of classes and a larger system.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326033701&siteId=291194637