什么是策略模式?

先看代码片段1。

// 代码片段1
var bonus = new Bonus();
bonus.setSalary(10000);
bonus.setStrategy(new performanceS());
console.log('bounsS' ,bonus.getBonus());
// => 40000 bonus.setStrategy(new performanceA());
console.log('bounsA',bonus.getBonus());
// => 30000

bonus是一个对象,而对象自带上下文。

这个对象在运行的不同阶段,通过setStrategy设置了不同的参数,导致同样的bonus.getBonus()输出结果不同。

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

下面的代码片段2是代码片段1的定义。

// 代码片段2
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;
}; 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);
};

改进的策略模式

同样的bonus.getBonus(),却输出结果不同。

当业务变得复杂,这会导致代码难以预测。

我们稍微改进一下,让对象初始化时接收一个策略对象,并且设置策略属性不可修改。

// 代码片段3
var bonusFactory = function(strategy){
this.salary = null;
Object.defineProperty(this, 'strategy',{
value: strategy,
writable: false,
configurable:false,
})
}; bonusFactory.prototype.setSalary = function (salary) {
this.salary = salary;
}; bonusFactory.prototype.getBonus = function () {
return this.strategy.calculate(this.salary);
}; var bonusS = new bonusFactory(new performanceS());
bonusS.setSalary(10000);
bonusS.strategy = 11;
console.log('bonusS', bonusS.getBonus()); var bonusA = new bonusFactory(new performanceA());
bonusA.setSalary(10000);
console.log('bonusA', bonusA.getBonus());

策略模式的函数式写法

这里使用了名为ramda的函数式工具库。

var R = require('ramda');

var salaryS = function(salary) {
return salary * 4;
}; var salaryA = function(salary) {
return salary * 3;
}; var salaryB = function(salary) {
return salary * 2;
}; var getBonus = function(salary, strategy){
return strategy(salary);
}; var getBonusFacotry = R.curry(getBonus);
var getBonusS = getBonusFacotry(R.__, salaryS);
var getBonusA = getBonusFacotry(R.__, salaryA);
var getBonusB = getBonusFacotry(R.__, salaryB); var getBouns1000 = getBonusFacotry(1000, R.__); console.log('封装奖金计算方式的策略'); console.log(getBonusS(1000));
console.log(getBonusA(1000));
console.log(getBonusB(1000)); console.log('封装奖金数目的策略'); console.log(getBouns1000(salaryS));
console.log(getBouns1000(salaryA));
console.log(getBouns1000(salaryB));

可以看到函数式写法更加灵活和简洁。

JavaScript形而上的策略模式的更多相关文章

  1. 第二章 --- 关于Javascript 设计模式 之 策略模式

    这一章节里面,我们会主要的针对JavaScript中的策略模式进行理解和学习 一.定义 策略模式: 定义一系列的算法,把他们封装起来,并且是他们可以相互替换. (这样的大的定义纲领,真的不好理解,特别 ...

  2. 理解javascript中的策略模式

    理解javascript中的策略模式 策略模式的定义是:定义一系列的算法,把它们一个个封装起来,并且使它们可以相互替换. 使用策略模式的优点如下: 优点:1. 策略模式利用组合,委托等技术和思想,有效 ...

  3. JavaScript设计模式(策略模式)

    策略模式的定义是:定义一系列的算法,把它们一个个封装起来,并且使它们可以相互替换.将不变的部分和变化的部分隔开是每个设计模式的主题,策略模式也不例外,策略模式的目的就是将算法的使用与算法的实现分离开来 ...

  4. JavaScript设计模式之策略模式(学习笔记)

    在网上搜索“为什么MVC不是一种设计模式呢?”其中有解答:MVC其实是三个经典设计模式的演变:观察者模式(Observer).策略模式(Strategy).组合模式(Composite).所以我今天选 ...

  5. JavaScript设计模式_02_策略模式

    在程序设计中,我们常常遇到这种情况,要实现某一个功能我们有很多种算法可以实现.这些算法灵活多样,而且可以随意互相替换.这种解决方案就是所谓的策略模式. /* * pre:策略模式 * 示例:公司计算奖 ...

  6. 再起航,我的学习笔记之JavaScript设计模式20(策略模式)

    策略模式 策略模式(Strategy):将定义的一组算法封装起来,使其相互之间可以替换.封装的算法具有一定的独立性,不会随客户端变化而变化. 其实策略模式在我们生活中可应用的地方还是比较多的,比如在商 ...

  7. JavaScript设计模式之策略模式

    所谓"条条道路通罗马",在现实中,为达到某种目的往往不是只有一种方法.比如挣钱养家:可以做点小生意,可以打分工,甚至还可以是偷.抢.赌等等各种手段.在程序语言设计中,也会遇到这种类 ...

  8. javascript设计模式:策略模式

    前言 策略模式有效利用组合.委托.多态等技术和思想,可以有效避免多重条件选择语句. 策略模式对开放-封闭原则提供了很好的支持,将算法封装在strategy中,使得他们易于切换.理解.扩展. 策略模式中 ...

  9. JavaScript设计模式之策略模式【组合委托】

    前言:语言只是工具,思想才是核心 今天要总结的是 策略模式 策略在开发中的应用非常广泛,所以也是非常常见且使用的设计模式. 在实际开发中,往往在实现一个功能时,有多种解决方案可行. 常见场景: 解压: ...

随机推荐

  1. 剑指offer——python【第30题】连续子数组的最大和

    题目描述 HZ偶尔会拿些专业问题来忽悠那些非计算机专业的同学.今天测试组开完会后,他又发话了:在古老的一维模式识别中,常常需要计算连续子向量的最大和,当向量全为正数的时候,问题很好解决.但是,如果向量 ...

  2. linux中时间命令详解

    DATE hling@hling:~$ date2018年 04月 11日 星期三 19:43:04 CSThling@hling:~$ date +%Y%M%d20184311hling@hling ...

  3. call()的个人理解

    先看两道道面试题 面试题1: var number = 50; var obj = { number: 60, getNum: function() { var number = 70; return ...

  4. php直接执行linux 命令

    注意你可以使用的命令只能是php这个用户组的权限和范围,注意这个linux 执行的,windows也是可以对应dos命令,但是打印格式不是很好看 //$output = `ls -al`; //$ou ...

  5. join算法分析

    对于单条语句,explain看下key,加个索引 多个条件,加复合索引 where a = ? order by b 加(a,b)的复合索引 上面都是比较基本的,这篇我们分析一些复杂的情况--join ...

  6. C# 如何使用长度来切分字符串

    参考网址:https://blog.csdn.net/yenange/article/details/39637211 using System; using System.Collections.G ...

  7. bugfree3.0.1-修改“优先级”“严重等级”为中文

    1.进入目录C:\xampp\htdocs\bugfree\protected\models 2.打开文件 Info.php

  8. 002-自定义打开terminal,以及快捷键,其他程序类似,ssh管理-sshpass, Shuttle

    一.利用Automator软件完成服务设定 1.使用Command+Space,打开Spotlight,搜索Automator 2.搜索到之后,双击打开,选择“服务[或快速操作]” 3.将“服务收到[ ...

  9. 271A

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h& ...

  10. Unicode编码学习

    unicode基础知识 简单来说,** unicode 是字符集,utf-8,utf-16,utf-32是编码规则.** unicode 字符集: ttps://unicode-table.com/ ...