1. Introduction

Hand over the handling to subclasses

  • Definition: Defines the skeleton of an algorithm and allows subclasses to provide implementation template methods for one or more steps that allow subclasses to redefine certain steps of the algorithm without changing the structure of the algorithm

  • Type: behavioral

  • Application scenario: Implement the invariable part of an algorithm at one time, and leave the mutable behavior to the subclasses to realize the common behavior in each subclass is extracted and concentrated in a common parent class, so as to avoid code duplication

  • Advantages: improve reusability improve expansibility in line with the open and closed principle

  • Disadvantages: Increasing the number of classes increases the complexity of the system implementation

Case scenario

  • Print the string and character loop five times

List of classes and interfaces

The name instructions
AbstractDisplay An abstract class that implements only the display method
CharDisplay Classes that implement open,print,close methods
StringDisplay Classes that implement open,print,close methods
Main Classes that test program behavior

AbstractDisplay class

Define abstract methods: open, print, and close are implemented by subclasses, and display methods define templates for methods

public abstract class AbstractDisplay {
    public abstract void open(a);

    public abstract void close(a);

    public abstract void print(a);

    public final void display(a) {
        open();
        for (int i = 0; i < 5; i++) { print(); } close(); }}Copy the code

CharDisplay class

Implement AbstractDisplay class abstract methods, custom method behavior

public class CharDisplay extends AbstractDisplay {

    private char c;

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

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

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

    @Override
    public void print(a) { System.out.print(c); }}Copy the code

StringDisplay class

Implement AbstractDisplay class abstract methods, custom method behavior

public class StringDisplay extends AbstractDisplay {

    private String str;

    public StringDisplay(String str) {
        this.str = str;
    }

    @Override
    public void open(a) {
        printLint();
    }

    @Override
    public void close(a) {
        printLint();
    }

    @Override
    public void print(a) {
        System.out.println("|" + str + "|");
    }

    private void printLint(a) {
        System.out.print("+");
        for (int i = 0; i < str.length(); i++) {
            System.out.print("-");
        }
        System.out.println("+"); }}Copy the code

The main class

public class Main {

    public static void main(String[] args) {
        AbstractDisplay ad1 = new CharDisplay('a');
        AbstractDisplay ad2 = new StringDisplay("hello world"); ad1.display(); ad2.display(); }}Copy the code
  • Execution Result:
<<aaaaa>>
+-----------+
|hello world|
|hello world|
|hello world|
|hello world|
|hello world|
+-----------+	
Copy the code