Focus on JavaScript's policy model

Source: Internet
Author: User
Tags what interface

? See a good article today to share with you:

Definition: Encapsulates a series of algorithms so that they can be replaced by each other, and this pattern uses an algorithm independent of the changes in the customer using it.

Description: The strategy mode, is a pattern of the organization algorithm, the core is not the algorithm, but the organization of a series of algorithms, and how to use it; the role of the strategy mode is to realize the unpredictable behavior, in the face of such a change, we have to think about how to use the program good dimension and extension, and make the customer very good use of

The strategy mode is to pay attention to its "change" side, the strategy mode is to solve the change problem.

For example, the price of shopping malls or promotional issues, if you do not use the model, it may just be "all" the situation with if else like "hard-coded" open write together, or pass a parameter, a little bit of internal logic code, preferably written together in a class inside;

Such as:

function Price (PersonType, price) {

Big customer 50 percent

if (PersonType = = ' VIP ') {

return price * 0.5;

}

else if (PersonType = = ' old ') {//Customer 30 percent

return price * 0.3;

} else {

return price; All the others are full fare

}

}

We look at the above as a class, if we want to expand a price method, we have to add a new else if, or modify an algorithm logic, you need to modify an if or else if, this is a single class modification, and this situation will often change this class, This violates a principle of the design pattern: the principle of closing the modification and opening the extension;

And these algorithms, in front of different customers may be often replaced, fixed parameters can not meet the demand;

To solve this problem can use the strategy model, the strategy is to pay attention to change, the implementation of the different behavior of each package, such as to achieve an animal, walking, shouting, eating a variety of performance, you can walk, call, eat, you can define three interface abstraction, and then the implementation of their respective implementation to achieve three interfaces ;

Previously said, the strategy mode is to make the algorithm can be independent of the use of customer changes, so the strategy mode, generally in the client-defined selection of the algorithm used, passed the corresponding parameters, and then return the results according to the algorithm;

This is a bit like a simple factory model, this additional layer of Context is for customers to use more simple call, do not have to self-processing some logic, let the team development, more consistent; the context can also be made into a base class, under which there can be multiple implementations of subclass inheritance;

Context and Client can be changed to:

Inheriting subclasses only need to reference their own implementation of the interface algorithm, so that customers can be more simple to call, and do not have to ignore the various kinds of animals use what interface, is how to achieve;

This is achieved by means of the above market price:

. Implementation of several algorithms, JavaScript is a weak type, no interface this number, is directly written;

VIP Customers

function Vipprice () {

This.discount = 0.5;

}

VipPrice.prototype.getPrice = function (price) {

return price * THIS.DISCOUNT;

}

Old Customers:

function Oldprice () {

This.discount = 0.3;

}

OldPrice.prototype.getPrice = function (price) {

return price * THIS.DISCOUNT;

}

General customers

function Price () {

This.discount = 1;

}

Price.prototype.getPrice = function (price) {

return price;

}

2. The context:

function Context () {

THIS.name = ";

This.strategy = null;

This.price = 0;

}

Context.prototype.set = function (name, strategy, price) {

THIS.name = name;

This.strategy = strategy;

This.price = Price;

}

Context.prototype.getResult = function () {

Console.log (this.name + ' checkout price: ' + this.stragegy.getPrice (this.price));

}

3. Client use;

Context

var context = new context ();

VIP Customers

var VIP = new Vipprice ();

Context.set (' VIP client ', VIP, 200);

Context.getprice ();

Old Customers

var old = new Oldprice ();

Context.set (' Old client ', OID, 200);

Context.getprice ();

General customers

//...

In this way, even if the client and the algorithm to decouple, and make the modification and expansion can be independent, not shadow to the client or other algorithm use;

Other Notes

Strategy mode is a very practical and commonly used design patterns, common for shopping malls prices, promotions and other scenes, he is like the factory model, it can be said that he is an upgrade version;

Strategy Mode related mode: Factory mode, combined mode;

?

Focus on JavaScript's policy model

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.