JavaScript Template Method pattern

Source: Internet
Author: User
Tags abstract define object definition define abstract final inheritance variable

Template Method Mode description

Definition: Defines the skeleton of the method operation, extends some concrete implementations into subclasses, and uses specific implementations that do not affect the skeleton's behavioral steps!

Description: A pattern-method pattern is a typical pattern of inheritance and reuse, which defines an abstract class, which is defined by the form class, that defines one or more template methods, the top-level methods that define the scheduling step of the behavior, typically invoking other specific classes or hooks defined in the abstract Defines some abstract methods that must be implemented by a specific subclass, and it is possible to define some hook methods, which are generally empty methods for child class extensions.

Template Method Pattern Composition:

1>. Template method: Also called skeleton method, define behavior scheduling steps! Can not be modified by the quilt class, if in the Java Advanced object-oriented language, the general will be decorated with final!

2>. Abstract method: The method that must be implemented for subclass inheritance;

3>. The specific method: for the top-level implementation in the abstract class, generally can not be overridden to modify, generally will add final modification!

4>. Hook method: Generally null method. Method naming generally starts with "do", similar to the doget under HttpServlet, DoPost;

Template method patterns, generally used with the same behavior logic process, but their implementation is not the same scenario, this situation can be used to define the skeleton, how to implement, order, scheduling, and so on, let the subclass of their own specific behavior;

Object-oriented thinking, pay attention to its constant and change factors, in the template method pattern, the industry logic is invariable, the behavior concrete realization is variable, separates the variable with the invariable to let the solution lotus root, this conforms to the object-oriented opening and closing principle; the definition abstract skeleton, lets the concrete realization in the subclass form to expand freely, does not affect mutually.

Template pattern Structure diagram:

Instance scenario:

1>. For example, different documents in the process of approval, through the people or departments are the same, but each process his treatment is inconsistent;

2>. For example, the university four years, the different major year of the textbook is not the same;

Instance source code

Here to the university for four years to achieve;

1. Define abstract classes;

function AbstractClass () { 
}

AbstractClass.prototype.template = function () {this
.    Firstyear ();
This.    Secondyear ();
This.    Thirdyear ();
This. Fourthyear ();
}

AbstractClass.prototype.FirstYear = function () {
console.log ("Need to override");
}

AbstractClass.prototype.SecondYear = function () {
console.log ("Need to override");
}

AbstractClass.prototype.ThirdYear = function () {
console.log ("Need to override");
}

AbstractClass.prototype.FourthYear = function () {
console.log ("Need to override");
}

2. Define a student;

function Student (name) {
this.name = name;
}

Student.prototype = Abstractclass.prototype; Inheritance

Student.prototype.FirstYear = function () {
console.log (this.name+ first year: language);
}

Student.prototype.SecondYear = function () {
Console.log (this.name+ "second year: math");
}

Student.prototype.ThirdYear = function () {
Console.log (this.name+ "third year: English");
}

Student.prototype.FourthYear = function () {
Console.log (this.name+ "IV: History");
}

3. How to use:

var student = new Student ("Chen So-and-so"); Student.template ();

Output:

Chen So-and-so first year of repair: Chinese

Chen So-and-so second year of repair: Mathematics

Chen So-and-so third year repair: English

Chen So-and-so four-year repair: History

Other Notes

In object-oriented inheritance, the behavior method is usually placed at the top level, the State member is placed in the subclass, and the behavior is concerned, not the state;

The template method pattern is a frequently used pattern, it embodies the characteristics of object-oriented classic inheritance and reuse, which is used to focus on fixed logical behavior, skeleton method is used to encapsulate fixed logic process, and to extract this part of unified process into abstract class when developing, and can not be influenced by the division of abstraction and concrete designer.



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.