官方网站建设平台,港南网站建设,网站服务器部署,泉州seo计费管理责任链模式
简介
将不同职责的步骤进行串联#xff0c;前一个执行完成之后才可以执行下一个#xff0c;即前一个的责任完成之后会将这个责任给到下一个。
组成结构
一共有两个主要的类
抽象的处理类#xff08;Handle#xff09;#xff0c;封装了每一个职责处理请求…责任链模式
简介
将不同职责的步骤进行串联前一个执行完成之后才可以执行下一个即前一个的责任完成之后会将这个责任给到下一个。
组成结构
一共有两个主要的类
抽象的处理类Handle封装了每一个职责处理请求的方法和下一步处理的处理器。具体的处理类ConcreteHandler对应抽象类的实现实际处理请求并将责任传递到下一个。
实现方式
class Handle {constructor () {this.handled false}setNext (handle) {this.next handlereturn this.next}deal (ctx) {// do something whit ctxif (!this.handled this.next) {this.next.deal(ctx)}return true}
}class HandleA extends Handle {deal (ctx) {// do something whit ctxctx.handleA trueif (!this.handled this.next) {this.next.deal(ctx)}return true}
}class HandleB extends Handle {deal (ctx) {// do something whit ctxctx.handleB trueif (!this.handled this.next) {this.next.deal(ctx)}return true}
}class HandleC extends Handle {deal (ctx) {// do something whit ctxctx.handleC trueif (!this.handled this.next) {this.next.deal(ctx)}return true}
}const handlea new HandleA()
handlea.setNext(new HandleB()).setNext(new HandleC())const ctx {}
handlea.deal(ctx)
console.log(ctx); // { handleA: true, handleB: true, handleC: true }可以看到由handled来决定是否在当前处理节点终止否则就会一直按照设置的链路往下执行这就是责任链的意义你可以把他应用于http请求的处理审批的设计实现等等。。。