Introduction to template method of Java design pattern

The template method pattern of Java design pattern defines the framework of an algorithm in operation, and delays some steps to subclasses, so that subclasses can redefine some specific steps in the algorithm without changing the structure of the algorithm. It belongs to behavior mode

As shown in the figure below:

In fact, the template method is a frequently used pattern in programming. Let's take a look at an example. One day, programmer a got a task: given an integer array, sort the numbers in the array from small to large, and then print the sorted results. After analysis, this task can be divided into two parts: sorting and printing. The printing function is easy to realize, and sorting is a little troublesome. But a has a way. First, complete the printing function and find someone else to do the sorting function.

After writing, a found a colleague who had just graduated and entered the post. B said: there is a task. I have written the main logic. You can implement the rest of the logic. So give the AbstractSort class to B and let B write the implementation. B take it and have a look. It's too simple. It'll be done in 10 minutes. The code is as follows:

Write it and give it to a. a brings it for operation:

Run result: sort result: 0 1 3 4 5 7 9 10 12 32

Normal operation. All right, the task is complete. Yes, this is the template method pattern. Most graduates who have just entered the workplace should have experience similar to B. For a complex task, the cattle in the company write the main logic, then write the seemingly simple methods into abstract ones and hand them over to other colleagues for development. This division of labor is often used in companies where the level of programmers is obvious. For example, in a project team, there are architects, senior engineers and junior engineers. Generally, architects use a large number of interfaces and abstract classes to string the logic of the whole system, and the implemented coding is handed over to senior engineers and junior engineers respectively according to different difficulties. How about the template method mode? Structure of template method pattern:

The template method pattern is composed of an abstract class and an (or a group) implementation class through inheritance structure. There are three methods in the abstract class:

1. Abstract method: the parent class only declares but does not implement it. Instead, the specification is defined and then implemented by its subclasses.

2. Template method: it is declared and implemented by abstract classes. Generally speaking, template methods call abstract methods to complete the main logical functions, and most template methods are defined as final type, indicating that the main logical functions cannot be overridden in subclasses.

3. Hook method: declared and implemented by abstract class. However, subclasses can be extended, and subclasses can affect the logic of template methods by extending hook methods.

The task of abstract class is to build a logical framework, which is usually written by experienced personnel, because the quality of abstract class directly determines the stability of the program.

Implementation classes are used to implement details. The template method in the abstract class completes the business logic by implementing the method of class extension. As long as the extension method in the implementation class passes the unit test, on the premise that the template method is correct, the overall function generally will not have major errors. Advantages and applicable scenarios of template method:

Easy to expand. Generally speaking, the template method in the abstract class is the part that is not easy to generate and change, and the abstract method is the part that is easy to generate and change. Therefore, it is generally easy to expand the function by adding the implementation class, which conforms to the opening and closing principle.

Easy to maintain. For the template method mode, it is precisely because their main logic is the same that the template method is used. If the template method is not used, it is very inconvenient to allow these same codes to be scattered in different classes.

Quite flexible. Because there are hook methods, the implementation of subclasses can also affect the operation of the main logic in the parent class. However, while being flexible, the subclass affects the parent class, violates the Richter substitution principle, and will also bring risks to the program. This has higher requirements for the design of abstract classes.

When multiple subclasses have the same methods and the logic of these methods is the same, you can consider using the template method pattern. When the main framework of the program is the same and the details are different, it is also more suitable to use this mode.

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
分享
二维码
< <上一篇
下一篇>>