JavaScript: Template Method of Design Pattern

Source: Internet
Author: User

JavaScript: Template Method of Design Pattern
Introduction

The template method (TemplateMethod) defines the skeleton of an algorithm in an operation, and delays 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 is a basic technology for code reuse. It is especially important in class libraries because it extracts public behaviors from class libraries. 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. It is embodied in the abstract classes (and the abstract methods) in the object-oriented programming language, and inherit the subclasses of the abstract classes (and abstract methods.

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 !);}; // By default, the small item CaffeineBeverage. prototype. customerWantsCondiments = function () {return true;} is added ;};

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 (from coffee maker to coffee !);}; Coffee. prototype. addCondiments = function () {console. log (add sugar and milk) ;}; Coffee. prototype. customerWantsCondiments = function () {return confirm (do you want to add sugar and milk ?);}; // 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.

     

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.