Template Method Template Design Pattern

What is Template Design Pattern

For those who don't know the template design mode, it can be considered as the ancient papermaking technique, the shape of the paper depends on the shape of the template used, and the shape is composed of hollow wooden boards, and what kind of paper you want to make depends on Depends on what material you use. Two key points are mentioned above < <template> > < <material> >

definition:

As we define the framework of the processing flow in the parent class, and the template that implements specific processing in the subclass is called the template method template design pattern

object-oriented

  • < <template> > in the object we describe as abstract class: define procedure pdl
  • < <material> > in the object we describe as a subclass of concrete behavior:

UML code

  • AbstractDisplay: Defines the template framework, as well as the specific process, defined here as printing 5 characters
  • CharDisplay: How to print the specific implementation of the subclass.
  • StringDisplay: same as above

    It should be explained here that the subclass should be developed collaboratively with the parent class, because the specific actions of the subclass cannot be written arbitrarily. The corresponding processing process should be defined according to the specific processing flow of the parent class. Of course, the document can be clearly written.

  • AbstractDisplay

package base.template;

/**
 * @program: DesignPatterns
 * @description: 只实现display方法抽象类
 * @author: Mr.Dai
 * @create: 2018-04-28 16:12
 **/
public abstract class AbstractDisplay {

      public abstract void open();
      public abstract void print();
      public abstract void close();

    /**
     * 实现打印5次的方法
     */
    public void  display() {
        open();
        for (int i = 0; i < 5; i++) {
            print();
        }
        close();
    }
}
  • CharDisplay
package base.template;

/**
 * @program: DesignPatterns
 * @description: 实现单个字符的打印
 * @author: Mr.Dai
 * @create: 2018-04-28 16:15
 **/
public class CharDisplay extends AbstractDisplay {

    private Character ch;

    public CharDisplay(char ch) {
        this.ch=ch;
    }

    @Override
    public void open() {
        System.out.print("<<<");
    }

    @Override
    public void print() {
        System.out.print(ch);
    }

    @Override
    public void close() {
        System.out.println(">>>");
    }

    @Override
    public void display() {
        super.display();
    }
}
  • StringDisplay
package base.template;

/**
 * @program: DesignPatterns
 * @description: 实现对个字符串打印
 * @author: Mr.Dai
 * @create: 2018-04-28 16:18
 **/
public class StringDisplay extends AbstractDisplay {
    private String string;
    private int width;

    public StringDisplay(String string) {
        this.string = string;
        this.width = string.getBytes().length;
    }

    @Override
    public void open() {
        printline();
    }

    private void printline() {
        System.out.print("+");
        for (int i = 0; i < width; i++) {
            System.out.print("-");
        }
        System.out.println("+");
    }
    @Override
    public void print() {
        System.out.println("|"+string+"|");
    }

    @Override
    public void close() {
        printline();
    }

    @Override
    public void display() {
        super.display();
    }
}

transfer boost

  • The method of using templates in java.io.inputstream, as shown in UML as shown below

  • Limited to the concept of polymorphism, the specific instances of chardisplay stringdisplay are all in the upward pointing abstractdisplay. There is no need to specify a specific subclass through instanceof
  • Because it is implemented through inheritance, it follows the LSP
  • In the process of cooperation between the subclass and the parent class, it is necessary to ensure that the amount of code and the number of methods are arranged reasonably. More method implementations in the parent class will make the subclass easier, but it also reduces the flexibility of the subclass. If the parent class There are too few methods implemented in the class, and it also causes code duplication between subclasses.
  • The reason for using abstract classes instead of interfaces in abstractdisplay is to determine the corresponding algorithm process when defining the template process.

Related Design Patterns

  • Factory Method: The factory pattern is a typical way to use the template method pattern to generate instances
  • Strategy: The strategy mode can use the delegate to change the behavior of the program. Unlike the template method mode, which changes part of the behavior, it can be used as a replacement for the entire algorithm.

Responsibilities to be mastered

  • subclass angle
  • Methods defined in parent class can be used in subclasses
  • You can add methods in subclasses to implement new functions
  • A subclass overrides the parent class's method to change the parent class's behavior. Generally, it is determined whether to use final when defining the parent class template.
  • parent angle
  • Expect subclasses to implement abstract methods.
  • Require subclasses to implement abstract methods.

Guess you like

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