Detailed explanation of template method of Android programming design mode

This paper describes the template method pattern of Android programming design pattern. Share with you for your reference, as follows:

1、 Introduction

In the process of object-oriented development, we usually encounter such a problem. We know the key steps required by an algorithm and determine the execution order of these steps. However, the specific implementation of some steps is unknown, or the implementation of some steps will change with the change of the environment. For example, the process of executing the program is roughly as follows:

1. Check the correctness of the code; 2. Link relevant class libraries; 3. Compile relevant codes; 4. Implementation procedures.

For different programming languages, the above four steps are different, but their execution process is fixed. The solution to this kind of problem is the template method pattern we want to talk about in this chapter.

2、 Definition

Define the framework of an algorithm in operation, and delay some steps to subclasses, so that subclasses can redefine some specific steps of an algorithm without changing the structure of an algorithm.

3、 Usage scenario

When multiple subclasses have public methods and the logic is basically the same.

For important and complex algorithms, the core algorithm can be designed as a template method, and the surrounding detailed functions are realized by each subclass.

During refactoring, the template method pattern is a frequently used pattern, which extracts the same code into the parent class, and then constrains its behavior through hook functions.

4、 UML class diagram of template method pattern

UML class diagram:

Role introduction:

AbstractClass: abstract class, which defines a set of algorithm framework.

Concreteclass1: concrete implementation class 1.

Concreteclass2: concrete implementation class 2.

5、 Simple example

Template method actually encapsulates a fixed process, just like a set of execution templates. What to do in the first step and what to do in the second step have been defined in the abstract class. Subclasses can be implemented by different algorithms. The algorithm replacement of some steps can be realized without modifying the framework. The following is a simple demonstration of the template method by opening the computer. The whole process of turning on the computer is relatively fixed. First, start the computer power. When the computer detects that there is no problem, it will enter the operating system. After verifying the user, you can log in to the computer. Let's use the template method to simulate this process:

Abstract computer

Windows system computer (no login required):

MAC (login required):

Call:

result:

6、 Template method pattern in Android source code

1、AsyncTask

When using asynctask, we all know to put time-consuming operations into doinbackground (params... Params). Before doinbackground, if you want to do some initialization operations, you can write the implementation in onpreexecute. After doinbackground is executed, the onpostexecute method will be executed, and we only need to build the asynctask object and then execute the execute method.

2. Activity lifecycle

After the main function of ActivityThread is invoked, the onCreate, onStart and onResume functions of Activity are executed sequentially. Users usually overwrite onCreate methods in the subclass of Activity, and invoke setContentView in this method to set up the layout.

7、 Distinction

The factory method is a special version of the template method.

Both policy pattern and template method pattern encapsulate algorithms, one with combination and the other with inheritance.

Policy mode and template mode can usually replace each other. They are all like test papers. The strategy mode is multiple-choice questions and the template mode is blank filling questions.

8、 Summary

The template method pattern is summarized in four words: process encapsulation. That is to encapsulate a fixed process into a final method, and allow subclasses to customize some or all steps in the process, which requires the parent class to extract common code, improve the reuse rate of code, and bring better scalability.

advantage:

Encapsulate the invariant part and expand the variable part.

Extract the common part code for easy maintenance.

Disadvantages:

It is necessary to provide a subclass for different implementations of each basic method. If there are too many variable basic methods in the parent class, the number of classes will increase, the system will be larger, and the design will be more abstract. At this time, it can be designed in combination with the bridge mode.

More readers interested in Android related content can view the special topics of this site: introduction and advanced tutorial of Android development, summary of Android debugging skills and common problem solving methods, summary of Android basic component usage, summary of Android view skills, summary of Android layout skills and summary of Android control usage

I hope this article will help you in Android programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>