define

Defining the framework of an algorithm in an operation and deferring some steps to subclasses allows subclasses to redefine some steps of the algorithm without changing the framework

nature

Fixed algorithm framework

His role

  • AbstractClass AbstractClass

    Define the framework of the algorithm, and you can also define the implementation of the common parts of the algorithm.

  • ConcreteClass (ConcreteClass)

    The concrete implementation class of the algorithm is used to implement some steps in the algorithm framework, namely abstract methods, to complete the whole function.

The sample code

Public abstract class AbstractDisplay {abstract void open(); abstract void print(); abstract void close(); public final void display(){ open(); for(int i=0; i<5; i++){ print(); } close(); 1 */ public class extends AbstractDisplay {private char aChar; CharDisplay(char c){ this.aChar = c; } @Override void open() { System.out.print("<<"); } @Override void print() { System.out.print(aChar); } @Override void close() { System.out.println(">>"); Public class AbstractDisplay extends AbstractDisplay{private String message; private int width; StringDisplay(String message){ this.message = message; this.width = message.length(); } @Override void open() { pringLine(); } @Override void print() { System.out.println("|" + message + "|"); } @Override void close() { pringLine(); } private void pringLine(){ System.out.print("+"); for(int i=0; i<width; i++){ System.out.print("-"); } System.out.println("+"); } } public class RunTemplate { public static void main(String[] args){ AbstractDisplay charDisplay = new CharDisplay('a'); AbstractDisplay stringDiaplay = new StringDisplay("message"); System.out.println(" This is the input of the first algorithm implementation: "); charDisplay.display(); System.out.println(" This is the input for the second algorithm implementation: "); stringDiaplay.display(); }}Copy the code

The results

This is the first kind of algorithm of input content: < < aaaaa > > this is the second algorithm input: + -- -- -- -- -- -- -- + | message | | message | | message | | message | | message | + -- -- -- -- -- -- -- +Copy the code

function

The function of the template approach is to fix the algorithm framework so that specific algorithm implementations can extend themselves as needed without deviating from the overall framework. That is, the extension of the subclass is satisfied by the steps of the fixed algorithm. The implementation of template methods mainly uses inheritance.

advantages

  • The advantage is that the code can be reused, that is, the algorithm framework abstract class can encapsulate not only the algorithm framework, but also the common code.

disadvantages

  • The disadvantage is that the algorithm framework is not easy to upgrade, because the implementation method is mostly inherited. If the algorithm framework is modified, all the subclass algorithm implementations involved may need to be changed accordingly. Therefore, when using the template method pattern, try to ensure that the parts that do not change are encapsulated in the algorithm framework abstract class.