DEV Community

Shark
Shark

Posted on

template method pattern

1. What is the template method pattern?

The pattern that defines the framework of the processing flow in the parent class and implements specific processing in the subclass is called Template method pattern. Specifically, the methods that make up the template are defined in the parent class. Since they are abstract methods, we only know how the parent class calls these methods. It is the subclass that implements the above abstract methods, so we only need to inherit the abstract class of the template method, and we can implement different processing methods for specific subclasses. Regardless of the specific implementation in the subclass, the processing flow will follow the parent class as defined in .

2. Example program

Here we list the method of printing characters according to the book

  1. Set the abstract class to define public methods and template methods

    public abstract class AbstractDisplay {
      public abstract void open();
    
      public abstract void print();
    
      public abstract void close();
    
      public final void display(){
        open();
        for(int i=0;i < 5;i++){
          print();
        }
        close();
      }
    }
    
  2. After defining the abstract class and the specific template method, we can implement a specific subclass. Here we only list a subclass of printing characters

    public class CharDisplay extends AbstractDisplay {
      private char achar;
    
      public CharDisplay(char achar){
        this.achar = achar;
      }
    
      @Override
      public void open(){
        System.out.print("<<");
      }
    
      @Override
      public void print(){
        System.out.print(achar);
      }
    
      @Override
      public void close(){
        System.out.println(">>")
      }
    }
    
  3. At this point, the specific implementation of our method is completed, and we can test the specific implementation

    public class TemplateMethodTest {
      public static void main(String[] args){
        AbstractDisplay abstractDisplay = new CharDisplay();
    
        abstractDisplay.display();
      }
    }
    
  4. At this point our test is over, let's take a look at its class diagram, and then talk about the template method pattern in detail

Three, detailed explanation of the mode

The role used

  1. abstract class

    Responsible for implementing template methods and abstract methods used by life in template methods. These abstract methods will be implemented by subclass concrete classes. The abstract class here is equivalent to AbstractDisplay in our sample program.

  2. concrete class

    Responsible for implementing the abstract method defined in the abstract class, the method implemented here will be called in the template method of the role of the abstract class, the concrete class here is equivalent to CharDisplay in our sample program.

When will we use the template method?

Suppose we don’t use the template method, but simply implement a lot of specific subclasses. If there is a problem immediately, the cost of modifying and finding the loophole will be very high. If we use the template method to design the pattern, we only need to check and modify The template method can solve the problem.

I was hoping to add a class diagram, but I see that it doesn't seem to support mermaid syntax, how should I implement it?

Top comments (0)