This is the 15th day of my participation in Gwen Challenge

Template method pattern

In the process of object-oriented development, when the execution process is fixed, each key step is known, and the execution sequence is consistent, but the specific realization process of some steps is unknown. In this case, the template-style pattern can be used.

Template method pattern definition: Defines an operational case framework that implements some steps in subclasses, redefining specific step implementations without changing the algorithm framework structure or process order.

In actual combat

A template method encapsulates a fixed process, a set of execution templates that define what needs to be done in step 1 and what needs to be done in step 2. These are all designed in advance in abstract classes, and each step has something that should be done. The subclass implementation processes and implements different things in each step according to its own execution. For example, in the following code, we define the abstract ComputerMainProcess class that designs several fixed methods during the computer boot process. PowerOpen Check the power supply -> checkCPU check the CPU status -> loadOS load the computer system -> checkUser check whether the user is valid -> login The subclass implementation only needs to realize the above methods to do what should be done in its own algorithm, while the bootUp method in the basic abstract class is final, which is the core point of the template method is fixed execution steps, and the logical process is stable and unchangeable.


public abstract class ComputerMainProcess{

    protected void powerOpen(a){}protected void loadOS(a){}protected void checkCPU(a){}protected void checkUser(a){}protected void login(a){}public final void bootUp(a){ powerOpen(); checkCPU(); loadOS(); checkUser(); login(); }}public class DellComputer extends ComputerMainProcess{
     @override
     protected void powerOpen(a){}@override
     protected void loadOS(a){}...@override
     protected void login(a){}}public class AsusComputer extends ComputerMainProcess{
     @override
     protected void powerOpen(a){... }@override
     protected void loadOS(a){... }...@override
     protected void login(a){... } } DellComputer dellComputer =new DellComputer();
AsusComputer asusComputer = new AsusComputer();

dellComputer.bootUp();
asusComputer.bootUp();
Copy the code

The Android template method AsyncTask

AsyncTask is a typical template method pattern in Android development. As a parent class, AsyncTask defines the asynchronous execution of the whole process method. When subclasses are used, you only need to do what you need to do in each step of an asynchronous operation, such as preparing for an asynchronous operation in onPreExecute; OnProgressUpdate gets the progress value during asynchronous execution. OnPostExecute returns the result of the asynchronous operation. Of course, the step method does not mean that all of them need to be realized. Specifically, the step algorithm needs to be realized according to the business needs.

class UseAsynctask extends AsyncTask{

  // Perform the operation before the thread task
  @Override
  protected void onPreExecute(a) {
      super.onPreExecute();
  }
  // Receives input parameters, time-consuming operations in the execution of the task, and returns the result of the execution of the thread task
  @Override
  protected Object doInBackground(Object[] params) {
      return null;
  }

  // Displays the progress of thread tasks in the main thread
  @Override
  protected void onProgressUpdate(Integer... progresses) {... }// Receives thread task execution results and displays them to UI components
  @Override
  protected void onPostExecute(Object o) {
      super.onPostExecute(o);
  }
}

UseAsynctask mTask = new UseAsynctask();
mTask.execute();

Copy the code

conclusion

The core of the template method is process encapsulation, which encapsulates the fixed process into the final function and implements the steps and methods by subclasses. Therefore, common methods of designing steps for parent classes are needed to improve code reuse and better extensibility.

Advantages of the template method pattern: encapsulate the immutable and extend the mutable. Extract common parts of code for easy maintenance.