Policy Mode : Define a series of algorithms, encapsulate them one by one, and make them interchangeable.
My understanding is that the various methods are encapsulated into functions, and there is a common function that can invoke these methods. The advantage of this is that you can digest the internal branch judgment and make the code more efficient.
Calculate bonuses using policy mode
Now to achieve such a thing, the year-end award is based on the employee's salary base and the year-end performance of the issue. For example, a person with a performance of S has 4 times times the year-end award, a person with a performance of a year-end award has 3 times times the salary, and a performance of B is a year-end award of twice times the salary. Suppose the Finance department asks us to provide a piece of code to make it easier for them to calculate the employee's year-end award.
idea One : Create a function, receive two parameters, one is performance, one is the salary base, and then returns the year-end award
1 varcalculate =function(Level, salary) {//A calculation function was created2 if(level = = = ' S ') ){3 returnSalary * 4;4 }5 if(level = = = ' A ' ){6 returnSalary * 3;7 }8 if(level = = = ' B ') ){9 returnSalary * 2;Ten } One }; ACalculate (' B ', 20000);//Output: 40000 -Calculate (' S ', 6000);//Output: 24000
There are a lot of drawbacks to this piece of code
(1) The Calculate function is quite large and contains many if statements that need to cover all the logical branches
(2) The function lacks elasticity, if adds a new performance Class C, or wants to change the performance s the bonus coefficient to 5, then we must delve into the internal implementation of the Calculate function, which violates the open-closed principle.
idea two : with the combination function, a function to calculate the corresponding bonus, another function to determine which calculation function to call
1 varlevels =function(Salary) {2 returnSalary * 4;3 };4 varLevela =function(Salary) {5 returnSalary * 3;6 };7 varLevelb =function(Salary) {8 returnSalary * 2;9 };Ten One varcalculate =function(level, salary) { A if(level = = = ' S ') ){ - returnlevels (salary); - } the if(level = = = ' A ' ){ - returnLevela (salary); - } - if(level = = = ' B ') ){ + returnLevelb (salary); - } + }; A atCalculate (' A ', 10000);//Output: 30000
This code is better than the above, but still does not solve the key problem, the Calculate function may become more and more large, and the function is still lack of elasticity.
idea three : The strategy mode, the invariant part and the change part separates is the theme of each design pattern, the strategy pattern is no exception, the goal of the strategy mode is to separate the use of the algorithm from the realization of the algorithm.
The policy mode is divided into the policy function and the calling function, the calling function should not contain the branch judgment, the method that calls any policy function should be consistent:
1 vars=function(Salary) {2 returnSalary * 4;3 };4 5 varA=function(Salary) {6 returnSalary * 3;7 };8 9 varb=function(Salary) {Ten returnSalary * 2; One } A - varcalculate =function(Func, salary) {//Higher order functions - returnfunc (salary); the }; - -Calculate (S, 20000);//Output: 80000 -Calculate (A, 10000);//Output: 30000
Or use objects so that policies can relate to each other:
1 varLv = {2"S":function(Salary) {3 returnSalary * 4;4 },5"A":function(Salary) {6 returnSalary * 3;7 },8"B":function(Salary) {9 returnSalary * 2;Ten } One }; A - varcalculate =function(level, salary) { - returnlv[level] (salary);//The only way to use the LV object here is to make the calulate function clearer, the above calulate too generic the }; - -Calculate (' S ', 20000);//Output: 80000 -Calculate (' A ', 10000);//Output: 30000
Summarize
advantages of the Strategy mode :
The strategy mode can avoid multiple conditional selection statements effectively by using the techniques and ideas of combination, delegation and polymorphism.
The policy model provides perfect support for the open-closed principle, encapsulating the algorithms in separate strategy, making them easy to switch, easy to understand, and easy to extend.
in summary, the policy function is encapsulated, and the essence of the calling function is to invoke these policy functions .
JavaScript Design patterns and development Practices reading notes (5)--Strategy mode