• JS模式--状态模式(状态机)


    • 下面的状态机选择通过Function.prototype.call方法直接把请求委托给某个字面量对象来执行。
     var light = function () {
                this.currstate = FSM.off;
                this.button = null;
            };
            light.prototype.init = function () {
                var button = document.createElement('button');
                self = this;
                button.innerHTML = '已关灯';
                this.button = document.body.appendChild(button);
    
                this.button.onclick = function () {
                    self.currstate.buttonWasPressed.call(self);
                };
            };
    
            var FSM = {
                off: {
                    buttonWasPressed: function () {
                        console.log('关灯');
                        this.button.innerHTML = '下一次按我是开灯';
                        this.currstate = FSM.on;
                    }
                },
                on: {
                    buttonWasPressed: function () {
                        console.log('开灯');
                        this.button.innerHTML = '下一次按我是关灯';
                        this.currstate = FSM.off;
                    }
                }
            };
            var light = new light();
            light.init();
    •  下面利用delegate函数来重写一遍这个状态机
    • 这是面向对象和闭包互换的一个例子。前者将变量保存为对象的属性,后者将变量封闭在闭包形成的环境中
    var delegate = function (client, delegation) {
                return {
                    buttonWasPressed: function () {
                        return delegation.buttonWasPressed.apply(client, arguments);
                    }
                };
            };
    
            var light = function () {
                this.offstate = delegate(this, FSM.off);
                this.onstate = delegate(this, FSM.on);
                this.currstate = FSM.off;
                this.button = null;
            };
            light.prototype.init = function () {
                var button = document.createElement('button');
                self = this;
                button.innerHTML = '已关灯';
                this.button = document.body.appendChild(button);
    
                this.button.onclick = function () {
                    self.currstate.buttonWasPressed.call(self);
                };
            };
    
            var FSM = {
                off: {
                    buttonWasPressed: function () {
                        console.log('关灯');
                        this.button.innerHTML = '下一次按我是开灯';
                        this.currstate = this.onstate;
                    }
                },
                on: {
                    buttonWasPressed: function () {
                        console.log('开灯');
                        this.button.innerHTML = '下一次按我是关灯';
                        this.currstate = this.offstate;
                    }
                }
            };
            var light = new light();
            light.init();

     
    Github上有另外一种方式:

    https://github.com/jakesgordon/javascript-state-machine

  • 相关阅读:
    Server2008 Enterprise SP2 is now installed
    “广” && “专”的抉择 个人技术发展之我见!
    从需求到UI的实现策略
    浙江行之杭州
    Windows 7 6956 安装过程感言
    为了生活选择了Microsoft,为了理想仰慕Google
    腾讯2009的一个小BUG,发现腾讯竟然也在用nginx
    生活GOOGLE,GOOGLE生活
    整理下自己电脑所用的软件下一步一步一步走做好该做的
    面试的一天二天
  • 原文地址:https://www.cnblogs.com/meiyh/p/6515477.html
Copyright © 2020-2023  润新知