序、设计模式分类
一、装饰者模式(原型链)
二、策略模式
三、代理模式
四、发布订阅模式
五、迭代器模式
六、工厂模式
七、外观模式
八、状态模式
九、单例模式
十、适配器模式
一、概念
策略模式的定义是:定义一系列的算法,把它们一个个封装起来,并且使它们可以相互替换。
策略模式指的是定义一系列的算法,把它们一个个封装起来。将不变的部分和变化的部分隔开是每个设计模式的主题,策略模式也不例外,策略模式的目的就是将算法的使用与算法的实现分离开来。
一个基于策略模式的程序至少由两部分组成。第一个部分是一组策略类,策略类封装了具体 的算法,并负责具体的计算过程。 第二个部分是环境类Context,Context 接受客户的请求,随后 把请求委托给某一个策略类。要做到这点,说明 Context中要维持对某个策略对象的引用。
策略模式的实现并不复杂,关键是如何从策略模式的实现背后,找到封装变化、委托和多态性这些思想的价值。
二、实现方式
-
原型实现
-
ES6类实现
-
字面量变量+函数对象实现
三、例子
不使用策略模式
var calculateBonus = function( performanceLevel, salary ){
if ( performanceLevel === 'S' ){
return salary * 4;
}
if ( performanceLevel === 'A' ){
return salary * 3;
}
if ( performanceLevel === 'B' ){
return salary * 2;
}
};
calculateBonus( 'B', 20000 ); // 输出:40000
calculateBonus( 'S', 6000 ); // 输出:24000
模拟传统面向对象策略模式(原型实现)
// 第一步:定义策略类(策略规则)
var performanceS = function(){};
performanceS.prototype.calculate = function( salary ){
return salary * 4;
};
var performanceA = function(){};
performanceA.prototype.calculate = function( salary ){
return salary * 3;
};
var performanceB = function(){};
performanceB.prototype.calculate = function( salary ){
return salary * 2;
};
// 第二步:环境类Context
var Bonus = function(){
this.salary = null; // 原始工资
this.strategy = null; // 绩效等级对应的策略对象
};
Bonus.prototype.setSalary = function( salary ){
this.salary = salary; // 设置员工的原始工资
};
Bonus.prototype.setStrategy = function( strategy ){
this.strategy = strategy; // 设置员工绩效等级对应的策略对象
};
// 通过原型调用策略类中的策略方法
Bonus.prototype.getBonus = function(){ // 取得奖金数额
return this.strategy.calculate( this.salary ); // 把计算奖金的操作委托给对应的策略对象
};
var bonus = new Bonus();
bonus.setSalary( 10000 );
// 为Context传入具体的策略类
bonus.setStrategy( new performanceS() ); // 设置策略对象
console.log( bonus.getBonus() ); // 输出:40000
bonus.setStrategy( new performanceA() ); // 设置策略对象
console.log( bonus.getBonus() ); // 输出:30000
JavaScript字面量对象实现
var strategies = {
'S': function(salary) { return salary * 4; },
'A': function(salary) { return salary * 3; },
'B': function(salary) { return salary * 2; }
};
var calcalateBonus = function(level, salary) {
return strategies[level](salary);
}
console.log(calcalateBonus('S', 2000));
console.log(calcalateBonus('A', 1000));
ES6类实现
var performanceS = function() {};
performanceS.prototype.caculate = function(salary) {
return salary * 4;
}
var performanceA = function() {};
performanceA.prototype.caculate = function(salary) {
return salary * 3;
}
var performanceB = function() {};
performanceB.prototype.caculate = function(salary) {
return salary * 2;
}
class Bonus {
constructor() {
this.salary = null;
this.strategy = null;
}
setSalary(salary) {
this.salary = salary;
}
setStrategy(strategy) {
this.strategy = strategy;
}
getBonus() {
return this.strategy.caculate(this.salary);
}
}
const bonus = new Bonus();
bonus.setSalary(10000);
bonus.setStrategy(new performanceS()); // 设置策略对象
console.log(bonus.getBonus()); // 输出:40000
bonus.setStrategy(new performanceA()); // 设置策略对象
console.log(bonus.getBonus()); // 输出:30000