[Design pattern] javascript policy pattern, design pattern javascript
Definition: encapsulates a series of algorithms so that they can be replaced with each other. In this mode, algorithms are used independently of customers' changes.
Note: The policy mode is a model for organizing algorithms. The core is not about algorithms, but about organizing a series of algorithms and how to use them. The policy mode aims at organizing a series of algorithms, the realization of behavior is unpredictable. In the face of such a change, we have to think about how to use the program to maintain and expand, and make the customer well use the algorithm; when using the rule mode, pay attention to its "change" aspect. The rule mode solves this change problem. For example, if the model is not used, the "all" case may be written together with the if else, which is similar to the "hard-coded" type, or pass a parameter and add some internal logic Code. It is best to write it in a class together; P for example: function Price (personType, price) {// Key Account 0.5 off if (personType = 'vip ') {return price *;} else if (personType = 'old ') {// The old customer returns price * 0.3 for;} else {return price; // all others are priced}. We regard the above as a class. If we want to extend a price method, you have to add a new else if in the Price or modify an algorithm logic. You have to modify it in an if or else if statement. This is a modification to a single class, this kind of situation also requires frequent modification of this class, which violates a principle of design pattern: the principle of closing modification and opening extension; and these algorithms, it may be often replaced in front of different customers, and fixed parameters cannot meet the requirements. To solve this problem, you can use the policy mode. The strategy is to focus on changes and encapsulate different implementations of different behaviors, for example, to implement the performance of an animal, walking, calling, and eating, you can define three abstract interfaces for walking, calling, and eating, then implement the specific implementation of each of them to implement three interfaces. As mentioned above, the Policy mode enables the algorithm to be independent of the customer's changes in use. Therefore, in the policy mode, generally, the algorithm used is defined on the client, the corresponding parameters are input, and then the results are returned based on the algorithm. This is a bit similar to the simple factory mode, this type of additional layer Context is used to make it easier for the customer to call, without the need to process some logic, so that the team can develop more consistent; Context can also be made into a base class, there can be inheritance of multiple implementation sub-classes under it; the Context and Client can be changed to: Inherit sub-classes only need to reference their own implementation interface algorithms, so that the customer can make more simple calls, you do not have to worry about the interfaces used by various animals, but how to implement them. The instance source code is implemented by the above mall price means: 1. implement several algorithms. Javascript is of a weak type and is directly written without an interface. vip client function vipPrice () {this. discount = 0.5;} vipPrice. prototype. getPrice = function (price) {return price * this. discount;} old customer: function oldPrice () {this. discount = 0.3;} oldPrice. prototype. getPrice = function (price) {return price * this. discount;} function Price () {this. discount = 1;} Price. prototype. getPrice = function (price) {return price;} 2. 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. the billing price of name + 'is:' + this. stragegy. getPrice (this. price);} 3. used by the client; // context var Context = new context (); // vip Customer var vip = new vipPrice (); context. set ('vip customer', vip, 200); context. getPrice (); // old customer var old = new oldPrice (); context. set ('old client', old, 200); context. getPrice (); // common customer //... in this way, even if the customer and the algorithm are decoupled, modifications and extensions can be performed independently, without affecting the use of the client or other algorithms; other descriptive policy models are very practical and commonly used design models. They are generally used in shopping mall prices, promotions, and other scenarios. They are similar to the factory model and can be said to be an upgraded version of the model; rule mode related mode: factory mode and combination mode;