We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
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);
The text was updated successfully, but these errors were encountered:
No branches or pull requests
AOP实现职责链
异步职责链
职责链
The text was updated successfully, but these errors were encountered: