SoFunction
Updated on 2025-03-03

Detailed explanation of JavaScript design pattern strategy pattern

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

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

The definition of a policy pattern is: define a series of algorithms, encapsulate them one by one, and make them replace each other.

/*
  * pre: policy mode
  * Example: The company calculates bonuses, divided into three performances: A, B, and C. The calculation method is as follows
  * Performance is A, bonus multiplied by 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;
 }
};
(calculateBonus('A', 2000));
/*
  Disadvantages: 1. The function system is huge and has too many if-else statements;
   2. If performance D is added, internal functions need to be modified, which violates the principle of closed-openness;
   3. Poor reusability. If you use calculation bonuses in other places, you can only assign and paste;
  */
//---------- Example 2 --------------var performanceA = function(salary) {
 return salary * 5;
};
var performanceB = function(salary) {
 return salary * 4;
};
var performanceC = function(salary) {
 return salary * 3;
};
var calculateBonus = function(performanceLevel, salary) {
 if(performanceLevel === 'A') {
  return performanceA(salary);
 }
 if(performanceLevel === 'B') {
  return performanceB(salary);
 }
 if(performanceLevel === 'C') {
  return performanceC(salary);
 }
};
(calculateBonus('A', 2000)); 
/*
  Disadvantages: 1. The functional system is huge and lacks flexibility when the system changes
  */
//---------- Example 3 --------------// Policy pattern reconstruction: define a series of algorithms and encapsulate them one by one.var performanceA = function(){};
 = function(salary){
 return salary * 5;
};
var performanceB = function(){};
 = function(salary){
 return salary * 4;
};
var performanceC = function(){};
 = function(salary){
 return salary * 3;
};

var Bonus = function(){
  = null;
  = null;
};
 = function(salary){
  = salary;
};
 = function(strategy){
  = strategy;
}
 = function(){
 return ();
}

var bonus = new Bonus();
(2000);
(new performanceA());
(());

// ------------- Example 4 ---------------------------------------------------------------------------------------------------------------// javaScript versionvar 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);
};
(caculateBonus("A",2000));

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.