JavaScript设计模式:二、策略模式

序、设计模式分类
一、装饰者模式(原型链)
二、策略模式
三、代理模式
四、发布订阅模式
五、迭代器模式
六、工厂模式
七、外观模式
八、状态模式
九、单例模式
十、适配器模式

一、概念

策略模式的定义是:定义一系列的算法,把它们一个个封装起来,并且使它们可以相互替换

策略模式指的是定义一系列的算法,把它们一个个封装起来。将不变的部分和变化的部分隔开是每个设计模式的主题,策略模式也不例外,策略模式的目的就是将算法的使用与算法的实现分离开来

一个基于策略模式的程序至少由两部分组成。第一个部分是一组策略类,策略类封装了具体 的算法,并负责具体的计算过程。 第二个部分是环境类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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值