Template Method-template method mode

Source: Internet
Author: User

 The template method mode is also called the template method mode. It is one of the 23 design modes proposed by gof. The template method mode is one of the action modes.AlgorithmSome of the necessary processing methods in are granted to the abstract method, and the behavior of the entire algorithm is changed through the different implementations of the abstract method inherited by the subclass.

This article describes the concept and usage of the template method in the design mode, and how to use the template method mode for development in actual applications.
Concept of template method mode

The template method pattern, just like its name, defines a template method (public) with fixed algorithms and can be subdivided into multiple steps in the parent class of the abstract class ), the template method mode abstracts the variable steps that can be subdivided into abstract methods that can be overloaded by the quilt class (protected abstract), and reloads (redefinition) in the subclass ), you do not need to change the algorithm steps of the template method, but can redefine some specific steps in the algorithm.
[This figure is from Wikipedia wikipedia.org]:

The figure is explained based on the above definition.
-Abstractclass is equivalent to the parent class of the abstract class. concreteclass is the concrete implementation subclass. In a specific application, one or more implementation subclasses may exist.
-Abstractclass defines a public templatemethod () template method and the Method1 () and method2 () Methods used as steps.
-Abstractclass # templatemethod () method body call the Method1 () and method2 () Methods
Public void templatemethod (){
...
This. Method () 1;
...
This. Method () 2;
...
}
-The Method1 () and method2 () methods are protected Abstract METHODS (protected abstract ). This method must be reloaded to implement the concreteclass subclass.

Application scenarios of the template method mode

The template method mode is generally used in applications with the following conditions:
-Unified operation steps or procedures
-Different Operation Details
-There are multiple application scenarios with the same operation steps, but some specific operation details are different

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.