1. 职责链模式
将对象连成一条链,并沿着这条链传递请求,直到有一个对象处理它为止。
var chain = function(fn){
this.fn = fn;
this.successor = null;
}
chain.prototype.setNextSuccessor = function(successor){
return this.successor = successor;
}
chain.prototype.passRequest = function(){
var ret = this.fn.apply(this,arguments);
if(ret === ‘nextSuccessor‘){
return this.successor && this.successor.apply(this.successor,arguments);
}
return ret;
}
var order200 = function(pay){
if(pay){
console.log(‘200‘);
}else{
return ‘nextSuccessor‘;
}
}
var normal = function(pay){
if(pay){
console.log(‘10‘);
}else{
console.log(‘50‘);
}
}
var chain200 = new chain(order200);
var chainNormal = new chain(normal);
- order200.setNextSuccessor(chainNormal);
chain200.passRequest(false);
时间: 2024-10-13 02:34:24