如何运用JavaScript实现职责链模式及其具体应用案例?

更新于
2026-09-25 10:16:36
1阅读来源:SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1769个文字,预计阅读时间需要8分钟。

如何运用JavaScript实现职责链模式及其具体应用案例?

JavaScript设计模式——职责链模式原理与用法

介绍:在软件设计中,职责链模式是一种行为设计模式,允许将请求的发送者与接收者解耦。这种模式通过建立一个请求处理链,将请求逐级传递,直到找到合适的处理者。职责链模式特别适用于处理多个请求,其中每个请求可能由不同的对象处理。

原理:职责链模式的核心思想是创建一个处理请求的链,每个节点都是一个处理者,它们按照一定的顺序连接起来。当一个请求到达时,它会沿着这个链传递,直到找到一个能够处理该请求的处理者。

用法:以下是一个简单的职责链模式的实现示例:

javascript// 定义一个处理者类class Handler { constructor() { this.nextHandler=null; }

// 设置下一个处理者 setNextHandler(handler) { this.nextHandler=handler; }

// 处理请求的方法 handleRequest(request) { if (this.canHandle(request)) { this.processRequest(request); } else if (this.nextHandler) { this.nextHandler.handleRequest(request); } else { console.log('No handler for this request.'); } }

// 判断当前处理者是否可以处理请求 canHandle(request) { // 根据实际情况实现判断逻辑 return false; }

// 处理请求的具体方法 processRequest(request) { // 处理请求的逻辑 }}

// 创建具体的处理者class ConcreteHandlerA extends Handler { canHandle(request) { // 判断是否可以处理该请求 return request.type==='A'; }

processRequest(request) { console.log('ConcreteHandlerA handles the request: ' + request); }}

