The strategy mode of understanding JS design pattern

Source: Internet
Author: User

Definition of the policy pattern: Define a series of algorithms and then invoke them according to the actual situation

An episode: Recently in the process of the project took over a more complex requirements, because it is the old project with a new framework refactoring, so you can see the previous code, draw the essence of the previous code, reuse some of the code available, so that they write a half-day. Of course, this topic is the strategy mode, will not digress, because when I finished version 1, the project inside a large number of If-else field validation is placed in a function, and some of the different fields will affect each other, causing the code is very long and chaotic about dozens of lines of if else, the slightest disorganized At this point, you need to refactor the original underlying validation function, then use the strategy mode

Give a scene of the strategy pattern: Verify that each field of a row of data is correct, the data field of each row is assumed to have 6, verify the correctness of the 6 fields, return the results

Then: Suppose that 6 fields are a B c d e F respectively;

If you do not use policy mode, the code is as follows:

function check (data) {   //data={a:, B:  , C:   ...}   if (data. A==???) {      }else{        }   if (data. B==???) {      }else{        }   if (data. C==???) {      }else{        } ...}

This is a bad way to get into the Strategy mode solution:

First, the strategy model means you have to have a strategy, what is a strategy? Is the solution that corresponds to each of these situations;

The method of each field inside the function is drawn out, and these methods of judging are the strategy!

What's another condition for the policy model? where you need to use the strategy to access your strategy , the performance of the extraction function is to call other functions

How do I organize my policies? Pulling away into one function and then calling to return the result, but this looks like the function is much more difficult to maintain, JS object literal provides a very elegant solution

var mystrategy={  a:function (value) {     //judgment     return result  },  b:function (value) {     //judgment     return result  },  c:function (value) {     //judgment     return result  },  d:function (value) {     / /Judgment     return result  },  e:function (value) {     //judgment     return result  },  f:function ( Value) {     //judgment     return result  }}

In this way, a policy object is established, you can create a new JS file, save the page needs of the various policies, and the underlying validation function, need to use the place to refer to the JS file that should be a better way

So how do you use policies?

function  Checke (data) {  var strategy=require (' Mystrategy '),//Get Policy object      result={};  for (var key in data) {  //assuming key=a then, the following execution is:    Result[key]=strategy[key] (Data[key]);   Result. A=strategy. A (data. A)  }  return result;

Super 6 has no, explain the above behavior

1, get the policy object

2, traverse the data object, get every key , namely abcdef these,

3, then go to the strategy to find the corresponding strategy method, execute to get the results returned

4, the result is assigned to the local result object, inside the Key,abcdef, save the corresponding field error message, return the result object

The strategy mode of understanding JS design pattern

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.