二、js前端策略者模式

1.定义一系列的算法

2.将这些算法封装起来

3.可以让它们相互替换

const EmployeeA = function(){}
EmployeeA.prototype.calculate = function(salary){
return salary * 4;
}
const EmployeeB = function(){}
EmployeeB.prototype.calculate = function(salary){
return salary * 3;
}
const EmployeeC = function(){}
EmployeeC.prototype.calculate = function(salary){
return salary * 2;
}
const Bounds = function(){
this.salary = null;
this.levelObj = null;
}
Bounds.prototype.setSalary = function(salary){
this.salary = salary
}
Bounds.prototype.setLevelObj = function(levelObj){
this.levelObj = levelObj
}
Bounds.prototype.getBounds = function(){
return this.levelObj.calculate(this.salary)
}
const bounds = new Bounds();
bounds.setSalary(10000)
bounds.setLevelObj(new EmployeeA())
console.info(bounds.getBounds());

 

 注1.柯里化处理

     function currying(fn){

               let arr = [];

              return function cd(){

                    if(arguments.length == 0) fn.apply(this,arr);

                   Array.prototype.push.apply(arr,[].slice.call(arguments));

                    return cd

             }

     }

 注2.https://blog.csdn.net/song_mou_xia/article/details/80763833设计模式

posted @ 2020-05-28 21:05  灏月天染  阅读(207)  评论(0编辑  收藏  举报