Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JS 职责链模式 #15

Open
zhongs opened this issue May 9, 2020 · 0 comments
Open

JS 职责链模式 #15

zhongs opened this issue May 9, 2020 · 0 comments

Comments

@zhongs
Copy link
Owner

zhongs commented May 9, 2020

AOP实现职责链

Function.prototype.after = function (fn) {
  const _this = this;
  return function () {
    const ret = _this.apply(this, arguments);
    if (ret === "nextSuccessor") {
      return fn.apply(this, arguments);
    }
    return ret;
  };
};

const order500 = function (orderType, pay, stock) {
  if (orderType === 1 && pay === true) {
    console.log("500元定金购买,得到100元优惠券");
  } else {
    return "nextSuccessor";
  }
};

const order200 = function (orderType, pay, stock) {
  if (orderType === 2 && pay === true) {
    console.log("200元定金购买,得到50元优惠券");
  } else {
    return "nextSuccessor";
  }
};

const orderNormal = function (orderType, pay, stock) {
  if (stock > 0) {
    console.log("普通购买,没有优惠券");
  } else {
    console.log("手机库存不足");
  }
};

const order = order500.after(order200).after(orderNormal);

order(1, true, 500);
order(1, false, 500);

异步职责链


const order500 = function (orderType, pay, stock) {
  if (orderType === 1 && pay === true) {
    console.log("500元定金购买,得到100元优惠券");
  } else {
    return "nextSuccessor";
  }
};

const order200 = function (orderType, pay, stock) {
  if (orderType === 2 && pay === true) {
    console.log("200元定金购买,得到50元优惠券");
  } else {
    return "nextSuccessor";
  }
};

const orderNext = function (orderType, pay, stock) {
  setTimeout(() => this.next(), 2 * 1000);
};

const orderNormal = function (orderType, pay, stock) {
  if (stock > 0) {
    console.log("普通购买,没有优惠券");
  } else {
    console.log("手机库存不足");
  }
};

const Chain = function (fn) {
  this.fn = fn;
  this.successor = null;
};

Chain.prototype.setNextSuccessor = function (successor) {
  return (this.successor = successor);
};

Chain.prototype.passRequest = function () {
  const ret = this.fn.apply(this, arguments);
  if (ret === "nextSuccessor") {
    return (
      this.successor &&
      this.successor.passRequest.apply(this.successor, arguments)
    );
  }
  return ret;
};

Chain.prototype.next = function () {
  return (
    this.successor &&
    this.successor.passRequest.apply(this.successor, arguments)
  );
};

const chainOrder500 = new Chain(order500);
const chainOrder200 = new Chain(order200);
const chainOrderNormal = new Chain(orderNormal);
const chainOrderNext = new Chain(orderNext);

chainOrder500
  .setNextSuccessor(chainOrder200)
  .setNextSuccessor(chainOrderNext)
  .setNextSuccessor(chainOrderNormal);

chainOrder500.passRequest(1, false, 0);

职责链

/**
 * 需求:
 *  orderType:
 *    1 500元订单用户
 *    2 200元订单用户
 *    3 普通购买用户
 *  pay:
 *    是否支付定金
 *  stock:
 *    针对orderType = 3的普通用户,其它用户不受限制
 */

const order500 = function(orderType, pay, stock) {
  if (orderType === 1 && pay === true) {
    console.log("500元定金购买,得到100元优惠券");
  } else {
    return "nextSuccessor";
  }
};

const order200 = function(orderType, pay, stock) {
  if (orderType === 2 && pay === true) {
    console.log("200元定金购买,得到50元优惠券");
  } else {
    return "nextSuccessor";
  }
};

const orderNormal = function(orderType, pay, stock) {
  if (stock > 0) {
    console.log("普通购买,没有优惠券");
  } else {
    console.log("手机库存不足");
  }
};

//包装函数
const Chain = function(fn) {
  this.fn = fn;
  this.successor = null;
};

Chain.prototype.setNextSuccessor = function(successor) {
  return (this.successor = successor);
};

Chain.prototype.passRequest = function() {
  const ret = this.fn.apply(this, arguments);
  if (ret === "nextSuccessor") {
    return (
      this.successor &&
      this.successor.passRequest.apply(this.successor, arguments)
    );
  }
  return ret;
};

//将三种订单包装成职责链的节点
const chainOrder500 = new Chain(order500);
const chainOrder200 = new Chain(order200);
const chainOrderNormal = new Chain(orderNormal);

//指定职责链顺序
chainOrder500
  .setNextSuccessor(chainOrder200)
  .setNextSuccessor(chainOrderNormal);

chainOrder500.passRequest(1, true, 500);
chainOrder500.passRequest(2, true, 500);
chainOrder500.passRequest(3, true, 500);
chainOrder500.passRequest(1, false, 0);

@zhongs zhongs changed the title JS AOP实现职责链 JS 职责链模式 May 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant