[design mode] JavaScript's strategy mode

Source: Internet
Author: User

Policy Mode description

Definition: Encapsulates a series of algorithms so that they are 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;

P such as:

 function   Price (PersonType, price) {  //  Big customer 50 percent  if  (PersonType = = ' VIP '  return     Price * 0.5;  else  if  (PersonType = = ' old ') { Span style= "color: #008000;"  >//  old customer 30 percent  return  Price * 0.3;  else   { return  Price; //  All other full price   

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 tables and, you can walk, call, eat, you can define three interface abstraction, and then the implementation of their respective specific 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;

Policy Mode structure diagram:

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;

Example Source

This is achieved by means of the above market price:

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

VIP Customers

function Vipprice () {    this. Discount= 0.5function(price) {return  This . Discount;}

Old Customers:

function Oldprice () {    this. Discount =0.3function(price) {     returnthis. discount;}

General customers

function Price () {    the. Discount =0.3function(price) {    return price ;}

2. The context:

functionContext () { 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 ( ThisThe checkout price for. Name + ' is: ' + This. Stragegy.getprice (Ths.price));}

3. Client use;

 //  context   context = new   context ();  // VIP customer  var  VIP = new   Vipprice () context.set ( ' VIP client ', VIP, 200 //  old customer  var  old = new   Oldprice (); Context.set ( old customer, older, 200 //  General client  //  ...  

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;

Keywords: algorithm, change, encapsulation, context;

[design mode] JavaScript's strategy mode

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.