一个请求(数据),请求先经过A处理器处理,然后再把请求传递给B处理器,B处理完在往C传,以此类推,链条上的每个处理器 各自承担各自的处理职责
定金返优惠券
公司针对支付过定金的用户有一定的优惠政策,在正式购买后已经支付过100定金的返50优惠券,已经支付200的返100优惠券!没有支付定金正常购买的没有优惠券!
class TaskChain {//target:目标任务constructor(target,data) {this.data = data || {};this.target = targetthis.index = 0this.interceptorSize = 0this.interceptors = []}//添加拦截器addInterceptor(interceptor) {this.interceptors.push(interceptor)this.interceptorSize++return this}//执行任务start() {this.index = 0this.next()}//继续执行next() {if (this.index < this.interceptorSize) {this.interceptors[this.index++](this.data, this.next.bind(this))return}if (this.target) {this.target()}}}function order100({ orderType, payStatus }, next){console.log('order100')if( orderType === '100' && payStatus ){return console.log('100预约金、得到50优惠券')}next()}function order200({ orderType, payStatus }, next){console.log('order200')if( orderType === '200' && payStatus ){return console.log('200预约金、得到100优惠券')}next()}let taskChain = new TaskChain(() =>{console.log('责任链结束')});taskChain.addInterceptor(order100)taskChain.addInterceptor(order200)taskChain.start()
