当前位置: 首页 > news >正文

JavaScript设计模式之责任链模式

适用场景:一个完整的流程,中间分成多个环节,各个环节之间存在一定的顺序关系,同时中间的环节的个数不一定,可能添加环节,也可能减少环节,只要保证顺序关系就可以。
如下图:
在这里插入图片描述

  1. ES5写法
const Chain = function (fn) {this.fn = fnthis.nextChain = nullthis.setNext = function (nextChain) {this.nextChain = nextChainreturn this.nextChain}this.run = function () {this.fn()this.nextChain && this.nextChain.run()}
}
//申请设备
const applyDevice = function () {console.log(111)
}
const chainApplyDevice = new Chain(applyDevice);
//选择收货地址
const selectAddress = function () {console.log(222)
}
const chainSelectAddress = new Chain(selectAddress);
//选择审核人
const selectChecker = function () {console.log(333)
}
const chainSelectChecker = new Chain(selectChecker);chainApplyDevice.setNext(chainSelectAddress).setNext(chainSelectChecker);
chainApplyDevice.run(); //最后执行结果为111-222-333
  1. ES6写法
class Chain {constructor(fn) {this.fn = fnthis.nextChain = null}setNext (nextChain) {this.nextChain = nextChainreturn this.nextChain}run() {this.fn()this.nextChain && this.nextChain.run()}
}
//申请设备
const applyDevice = function () {console.log(111)
}
const chainApplyDevice = new Chain(applyDevice);
//选择收货地址
const selectAddress = function () {console.log(222)
}
const chainSelectAddress = new Chain(selectAddress);
//选择审核人
const selectChecker = function () {console.log(333)
}
const chainSelectChecker = new Chain(selectChecker);chainApplyDevice.setNext(chainSelectAddress).setNext(chainSelectChecker);
chainApplyDevice.run(); //最后执行结果为111-222-333
http://www.lryc.cn/news/219482.html

相关文章:

  • 云安全—kubelet攻击面
  • leetcode经典面试150题---5.多数元素
  • Vue ElementUI el-tooltip 全局样式修改
  • MATLAB_5MW风电永磁直驱发电机-1200V直流并网MATLAB仿真模型
  • 11.4商业伦理(全)
  • 【漏洞复现】S2-045 Remote Code Execution(CVE-2017-5638)
  • Linux----------------Shell重定向输入输出
  • apachesolr中简单使用
  • C++多线程编程:其一、thread类概述
  • C++11 initializer_list 轻量级初始化列表的使用场景(让自定义类可以用初始化列表的形式来实例化对象)
  • 请求地址‘/operlog‘,发生未知异常
  • Makefile 保姆级使用教程
  • 【GitHub】Watch、Star、Fork、Follow 有什么区别?
  • MyBatis实现多表映射、分页显示、逆向工程
  • C++基础面试题
  • asp.net人事管理信息系统VS开发sqlserver数据库web结构c#编程Microsoft Visual Studio
  • 【Docker】Docker中 的AUFS、BTRFS、ZFS、存储池概念的详细讲解
  • 华为云运维小结
  • Firefox 119 正式发布
  • apachesolr启动带调试
  • 【MATLAB】基于灰狼优化算法优化BP神经网络 (GWO-BP)的数据回归预测
  • 雨水收集设施模块把雨水收集起来,经简单处理用于消防洗车冲厕等
  • Mac机RVM安装,手动下载安装,经过验证可以正常使用
  • 人工智能-深度学习之延后初始化
  • Jupyter Notebook交互式开源笔记本工具
  • 基于晶体结构算法的无人机航迹规划-附代码
  • 刷题笔记day11-栈与队列2
  • ngixn的指令
  • 管理类联考——数学——汇总篇——知识点突破——代数——函数、方程——记忆
  • 2014年亚太杯APMCM数学建模大赛C题公共基础课教师专业化培养方式研究求解全过程文档及程序