代码拉取完成,页面将自动刷新
同步操作将从 老胡来也/EveryDay 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
责任链模式Chain of Responsibility Pattern
为请求创建了一个接收者对象的链,其对请求的发送者和接收者进行解耦,这种类型的设计模式属于行为型模式,在这种模式中,通常每个接收者都包含对另一个接收者的引用,如果一个对象不能处理该请求,那么它会把相同的请求传给下一个接收者,依此类推。
在面向对象设计中,责任链模式是一种由命令对象源和一系列处理对象组成的设计模式。每个处理对象都包含逻辑,这些逻辑定义了它可以处理的命令对象的类型,其余对象将传递给链中的下一个处理对象。为避免请求发送者与接收者耦合在一起,让多个对象都有可能接收请求,将这些对象连接成一条链,并且沿着这条链传递请求,直到有对象处理它为止。职责链上的处理者负责处理请求,客户只需要将请求发送到职责链上即可,无须关心请求的处理细节和请求的传递,所以职责链将请求的发送者和请求的处理者解耦了。
/*
当帐户中设置了三种付款方式 bank、paypal、bitcoin,
bank有100RMB,paypal有200RMB,bitcoin有300RMB,
付款的优先顺序是bank然后paypal,然后是bitcoin,
当试图购买价值251RMB的东西,使用责任链进行采购,
首先检查bank账户是否可以进行采购,如果可以,则进行采购,并将链条断开,
如果没有,请求将前进到帐户paypal检查金额是否足够,如果是,则进行采购,并将链条断开,
否则请求将继续转发,直到找到合适的处理程序。
在这里,bank、paypal、bitcoin是链条上的环节,整个现象就是责任链。
*/
class Account {
setNext(mode) {
this.mode = mode;
}
pay(amountToPay) {
if (this.canPay(amountToPay)) {
console.log(`Paid ${amountToPay} using ${this.name}`);
}else if(this.mode) {
console.log(`Cannot pay using ${this.name}. Proceeding...`);
this.mode.pay(amountToPay);
}else{
console.log("None of the accounts have enough balance");
}
}
canPay(amount) {
return this.balance >= amount;
}
}
class Bank extends Account {
constructor(balance) {
super();
this.name = "bank";
this.balance = balance;
}
}
class Paypal extends Account {
constructor(balance) {
super();
this.name = "Paypal";
this.balance = balance;
}
}
class Bitcoin extends Account {
constructor(balance) {
super();
this.name = "bitcoin";
this.balance = balance;
}
}
(function(){
const bank = new Bank(100); // Bank with balance 100
const paypal = new Paypal(200); // Paypal with balance 200
const bitcoin = new Bitcoin(300); // Bitcoin with balance 300
bank.setNext(paypal);
paypal.setNext(bitcoin);
bank.pay(251);
// Cannot pay using bank. Proceeding...
// Cannot pay using Paypal. Proceeding...
// Paid 251 using bitcoin
})();
https://github.com/WindrunnerMax/EveryDay
https://juejin.im/post/6844903855348514829
https://www.runoob.com/design-pattern/chain-of-responsibility-pattern.html
https://github.com/sohamkamani/javascript-design-patterns-for-humans#-chain-of-responsibility
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。