Deep understanding of JavaScript series (41): Template Method of Design Pattern

Source: Internet
Author: User
Introduction

The template method (templatemethod) definesAlgorithmAnd delay some steps to the subclass. The template method allows the subclass to redefine certain steps of an algorithm without changing the structure of an algorithm.

The template method isCodeThe basic reuse technology is particularly important in the class library because it extracts the public behavior in the class library. The template method leads to a reverse control structure, which is the legendary "Hollywood law", that is, "Don't look for us, let's look for you ", this means that the parent class calls a class operation, rather than the opposite. The specific embodiment is Object-Oriented ProgrammingProgramming LanguageAnd inherit the subclass of the abstract class (and abstract method.

Body

For example, tea and coffee take the same steps, such as boiling water, brew, pouroncup, and addcondiments. However, the brewing method for each type of beverage and the addition of small materials are different, so we can use the template method to achieve this main step.

First, define the abstract steps:

 VaR Caffeinebeverage = Function (){

};
Caffeinebeverage. Prototype. preparerecipe = Function (){
This . Boilwater ();
This . Brew ();
This . Pouroncup ();
If ( This . Customerwantscondiments ()){
// If you want to add small materials, add
This . Addcondiments ();
}
};
Caffeinebeverage. Prototype. boilwater = Function (){
Console. Log ("boil water! ");
};
Caffeinebeverage. Prototype. pouroncup = Function (){
Console. Log ("bring the drink to the cup again! ");
};
Caffeinebeverage. Prototype. Brew = Function (){
Throw New Error ("This method must be rewritten! ");
};
Caffeinebeverage. Prototype. addcondiments = Function (){
Throw New Error ("This method must be rewritten! ");
};
// Small items are added by default.
Caffeinebeverage. Prototype. customerwantscondiments = Function (){
Return True ;
};

This function expands all the basic steps and main steps in the prototype. The Brewing and addition steps are not implemented for the function corresponding to the specific beverage, and whether or not to add the customerwantscondiments) returns true by default. This value can be overwritten when the subfunction is rewritten.

The following two functions correspond to coffee and tea respectively:

 //  Coffee  
VaR Coffee =Function (){
Caffeinebeverage. Apply ( This );
};
Coffee. Prototype = New Caffeinebeverage ();
Coffee. Prototype. Brew = Function (){
Console. Log ("pour coffee in from coffee maker! ");
};
Coffee. Prototype. addcondiments = Function (){
Console. Log ("add sugar and milk ");
};
Coffee. Prototype. customerwantscondiments = Function (){
Return Confirm ("do you want to add sugar and milk? ");
};

// Chong tea
VaR Tea = Function (){
Caffeinebeverage. Apply ( This );
};
Tea. Prototype = New Caffeinebeverage ();
Tea. Prototype. Brew = Function (){
Console. Log ("Tea bubble! ");
};
Tea. Prototype. addcondiments = Function (){
Console. Log ("add lemon! ");
};
Tea. Prototype. customerwantscondiments = Function (){
Return Confirm ("do you want to add lemons? ");
};

In addition, the use of confirm allows users to choose not to add small materials, which is good, isn't it?

Summary

The template method is applicable to the following situations:

    1. Implement the unchanged part of an algorithm at one time, and leave the variable behavior to the subclass for implementation.
    2. Common behaviors in each subclass should be extracted and centralized into a common parent class to avoid code duplication. differences are separated into new operations. Finally, replace these different codes with the template method of A New phishing operation.
    3. Control subclass extension. The template method only calls the "Hook" operation at specific points, so that extension can be performed at these points.

Unlike the policy mode, the template method uses inheritance to change part of the algorithm, while the policy mode uses delegation to change the entire algorithm.

Reference: https://github.com/tcorral/Design-Patterns-in-Javascript/blob/master/Template/withHook/index.html

Transferred from: Uncle Tom

Related Article

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.