Policy pattern: Encapsulate a set of algorithmic policies and make them interchangeable

Before we talk about strategic patterns, let’s sketch two basic concepts:

  • Context class: is responsible for receiving user requests and sending them to the policy algorithm for execution
  • Strategy class (Strategy) : the concrete implementation of the Strategy algorithm receives the calculation request distributed by the environment class and returns the calculation result

Question

According to the employee performance grade, to achieve an employee bonus calculation algorithm

S => 4 * monthly salary A => 3 * monthly salary B => 2 * monthly salaryCopy the code

Intuitive implementation

When we get to the question above, intuitive thinking is of course a logical judgment. Such as

const calculateBonus = (performanceLevel, salary) => {
  if (performanceLevel === 'S') {
    return salary * 4
  }
  if (performanceLevel === 'A') {
    return salary * 3
  }
  if (performanceLevel === 'B') {
    return salary * 2
  }
  return salary
}

calculateBonus('S', 10000)
calculateBonus('A', 5000)
Copy the code

Yeah, it’s intuitive, it’s easy to understand. But it is not very extensible, and if the strategy changes and a ‘C’ performance is added, the calculation function has to be modified

Policy pattern implementation

Implementing the policy pattern in JavaScript could not be simpler, and we probably use it many times in actual development, but we don’t pay much attention to it. Such as

const Strategy = {
  S: (salary) => salary * 4,
  A: (salary) => salary * 3,
  B: (salary) => salary * 2,
}

const calculateBonus = (performanceLevel, salary) => {
  return Strategy[performanceLevel](salary)
}
Copy the code
  • CalculateBonus: Environment class, invariable part of policy pattern;
  • Strategy: Strategy class, specific implementation process of algorithm;

Of course, we can also use the class implementation

class StrategyS {
  constructor(salary) {
    this.salary = salary
  }
  calculate() {
    return this.salary * 4
  }
}

class StrategyA {
  constructor(salary) {
    this.salary = salary
  }
  calculate() {
    return this.salary * 3
  }
}

class StrategyB {
  constructor(salary) {
    this.salary = salary
  }
  calculate() {
    return this.salary * 2
  }
}

class Bonus {
  strategy = null
  setStrategy(strategy) {
    this.strategy = strategy
  }
  getBonus() {
    return this.strategy.calculate()
  }
}

const bonus = new Bonus()
bonus.setStrategy(new StrategyS(10000))
bonus.getBonus()
Copy the code

Policy mode is to remove the invariable part (policy distribution) and algorithm part, which is convenient for new and replacement of policies and enhance the flexibility of code

conclusion

advantages
  • Code combination is used to avoid multi-condition judgment.
  • Code can be taken, easy to expand and combine;
disadvantages
  • Compared with writing business logic directly, the policy mode will increase the amount of code appropriately, mainly focusing on the policy algorithm.
  • The business logic and strategy algorithm need to be clear, and the algorithm implementation process needs to be accurately isolated