Detailed explanation of the Rule mode of the JavaScript design mode, and the javascript Design Mode

Source: Internet
Author: User

Detailed explanation of the Rule mode of the JavaScript design mode, and the javascript Design Mode

In programming, we often encounter this situation. We have many algorithms to implement a function.

These algorithms are flexible and can be replaced at will. This solution is called a policy model.

Policy patterns are defined to define a series of algorithms, encapsulate them one by one, and make them replace each other.

/** Pre: Policy mode * example: The Company calculates the bonus, which is divided into three types of performance: A, B, and C. The calculation method is as follows * performance is A, and the bonus is multiplied by the coefficient 5 * performance is B, bonus multiplied by coefficient 4 * performance is C, bonus multiplied by coefficient 3 * // -------- Example 1 ---------- var calculateBonus = function (performanceLevel, salary) {if (performanceLevel = 'A') {return salary * 5;} if (performanceLevel = 'B') {return salary * 4 ;} if (performanceLevel === 'C') {return salary * 3 ;}}; console. log (calculateBonus ('A', 2000);/* disadvantages: 1. The function system is large and has too many if-else statements; 2. If performance D is added, internal functions need to be modified, which violates the closed-open principle. 3. Poor reusability. If calculation bonuses are used elsewhere, values can only be assigned and pasted; * // --------- Example 2 ------------ var region CEA = function (salary) {return salary * 5 ;}; var performanceB = function (salary) {return salary * 4 ;}; var export CEC = function (salary) {return salary * 3;}; var calculateBonus = function (performanceLevel, salary) {if (performanceLevel = 'A ') {return region CEA (salary);} if (Region celeve L === 'B') {return performanceB (salary);} if (performanceLevel === 'C') {return performanceC (salary) ;}; console. log (calculateBonus ('A', 2000);/* disadvantages: 1. The function system is huge, lack of flexibility when the system changes * // --------- Example 3 ------------ // policy mode refactoring: defines a series of algorithms and encapsulates them one by one. Var multiple CEA = function () {}; multiple CEA. prototype. caculate = function (salary) {return salary * 5 ;}; var performanceB = function () {}; performanceB. prototype. caculate = function (salary) {return salary * 4 ;}; var returns CEC = function () {}; returns Cec. prototype. caculate = function (salary) {return salary * 3 ;}; var Bonus = function () {this. salary = null; this. strategy = null;}; Bonus. prototype. setSalary = function (salary) {this. salary = salary;}; Bonus. prototype. setStrategy = function (strategy) {this. strategy = strategy;} Bonus. prototype. getBonus = function () {return this. strategy. caculate (this. salary);} var bonus = new Bonus (); bonus. setSalary (1, 2000); bonus. setStrategy (new performanceA (); console. log (bonus. getBonus (); // ----------- Example 4 --------------- // javaScript version var Strategies = {"A": function (salary) {return salary * 5;}, "B ": function (salary) {return salary * 4;}, "C": function (salary) {return salary * 3 ;}}; var caculateBonus = function (level, salary) {return Strategies [level] (salary) ;}; console. log (caculateBonus ("A", 2000 ));

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.