class ConcreteHandlerB extends Handler { canHandle(request) { // 判断是否可以处理该请求 return request.type==='B'; }

processRequest(request) { console.log('ConcreteHandlerB handles the request: ' + request); }}

// 设置处理者链let handlerA=new ConcreteHandlerA();let handlerB=new ConcreteHandlerB();handlerA.setNextHandler(handlerB);

如何运用JavaScript实现职责链模式及其具体应用案例?

// 创建请求let request={ type: 'A' };handlerA.handleRequest(request); // 输出: ConcreteHandlerA handles the request: A

request={ type: 'B' };handlerA.handleRequest(request); // 输出: ConcreteHandlerB handles the request: B

request={ type: 'C' };handlerA.handleRequest(request); // 输出: No handler for this request.

在实际应用中,可以根据需要创建更多具体的处理者,并将它们按照合适的顺序连接起来,形成一个灵活的职责链。

本文实例讲述了javascript设计模式 – 职责链模式原理与用法。分享给大家供大家参考,具体如下:

介绍:很多情况下,在一个软件系统中可以处理某个请求的对象不止一个。例如一个网络请求过来,需要有对象去解析request Body,需要有对象去解析请求头,还需要有对象去对执行对应controller。请求一层层传递,让每一个对象都基于请求完成自己的任务,然后将请求传递给下一个处理程序。是不是感觉有点中间件的感觉。

定义:职责链就是避免请求发送者与接收者耦合在一起,让多个对象都有可能接收请求。将这些对象连成一条链,并沿着链传递请求,直到有对象处理它为止。职责链模式是一种对象行为型模式。

场景:我们继续画圆,我们准备了两组示例:

示例:

var Circle = function(){ this.radius = 0; this.drawByRadius = function(radius){ if(radius < 5){ this.drawVerySmalCircle(); }else if(radius < 10){ this.drawSmalCircle(); }else if(radius < 15){ this.drawMediumCircle(); }else if(radius < 20){ this.drawBigCircle(); }else{ this.drawVeryBigCircle(); } } this.drawVerySmalCircle = function(){ console.log('画一个超小的圆( 5以下 )'); } this.drawSmalCircle = function(){ console.log('画一个小圆( 5-10 )'); } this.drawMediumCircle = function(){ console.log('画一个中圆 ( 10-15 )'); } this.drawBigCircle = function(){ console.log('画一个大圆 ( 15-20 )'); } this.drawVeryBigCircle = function(){ console.log('画一个超大的圆 ( 20以上 )'); } } var circle = new Circle(); circle.drawByRadius(30); //画一个超大的圆 ( 20以上 )

观察上面的代码,这是很常见的逻辑,通过参数来决定执行哪个方法。首先drawByRadius方法职责过重,其次这样的方式在修改,新增时需要修改源代码,不符合开关原则。

我们使用职责链模式重写下:

var drawSmalCircle = function(min,max){ this.max = max; this.min = min; this.nextCircle; this.setNextDraw = function(circle){ this.nextCircle = circle; } this.draw = function(radius){ console.log('执行:drawSmalCircle'); if(this.min < radius && radius < this.max){ console.log('画一个小圆( 10以下 )'); } if(this.nextCircle){ this.nextCircle.draw(radius) } } } var drawMediumCircle = function(min,max){ this.max = max; this.min = min; this.nextCircle; this.setNextDraw = function(circle){ this.nextCircle = circle; } this.draw = function(radius){ console.log('执行:drawMediumCircle'); if(this.min < radius && radius < this.max){ console.log('画一个中圆 ( 10-20 )'); } if(this.nextCircle){ this.nextCircle.draw(radius) } } } var drawBigCircle = function(min,max){ this.max = max; this.min = min; this.nextCircle; this.setNextDraw = function(circle){ this.nextCircle = circle; } this.draw = function(radius){ console.log('执行:drawBigCircle'); if(this.min < radius && radius < this.max){ console.log('画一个大圆 ( 20以上 )'); } if(this.nextCircle){ this.nextCircle.draw(radius) } } } function initChain(){ var smalCircle = new drawSmalCircle(0,10); var mediumCircle = new drawMediumCircle(10,20); var bigCircle = new drawBigCircle(20,100); smalCircle.setNextDraw(mediumCircle); mediumCircle.setNextDraw(bigCircle); return smalCircle; } var circle = initChain(); circle.draw(30) // 执行:drawSmalCircle // 执行:drawMediumCircle // 执行:drawBigCircle // 画一个大圆 ( 20以上 circle.draw(15) // 执行:drawSmalCircle // 执行:drawMediumCircle // 画一个中圆 ( 10-20 ) // 执行:drawBigCircle circle.draw(5) // 执行:drawSmalCircle // 画一个小圆( 10以下 ) // 执行:drawMediumCircle // 执行:drawBigCircle

以上就是职责链模式的实例代码,drawSmalCircle,drawMediumCircle,drawBigCircle称为处理者类,处理者类保存了下一级对象的引用,

当我每执行一次draw时,程序会挨个执行职责链上的每一个方法。

职责链模式分为纯职责链和不纯职责链,纯的职责链在处理请求时,只能选择全部处理不传递或者全部传递不处理。我们这里的例子就是不纯职责链。它允许处理完成后继续向后传递。

职责链模式总结:

优点:
* 降低耦合,互相都不清楚执行顺序以及执行处理的类。
* 请求对象仅需维持一个指向其后继者的引用,简化了对象的相互连接。
* 新增修改职责链结构方便,满足开关原则。

缺点:
* 由于没有明确接受者,可能职责链走到最后都没有被正确处理。
* 职责链较长时会导致系统性能受影响。
* 建链不当,会造成循环调用,导致系统陷入死循环。

适用场景:
* 多个对象处理同一请求
* 动态创建执行顺序,流程

感兴趣的朋友可以使用在线HTML/CSS/JavaScript代码运行工具:tools.jb51.net/code/HtmlJsRun测试上述代码运行效果。

更多关于JavaScript相关内容感兴趣的读者可查看本站专题:《javascript面向对象入门教程》、《JavaScript错误与调试技巧总结》、《JavaScript数据结构与算法技巧总结》、《JavaScript遍历算法与技巧总结》及《JavaScript数学运算用法总结》

希望本文所述对大家JavaScript程序设计有所帮助。

本文共计1769个文字,预计阅读时间需要8分钟。

如何运用JavaScript实现职责链模式及其具体应用案例?

JavaScript设计模式——职责链模式原理与用法

介绍:在软件设计中,职责链模式是一种行为设计模式,允许将请求的发送者与接收者解耦。这种模式通过建立一个请求处理链,将请求逐级传递,直到找到合适的处理者。职责链模式特别适用于处理多个请求,其中每个请求可能由不同的对象处理。

原理:职责链模式的核心思想是创建一个处理请求的链,每个节点都是一个处理者,它们按照一定的顺序连接起来。当一个请求到达时,它会沿着这个链传递,直到找到一个能够处理该请求的处理者。

用法:以下是一个简单的职责链模式的实现示例:

javascript// 定义一个处理者类class Handler { constructor() { this.nextHandler=null; }

// 设置下一个处理者 setNextHandler(handler) { this.nextHandler=handler; }

// 处理请求的方法 handleRequest(request) { if (this.canHandle(request)) { this.processRequest(request); } else if (this.nextHandler) { this.nextHandler.handleRequest(request); } else { console.log('No handler for this request.'); } }

// 判断当前处理者是否可以处理请求 canHandle(request) { // 根据实际情况实现判断逻辑 return false; }

// 处理请求的具体方法 processRequest(request) { // 处理请求的逻辑 }}

// 创建具体的处理者class ConcreteHandlerA extends Handler { canHandle(request) { // 判断是否可以处理该请求 return request.type==='A'; }

processRequest(request) { console.log('ConcreteHandlerA handles the request: ' + request); }}

