Learning JavaScript Design Patterns Template method pattern _javascript techniques

Source: Internet
Author: User

I. Definition

Template method is based on the design pattern of inheritance, which can improve the scalability of the system. Abstract parent class, subclass in Java
The template method has two parts structure, the first part is abstract parent class, the second part is the concrete implementation subclass.

Second, the example

Coffee or tea
(1) Boil the water
(2) soaking tea with boiling water
(3) Pour the tea into the cup
(4) Add lemon

/* Abstract Parent: beverage/var beverage = function () {};//(1) Boil the water Beverage.prototype.boilWater = Func
tion () {console.log ("boil water");
(2) boiling water immersion Beverage.prototype.brew = function () {throw new Error ("Subclass must override Brew Method");
(3) pour into cup Beverage.prototype.pourInCup = function () {throw new Error ("Subclass must override Pourincup method");

(4) Add seasoning Beverage.prototype.addCondiments = function () {throw new Error ("Subclass must override Addcondiments method");
  /* Template Method */Beverage.prototype.init = function () {this.boilwater ();
  This.brew ();
  This.pourincup ();
This.addcondiments ();
/* Implement subclass coffee*/var Coffee = function () {};
Coffee.prototype = new beverage ();
Overrides Non-public Method Coffee.prototype.brew = function () {Console.log ("Brew coffee in boiling water");
Coffee.prototype.pourInCup = function () {Console.log ("Pour coffee into cup");
Coffee.prototype.addCondiments = function () {console.log ("Add milk");
var coffee = new coffee ();

Coffee.init (); 

The algorithm framework of subclasses is encapsulated in the parent class through the template method pattern. These algorithm frameworks are suitable for most subclasses in normal state, but also appear as "personality" subclasses.
As the above process, add seasoning is optional.
Hook methods can solve this problem, and placing hooks is a common means of isolating changes.

/* Add Hook Method
/* Beverage.prototype.customerWantsCondiments = function () {return
  true;
};
Beverage.prototype.init = function () {
  this.boilwater ();
  This.brew ();
  This.pourincup ();
  if (this.customerwantscondiments ()) {
    this.addcondiments ();
  }
}

/* Implement subclass tea*/
var tea = function () {};
Tea.prototype = new beverage ();
Overrides Non-public method
Tea.prototype.brew = function () {
  console.log ("Brew tea with boiling water");
Tea.prototype.pourInCup = function () {
  console.log ("pour tea into cup");
Tea.prototype.addCondiments = function () {
  console.log ("Add milk");
Tea.prototype.customerWantsCondiments = function () {return
  window.confirm () need to add seasoning? ");
};
var tea = new Tea ();
Tea.init ();

JavaScript does not provide true class inheritance, which is implemented through a delegate between objects and objects.

Third, "Hollywood Principles": Do Not call us, we will call you

Typical usage scenarios:

(1) Template method pattern: Using this design pattern means that subclasses give up control over themselves instead of the parent class notification subclass. As a subclass, it is only responsible for providing some design details.
(2) Observer mode: the publisher pushes the message to the Subscriber.
(3) callback function:Ajax asynchronous request, the operation needs to be carried out in the callback function, when the data returned, the callback function is executed.

I hope this article will help you learn about JavaScript programming.

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.