console.log('职责链');
class Tank {
}
class AbstractHandler{
constructor() {
this.comand = 0;
this.function = "";
this.nexHandler = null;
}
handleRequest(comand) {
if (this.comand == comand) {
console.log(this.function);
} else {
if (this.nexHandler != null) {
this.nexHandler.handleRequest(comand);
}
}
}
}
class SortHandler extends AbstractHandler {
constructor() {
super();
this.comand = 1;
this.function = "发射炮弹";
this.nexHandler = new RunHandler();
}
}
class RunHandler extends AbstractHandler {
constructor() {
super();
this.comand = 2;
this.function = "跑";
this.nexHandler = new HongwaiHandler();
}
}
class HongwaiHandler extends AbstractHandler {
constructor() {
super();
this.comand = 3;
this.function = "红外扫描";
}
}
// 客户端
class Client {
main() {
var sortHandler = new SortHandler();
sortHandler.handleRequest(3);
}
}
var client = new Client();
client.main();
来源:https://blog.csdn.net/xie__jin__cheng/article/details/102777806