Design patterns are handed over to subclasses TemplateMethod module patterns for specific processing to subclasses.

Posted by crimsonmoon on Fri, 23 Aug 2019 11:00:10 +0200

Preface

Blogger github

Blogger's personal blog http://blog.healerjean.com

1. Interpretation

Define the framework of the processing process in the parent class and implement the specific processing pattern in the subclass. This pattern is often encountered. There are too many projects.

That is to say, some methods in a parent class have different implementations and need to be processed by several subclasses.

2. Business scenarios

Look at the content in the following 2. That is to say, there are overlaps among multiple businesses, where overlaps are handed over to parent classes, inheritance of subclasses, or rewriting.

1. Sample Program

1.1. UML diagrams

[Img-YHrEmOze-1566550484262 (https://raw.githubusercontent.com/HealerJean/HealerJean.github.io/master/blogImages/1558691657273.png)]

1.2. Sample code

1.2.1, AbstractDisplay, the abstract parent class
public abstract class AbstractDisplay { // AbstractDisplay
    public abstract void open();        // An abstract method to be implemented by subclasses (1) open
    public abstract void print();       // An abstract method to be implemented by subclasses (2) print
    public abstract void close();       // An abstract method to be implemented by subclasses (3) close
    public final void display() {       // Displaying method implemented in this abstract class
        open();                         // First, open ____________.
        for (int i = 0; i < 5; i++) {   // Loop call five print times
            print();                    
        }
        close();                        // ... Finally close. This is what the display method does.
    }
}
1.2.2, subclass CharDisplay
package com.hlj.moudle.design.D03_Delivery to subclasses.D03_TeampleMethod Pattern.TemplateMethod.Sample;

public class CharDisplay extends AbstractDisplay {  // CharDisplay is a subclass of AbstractDisplay
    private char ch;                                // Characters to be displayed

    public CharDisplay(char ch) {                   // The character received in the constructor is
        this.ch = ch;                               // Save it in a field
    }

    public void open() {                            // In the parent class is an abstract method, which is rewritten here
        System.out.print("<<");                     // Display start character "<"
    }

    public void print() {                           // Similarly, override the print method. This method is called repeatedly in display
        System.out.print(ch);                       // Display characters saved in field ch
    }

    public void close() {                           // Likewise, override the close method
        System.out.println(">>");                   // Display the End Character ">"
    }
}

1.2.3, Subclass StringDisplay
package com.hlj.moudle.design.D03_Delivery to subclasses.D03_TeampleMethod Pattern.TemplateMethod.Sample;

public class StringDisplay extends AbstractDisplay {    // StringDisplay is also a subclass of AbstractDisplay
    private String string;                              // Strings to be displayed
    private int width;                                  // String length calculated in bytes

    public StringDisplay(String string) {               // The string received in the constructor is
        this.string = string;                           // Save it in a field
        this.width = string.getBytes().length;          // At the same time, the byte length of the string is saved in the field for later use. 
    }

    public void open() {                                // Rewritten open method
        printLine();                                    // Call the printLine method of this class to draw a line
    }

    public void print() {                               // print method
        System.out.println("|" + string + "|");         // Add "|" before and after the string saved in the field and display it. 
    }

    public void close() {                               // close method
        printLine();                                    // Like the open method, call the printLine method to draw lines
    }

    private void printLine() {                          // Called by open and close methods. Because visibility is private, it can only be called in this class
        System.out.print("+");                          // Displays "+" for the corner of the box
        for (int i = 0; i < width; i++) {               // Display width "-"
            System.out.print("-");                      // Frames that make up boxes
        }
        System.out.println("+");                        // / Displays "+" for the corner of the box
    }
}

1.2.4, Test Main
public class Main {
    public static void main(String[] args) {
        AbstractDisplay d1 = new CharDisplay('H');                  // Generate an instance of the CharDisplay class holding'H' 
        AbstractDisplay d2 = new StringDisplay("Hello, world.");    // Generate an instance of the StringDisplay class that holds "Hello, world." 
        AbstractDisplay d3 = new StringDisplay("Hello, World.");     // Generate a hold "Hello World". Instances of the StringDisplay class 
        d1.display();                                               // Because d1, d2 and d3 are subclasses of the AbstractDisplay class
        d2.display();                                               // Inheritant display methods can be invoked
        d3.display();                                               // The actual program behavior depends on the implementation of the CharDisplay class and StringDisplay class.

    }
}

2. The role in Template Method

2.1. AbstractClass (abstract class parent)

1. Expect and require subclasses to implement abstract methods

2.2. ConcreteClass (concrete class, subclass)

1. The method defined in the parent class can be used in the subclass
2. Adding methods to subclasses to implement new functions
3. The method of restoring the parent class in the subclass can change the behavior of the program.

### 3. UML Diagram

[Img-CHyYl3Wl-1566550484263 (https://raw.githubusercontent.com/HealerJean/HealerJean.github.io/master/blogImages/1558693456672.png)]



Interested, welcome to add blogger Wechat

Ha, the blogger is very happy to communicate with friends from all walks of life. If you are satisfied, please reward the amount of money the blogger intends to pay. Interested in Wechat transfer, please note your Wechat or other contact information. Add the blogger Weixin.

Please leave a message below. Discuss freely with bloggers

WeChat Wechat Public Number Alipay

Topics: github