Template Method pattern (gof-behavioral design pattern)

Source: Internet
Author: User

The template method mode seems easy to understand.

The main idea is to define an abstract class and put the method to be implemented in it, that is, the template. Then implement these methods in the specific subclass. The purpose is to increase the scalability of the class. Based on this template, different implementation methods may vary depending on the situation.

This mode is also easy to understand in practice, and the most typical is database connection and access.

To add applicationsProgramDevelopers will design their own programs to support as many database types as possible. However, different databases vary from connection methods to SQL queries. In this case, the template method is used.

Define an abstract data connection category class, list all attributes and methods as templates, and then write the corresponding implementation subclass for different databases. During the application, the corresponding subclass is called based on the database connection information configured by the system.

In object-oriented programs, inheritance is always essential, and the parent class is the template of the subclass. Therefore, this mode is everywhere.

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.