1. Concept of template method pattern

1.1 introduction

In Template Pattern, an abstract class exposes a method/Template that executes its methods. Its subclasses can override method implementations as needed, but the calls will be made in the manner defined in the abstract class. This type of design pattern is behavioral.

1.2 define

Template method pattern: Defines the skeleton of an algorithm, deferring specific steps from the skeleton to subclasses. The template method pattern allows subclasses to redefine specific steps of an algorithm without changing its structure

1.3 Application Scenarios
  • The system needs to decouple the request caller and the request receiver so that the caller and receiver do not interact directly.
  • The system needs to specify requests, queue requests (e.g., thread pool + work queue), and execute requests at different times.
  • The system needs to support Undo and Redo operations.
  • The system needs to put together a set of actions that support macro commands.

2. Template method pattern UML class diagram

  • AbstractClass: Implements template methods and defines the skeleton of an algorithm.
  • ConcreteClass: Implements an abstract method in an abstract class, complete with algorithm.

3. Template method code implementation

AbstractClass:
public abstract class Game {
       abstract void initialize(a);
       abstract void startPlay(a);
       abstract void endPlay(a);

       public final void play(a){ initialize(); startPlay(); endPlay(); }}Copy the code
ConcreteClass:
public class KingGloryGame extends Game {
    @Override
    void initialize(a) {
        System.out.println("Initialize the King of Glory game.");
    }

    @Override
    void startPlay(a) {
        System.out.println("Honor of Kings begins.");
    }

    @Override
    void endPlay(a) {
        System.out.println("Honor of Kings is over."); }}Copy the code
public class LoLGame extends Game {
    @Override
    void initialize(a) {
        System.out.println("Initialize LOL game");
    }

    @Override
    void startPlay(a) {
        System.out.println("LOL game on.");
    }

    @Override
    void endPlay(a) {
        System.out.println("LOL game over."); }}Copy the code
Client:
public class Client {
    public static void main(String[] args){
        Game game = new LoLGame();
        game.play();
        game = newKingGloryGame(); game.play(); }}Copy the code

Result output:

Initialize LOL game LOL game start LOL Game end Initialize Glory of Kings Game Start Glory of Kings Game endCopy the code

4. Template method mode is used in Android

Life cycles of AsyncTask classes, onCreate() in activities, etcCopy the code

5. Model summary

5.1 the advantages
  • Encapsulate invariant parts and extend variable parts.
  • Extract common code for easy maintenance.
  • The behavior is controlled by the parent class and implemented by the child class.
5.2 disadvantages
  • Each different implementation requires a subclass to implement, resulting in an increase in the number of classes, making the system larger.