责任链模式 - Chain of Responsibility

122 阅读2分钟

一、定义

使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系,将这些对象连成一条链,并沿着这条链 传递该请求,直到有一个对象处理它为止。

二、核心

请求发送者只需要知道链中的第一个节点,弱化发送者和一组接收者之间的强联系,可以便捷地在职责链中增加或删除一个节点;同样地,指定谁是第一个节点也很便捷。

三、责任链模式里的角色

  • 抽象处理者角色,定义处理方法,以配置是否具有下一个节点对象;
  • 具体处理这角色,定义处理方法的具体执行逻辑,判断是否可以处理该请求,如果可以就处理,如果不行,旧查看是否有下一个节点,有的话就传递给下家。

三、实现

一切可能需要经过几个流程直到某个流程节点能够处理它为止的情况,例:采纳审批流程。

// 流程
function Handler() {
  this.next = null;
  this.setNext = function(_handler) {
      this.next = _handler;
  };
  this.handleRequest = function(money) {
  }
};

// 采购部
function CGBHandler = function() {}
CGBHandler.prototype = new Handler();
CGBHandler.prototype.handleRequest = function(money) {
// 处理权限最多10000
  if (money < 10000) {
      console.log("同意");
  } else {
      console.log("金额太大,只能处理一万以内的采购");
      if (this.next) {
          this.next.handleRequest(money);
      }
  }
};

// 总经理
function ZJLHandler = function() {}
ZJLHandler.prototype = new Handler();
ZJLHandler.prototype.handleRequest = function (money) {
  if (money < 100000) {
      console.log("十万以内的同意");
  } else {
      console.log("金额太大,只能处理十万以内的采购");
      if (this.next) {
          this.next.handleRequest(money);
      }
  }
};

// 董事长
function DSZHandler = function() {}
DSZHandler.prototype = new Handler();
DSZHandler.prototype.handleRequest = function(money) {
  if (money >= 100000) {
      console.log("十万以上的我来处理");
  }
};

// 客户端使用
function Client() {
  var cgb = new CGBHandler();
  var zjl = new ZJLHandler();
  var dsz = new DSZHandler();
  cgb.setNext(zjl);
  zjl.setNext(dsz);

  cgb.handleRequest(800000);
}// 流程
function Handler() {
  this.next = null;
  this.setNext = function(_handler) {
      this.next = _handler;
  };
  this.handleRequest = function(money) {
  }
};

// 采购部
function CGBHandler () {}
CGBHandler.prototype = new Handler();
CGBHandler.prototype.handleRequest = function(money) {
// 处理权限最多10000
  if (money < 10000) {
      console.log("采购部:同意");
  } else {
      console.log("采购部:金额太大,只能处理一万以内的采购");
      if (this.next) {
          this.next.handleRequest(money);
      }
  }
};

// 总经理
function ZJLHandler () {}
ZJLHandler.prototype = new Handler();
ZJLHandler.prototype.handleRequest = function (money) {
  if (money < 100000) {
      console.log("总经理:十万以内的同意");
  } else {
      console.log("总经理:金额太大,只能处理十万以内的采购");
      if (this.next) {
          this.next.handleRequest(money);
      }
  }
};

// 董事长
function DSZHandler () {}
DSZHandler.prototype = new Handler();
DSZHandler.prototype.handleRequest = function(money) {
  if (money >= 100000) {
      console.log("董事长:十万以上的我来处理");
  }
};

// 测试
const cgb = new CGBHandler();
const zjl = new ZJLHandler();
const dsz = new DSZHandler();
cgb.setNext(zjl);
zjl.setNext(dsz);

cgb.handleRequest(800000);// 流程
function Handler() {
  this.next = null;
  this.setNext = function(_handler) {
      this.next = _handler;
  };
  this.handleRequest = function(money) {
  }
};

// 采购部
function CGBHandler () {}
CGBHandler.prototype = new Handler();
CGBHandler.prototype.handleRequest = function(money) {
// 处理权限最多10000
  if (money < 10000) {
      console.log("采购部:同意");
  } else {
      console.log("采购部:金额太大,只能处理一万以内的采购");
      if (this.next) {
          this.next.handleRequest(money);
      }
  }
};

// 总经理
function ZJLHandler () {}
ZJLHandler.prototype = new Handler();
ZJLHandler.prototype.handleRequest = function (money) {
  if (money < 100000) {
      console.log("总经理:十万以内的同意");
  } else {
      console.log("总经理:金额太大,只能处理十万以内的采购");
      if (this.next) {
          this.next.handleRequest(money);
      }
  }
};

// 董事长
function DSZHandler () {}
DSZHandler.prototype = new Handler();
DSZHandler.prototype.handleRequest = function(money) {
  if (money >= 100000) {
      console.log("董事长:十万以上的我来处理");
  }
};

// 测试
const cgb = new CGBHandler();
const zjl = new ZJLHandler();
const dsz = new DSZHandler();
cgb.setNext(zjl);
zjl.setNext(dsz);

cgb.handleRequest(800000);

// 输出:
采购部:金额太大,只能处理一万以内的采购
总经理:金额太大,只能处理十万以内的采购
董事长:十万以上的我来处理

跳转:设计模式目录