1. The SUMMARY
In this article, we have a comprehensive understanding of the template method pattern.
2. Job Search Newsletter
Oh, if you don't mind, I hope to do a job advertisement here.
Language direction: JAVA/C#/VB, language familiarity in descending order.
Job Orientation: development, testing and other IT related work.
Monthly salary Requirements: 1000 Yuan or more
Work Location: Beijing
3. Straight to the point
Template method, which defines an algorithm skeleton in an operation, and delays some steps into subclasses to implement.
Let's take a look at the schematic diagram of the template method:
The pattern itself is easy to understand and I don't draw it myself.
The key to seeing this pattern is the Templatemethod () method in the parent class, which provides the skeleton of our algorithm.
Having discussed design patterns with friends, we agree that the template approach is the most important design pattern in the GOF23 design pattern, but we think that this pattern, we do not need to treat him as a design pattern, even as a very ordinary inheritance rules.
4. The purpose of the template method
The purpose of the template method, I think there are two points:
A. Reduce duplication of code.
B. Prevent the call from making an error.
In that case, let's boil down to the steps of using the template method:
A. Identification
B. Separation
C. Implementation
That is, we first have to find out from a series of algorithms, where their common parts are called recognition. Then we extract the different code and call it separation. Finally, we use inheritance to implement the extracted method in subclasses.