二、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());

https://blog.csdn.net/song_mou_xia/article/details/80763833

猜你喜欢

转载自www.cnblogs.com/wangc04/p/12983900.html