This paper is summed up by our team Xiao Jianpeng
JavaScript design pattern strategy pattern
define
The definition of a policy pattern is to define a set of algorithms, encapsulate them, and make them interchangeable.
Separating invariant parts from changing parts is the theme of every design pattern. The strategy pattern is the separation of algorithm use from algorithm implementation.
A policy-based program consists of at least two parts:
- A group of policy classes that encapsulate specific algorithms and are responsible for specific calculation procedures.
- The Context class receives a request from a client and delegates the request to a policy class.
Year-end Bonus Calculation
Use a strategy model to calculate employees’ year-end bonuses.
Many companies pay bonuses based on employees’ salary base and year-end performance. For example, an S person gets 4 times his salary, an A person gets 3 times his salary, and A B person gets 2 times his salary. Suppose the finance department asks us for a code to help them calculate employees’ year-end bonuses.
Calculate the class
This class contains a year-end bonus calculation method strategies, which need to pass in the parameters performance level strategy and basic salary.
Do different processing according to the incoming performance level and return different results.
class Calculate {
constructor() {
}
static strategies(strategy, salary) {
switch(strategy) {
case 'S':
return salary * 4
case 'A':
return salary * 3
case 'B':
return salary * 2
default:
break
}
return 0
}
}
Copy the code
Bonus class
The Bonus class contains the attribute performance level strategy and base salary, as well as an instance method to get year-end Bonus, getBonus.
class Bonus {
constructor(salaey, strategy) {
this.salaey = salaey
this.strategy = strategy
}
getBonus(salaey, strategy) {
const result = Calculate.strategies(this.strategy, this.salaey)
console.log(result)
return result
}
}
Copy the code
call
Use the following code to call the test output:
Let bonus = new bonus (3000, 'S') bonus. GetBonus (Copy the code
Improve the year-end bonus calculation method
Through the above example, we have implemented a simple program for calculating annual bonus using the strategy mode. However, we will find that if SS,C and other grades appear in the performance level in the future, we need to constantly modify the Calculate class and add more cases in the switch cycle of strategies method. This violates the principle of easy maintenance, so what do we do? We can have a variety of solutions, such as putting dynamically configurable parts in a configuration file, or we can put them in a database to dynamically get the latest performance ratings.
Here we use performance ratings obtained from profiles. Here we create an enumeration type strategy
Enumeration class Enum. Js
Here we define a strategy to place a performance rating.
/** ** / const strategy = {SS: 5, S: 4, A: 3, B: 2} export {strategy} export default {strategy}Copy the code
Transform Calculate class
We replaced switch with Return Salary * strategy, so relying on the Calculate looks much easier.
/ / Class Calculate {constructor() {} static strategies(strategy) salary) { return salary * strategy } } export default CalculateCopy the code
call
Import {strategy} from ‘your path /Enum’
The calling code is changed to:
Let bonus = new bonus (3000, strategy.ss) bonus. GetBonus (Copy the code
We can also maintain salary as a configurable object.
So far we have implemented a relatively maintainable year-end bonus calculation feature using the policy pattern.
conclusion
In real projects, the policy pattern is much more useful for development than that. We will spread the meaning of the algorithm and use the policy pattern to encapsulate some business rules, as long as the business rules have the same goal and can be used interchangeably. For example, it can be used for form verification, animation, etc.