introduce
The template method defines the skeleton of an operational algorithm and delays some steps to a subclass. The template method allows the subclass to redefine certain specific steps of the algorithm without changing the structure of the algorithm.
Template methods are a basic technique for code reuse, which is particularly important in class libraries because they extract public behavior in class libraries. The template method leads to a reverse control structure, which is the legendary "Hollywood law", that is, "Don't look for us, we'll look for you", which refers to the parent class calling the operation of a class, not the other way around. The concrete manifestations are abstract classes (and abstract methods in them) in object-oriented programming languages, as well as subclasses that inherit the abstract class (and abstract methods).
text
For example, making tea and coffee have the same steps, such as boiling water, brewing, pouring it into a cup, adding small ingredients, etc. However, the method of brewing each beverage and the added small ingredients are different, so we can use the template method to achieve this main step.
First, define the abstract steps:
The code copy is as follows:
var CaffeineBeverage = function () {
};
CaffeineBeverage.prototype.prepareRecipe = function () {
this.boilWater();
this.brew();
this.pourOnCup();
if (this.customerWantsCondiments()) {
// If you can add small ingredients, add
this.addCondiments();
}
};
CaffeineBeverage.prototype.boilWater = function () {
console.log("Bring the water to a boil!");
};
CaffeineBeverage.prototype.pourOnCup = function () {
console.log("Put the drink into the cup!");
};
CaffeineBeverage.prototype.brew = function () {
throw new Error("This method must be rewritten!");
};
CaffeineBeverage.prototype.addCondiments = function () {
throw new Error("This method must be rewritten!");
};
// Add small materials by default
CaffeineBeverage.prototype.customerWantsCondiments = function () {
return true;
};
This function extends all basic steps and main steps on the prototype. The brewing and adding small ingredients are not implemented, and is used by the function corresponding to the specific beverage to implement. In addition, whether to add small ingredients (customerWantsCondiments) returns true by default. This value can be rewrite when rewriting the sub-function.
The following two functions are the corresponding functions for brewing coffee and brewing tea:
The code copy is as follows:
// Make coffee
var Coffee = function () {
CaffeineBeverage.apply(this);
};
Coffee.prototype = new CaffeineBeverage();
Coffee.prototype.brew = function () {
console.log("I want to pour coffee from the 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?");
};
//Make tea leaves
var Tea = function () {
CaffeineBeverage.apply(this);
};
Tea.prototype = new CaffeineBeverage();
Tea.prototype.brew = function () {
console.log("Puuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
};
Tea.prototype.addCondiments = function () {
console.log("Add lemon!");
};
Tea.prototype.customerWantsCondiments = function () {
return confirm("Do you want to add lemon?");
};
In addition, using confirm allows users to choose whether to add small ingredients by themselves, which is very good, isn’t it?
Summarize
The template method is applied to the following situations:
1. Implement the unchanging part of an algorithm at one time and leave the mutable behavior to subclasses to implement it
2. The public behavior in each subclass should be extracted and concentrated in a public parent class to avoid duplication of code, and the differences are separated into new operations. Finally, replace these different codes with a template method for phishing these new operations.
3. Control subclass extensions. The template method only calls the "hook" operation at specific points, so that it allows extensions at these points.
Unlike policy patterns, template methods use inheritance to change part of the algorithm, while policy patterns use delegates to change the entire algorithm.