preface

There are dozens of design patterns. After reading the book “JavaScript Design Patterns and Development Practices”, I feel that JS is designed around objects, and I find that daily writing code can be used for not much. Here are several commonly used design patterns.

Design patterns

The strategy pattern

Policy mode code is very elegant, one of my favorite patterns, and easy to modify, see the code.

Common mode:

  var awardS = function (salary) {
    return salary * 4
  };

  var awardA = function (salary) {
    return salary * 3
  };

  var awardB = function (salary) {
    return salary * 2
  };

  var calculateBonus = function (level.salary) {
    if (level = = = 'S') {
      return awardS(salary);
    }
    if (level = = = 'A') {
      return awardA(salary);
    }
    if (level = = = 'B') {
      return awardB(salary); }};calculateBonus('A'.10000);
Copy the code

Strategy mode:

  var strategies = {
    'S': function (salary) {
      return salary * 4;
    },
    'A': function (salary) {
      return salary * 3;
    },
    'B': function (salary) {
      return salary * 2; }}var calculateBonus = function (level.salary) {
    return strategies[level](salary);
  }

  calculateBonus('A'.10000);
Copy the code

Chain of Responsibility model

Yes, it was nested like this when I started my first project, with too much repetitive code, too much logic, and poor maintenance.

  var order = function (orderType.pay.stock) {
    //500 yuan deposit mode
    if (orderType = = = 1) {
      if (pay = = = true) {
        console.log('500 yuan deposit, 100 yuan coupon');
      } else {
        if (stock > 0) {
          console.log('Regular purchase, no coupon');
        } else {
          console.log('Lack of mobile phone stock'); }}//200 yuan deposit mode
    } else if (orderType = = = 2) {
      if (pay = = = true) {
        console.log('200 yuan deposit, 50 yuan coupon');
      } else {
        if (stock > 0) {
          console.log('Regular purchase, no coupon');
        } else {
          console.log('Lack of mobile phone stock'); }}//No deposit model
    } else if (orderType = = = 3) {
      if (stock > 0) {
        console.log('Regular purchase, no coupon');
      } else {
        console.log('Lack of mobile phone stock'); }}}order(1.true.500);
Copy the code

Chain of responsibilities, where a series of objects that might handle a request are connected into a chain, and the request is passed from one object to another until one is found that can handle it, reducing a lot of repetitive code.

  var order500 = function (orderType.pay.stock) {
    if (orderType = = = 1 && pay = = = true) {
      console.log('500 yuan deposit, 100 yuan coupon');
    } else {
      order200(orderType, pay, stock); }}var order200 = function (orderType.pay.stock) {
    if (orderType = = = 2 && pay = = = true) {
      console.log('200 yuan deposit, 50 yuan coupon');
    } else {
      orderNormal(orderType, pay, stock); }}var orderNormal = function (orderType.pay.stock) {
    if (stock > 0) {
      console.log('Regular purchase, no coupon');
    } else {
      console.log('Lack of mobile phone stock'); }}order500(1.true.500);
  order500(1.false.500);
  order500(2.true.500);
Copy the code

reference