class ConcreteHandlerB extends Handler { canHandle(request) { // 判断是否可以处理该请求 return request.type==='B'; }

processRequest(request) { console.log('ConcreteHandlerB handles the request: ' + request); }}

// 设置处理者链let handlerA=new ConcreteHandlerA();let handlerB=new ConcreteHandlerB();handlerA.setNextHandler(handlerB);

如何运用JavaScript实现职责链模式及其具体应用案例?

// 创建请求let request={ type: 'A' };handlerA.handleRequest(request); // 输出: ConcreteHandlerA handles the request: A

request={ type: 'B' };handlerA.handleRequest(request); // 输出: ConcreteHandlerB handles the request: B

request={ type: 'C' };handlerA.handleRequest(request); // 输出: No handler for this request.

在实际应用中,可以根据需要创建更多具体的处理者,并将它们按照合适的顺序连接起来,形成一个灵活的职责链。

本文实例讲述了javascript设计模式 – 职责链模式原理与用法。分享给大家供大家参考,具体如下:

介绍:很多情况下,在一个软件系统中可以处理某个请求的对象不止一个。例如一个网络请求过来,需要有对象去解析request Body,需要有对象去解析请求头,还需要有对象去对执行对应controller。请求一层层传递,让每一个对象都基于请求完成自己的任务,然后将请求传递给下一个处理程序。是不是感觉有点中间件的感觉。

定义:职责链就是避免请求发送者与接收者耦合在一起,让多个对象都有可能接收请求。将这些对象连成一条链,并沿着链传递请求,直到有对象处理它为止。职责链模式是一种对象行为型模式。

场景:我们继续画圆,我们准备了两组示例:

示例:

var Circle = function(){ this.radius = 0; this.drawByRadius = function(radius){ if(radius < 5){ this.drawVerySmalCircle(); }else if(radius < 10){ this.drawSmalCircle(); }else if(radius < 15){ this.drawMediumCircle(); }else if(radius < 20){ this.drawBigCircle(); }else{ this.drawVeryBigCircle(); } } this.drawVerySmalCircle = function(){ console.log('画一个超小的圆( 5以下 )'); } this.drawSmalCircle = function(){ console.log('画一个小圆( 5-10 )'); } this.drawMediumCircle = function(){ console.log('画一个中圆 ( 10-15 )'); } this.drawBigCircle = function(){ console.log('画一个大圆 ( 15-20 )'); } this.drawVeryBigCircle = function(){ console.log('画一个超大的圆 ( 20以上 )'); } } var circle = new Circle(); circle.drawByRadius(30); //画一个超大的圆 ( 20以上 )

观察上面的代码,这是很常见的逻辑,通过参数来决定执行哪个方法。首先drawByRadius方法职责过重,其次这样的方式在修改,新增时需要修改源代码,不符合开关原则。

我们使用职责链模式重写下:

var drawSmalCircle = function(min,max){ this.max = max; this.min = min; this.nextCircle; this.setNextDraw = function(circle){ this.nextCircle = circle; } this.draw = function(radius){ console.log('执行:drawSmalCircle'); if(this.min < radius && radius < this.max){ console.log('画一个小圆( 10以下 )'); } if(this.nextCircle){ this.nextCircle.draw(radius) } } } var drawMediumCircle = function(min,max){ this.max = max; this.min = min; this.nextCircle; this.setNextDraw = function(circle){ this.nextCircle = circle; } this.draw = function(radius){ console.log('执行:drawMediumCircle'); if(this.min < radius && radius < this.max){ console.log('画一个中圆 ( 10-20 )'); } if(this.nextCircle){ this.nextCircle.draw(radius) } } } var drawBigCircle = function(min,max){ this.max = max; this.min = min; this.nextCircle; this.setNextDraw = function(circle){ this.nextCircle = circle; } this.draw = function(radius){ console.log('执行:drawBigCircle'); if(this.min < radius && radius < this.max){ console.log('画一个大圆 ( 20以上 )'); } if(this.nextCircle){ this.nextCircle.draw(radius) } } } function initChain(){ var smalCircle = new drawSmalCircle(0,10); var mediumCircle = new drawMediumCircle(10,20); var bigCircle = new drawBigCircle(20,100); smalCircle.setNextDraw(mediumCircle); mediumCircle.setNextDraw(bigCircle); return smalCircle; } var circle = initChain(); circle.draw(30) // 执行:drawSmalCircle // 执行:drawMediumCircle // 执行:drawBigCircle // 画一个大圆 ( 20以上 circle.draw(15) // 执行:drawSmalCircle // 执行:drawMediumCircle // 画一个中圆 ( 10-20 ) // 执行:drawBigCircle circle.draw(5) // 执行:drawSmalCircle // 画一个小圆( 10以下 ) // 执行:drawMediumCircle // 执行:drawBigCircle

以上就是职责链模式的实例代码,drawSmalCircle,drawMediumCircle,drawBigCircle称为处理者类,处理者类保存了下一级对象的引用,

当我每执行一次draw时,程序会挨个执行职责链上的每一个方法。

职责链模式分为纯职责链和不纯职责链,纯的职责链在处理请求时,只能选择全部处理不传递或者全部传递不处理。我们这里的例子就是不纯职责链。它允许处理完成后继续向后传递。

职责链模式总结:

优点:
* 降低耦合,互相都不清楚执行顺序以及执行处理的类。
* 请求对象仅需维持一个指向其后继者的引用,简化了对象的相互连接。
* 新增修改职责链结构方便,满足开关原则。

缺点:
* 由于没有明确接受者,可能职责链走到最后都没有被正确处理。
* 职责链较长时会导致系统性能受影响。
* 建链不当,会造成循环调用,导致系统陷入死循环。

适用场景:
* 多个对象处理同一请求
* 动态创建执行顺序,流程

感兴趣的朋友可以使用在线HTML/CSS/JavaScript代码运行工具:tools.jb51.net/code/HtmlJsRun测试上述代码运行效果。

更多关于JavaScript相关内容感兴趣的读者可查看本站专题:《javascript面向对象入门教程》、《JavaScript错误与调试技巧总结》、《JavaScript数据结构与算法技巧总结》、《JavaScript遍历算法与技巧总结》及《JavaScript数学运算用法总结》

希望本文所述对大家JavaScript程序设计有所帮助。