Deep understanding of the JavaScript series (41): The template method of design patterns detailed knowledge of the basics

Source: Internet
Author: User

Introduced

The template Method (Templatemethod) defines the skeleton of an algorithm in an operation and delays some steps into subclasses. Template methods enable subclasses to redefine certain steps of the algorithm without altering the structure of an algorithm.

Template methods are a basic technique for code reuse and are particularly important in class libraries because they extract public behavior from the class library. Template method leads to a reverse control structure, which is the legendary "Hollywood Law", that is, "Don't look for us, we're looking for you", which refers to the parent class calling a class operation, not the other way around. The embodiment is abstract classes (and abstract methods) in Object-oriented programming languages, and subclasses that inherit the abstract class (and abstract methods).

Body

For example, tea and coffee have the same steps, such as boiling water (boilwater), Brewing (brew), pouring into a cup (pouroncup), adding small material (addcondiments), and so on. However, the method of brewing each drink is different from the addition of the small material, so we can use the template method to achieve this major step.

First, you define the abstract steps:

Copy Code code as follows:

var caffeinebeverage = function () {

};
CaffeineBeverage.prototype.prepareRecipe = function () {
This.boilwater ();
This.brew ();
This.pouroncup ();
if (this.customerwantscondiments ()) {
If you want to add a small material, add
This.addcondiments ();
}
};
CaffeineBeverage.prototype.boilWater = function () {
Console.log ("Boil the water!");
};
CaffeineBeverage.prototype.pourOnCup = function () {
Console.log ("Drink into the cup again!");
};
CaffeineBeverage.prototype.brew = function () {
throw new Error ("This method must be overridden!");
};
CaffeineBeverage.prototype.addCondiments = function () {
throw new Error ("This method must be overridden!");
};
Default plus small material
CaffeineBeverage.prototype.customerWantsCondiments = function () {
return true;
};

The function extends all the basic steps on the prototype, as well as the main steps, the bubbling and dosing steps are not implemented, the functions corresponding to the specific beverage are implemented, and whether or not the addition of a small material (customerwantscondiments) returns True by default, which can be overridden when a child function is overridden.

The following two functions are the corresponding functions for coffee and tea:

Copy Code code as follows:

Brew Coffee
var Coffee = function () {
Caffeinebeverage.apply (this);
};
Coffee.prototype = new Caffeinebeverage ();
Coffee.prototype.brew = function () {
Console.log ("Pour the coffee into the coffee machine!");
};
Coffee.prototype.addCondiments = function () {
Console.log ("Add sugar and milk");
};
Coffee.prototype.customerWantsCondiments = function () {
return confirm ("Do you want to add sugar and milk?") ");
};

Red Tea
var tea = function () {
Caffeinebeverage.apply (this);
};
Tea.prototype = new Caffeinebeverage ();
Tea.prototype.brew = function () {
Console.log ("Bubble tea!");
};
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, you can allow users to choose the addition of small materials, very good, is not it?

Summarize

The template method applies to the following situations:

1. A one-time implementation of an invariant part of the algorithm, and the variable behavior left to the subclass to implement
2. The public behavior in each subclass should be extracted and centralized into a common parent class to avoid code duplication, separate the new operations, and, finally, replace these different codes with a template method for these new operations
3. Control subclass Extensions, template methods invoke "hook" operations only at specific points, allowing extensions at these points

Unlike policy patterns, template methods use inheritance to change part of an algorithm, and the policy pattern uses delegates to change the entire algorithm.

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.