Revisit design pattern (vii)--template method pattern

Source: Internet
Author: User
Tags implement inheritance

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.