职责链-坦克大战-js

我们两清 提交于 2019-12-02 15:04:17
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();

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!