• 状态模式


    允许一个对象在其内部状态改变时改变它的行为,对象看起来似乎修改了它的类。(开关灯)

    状态模式的通用结构:

    在一个Light的构造函数中,我们要创建每一个状态类的实例对象,Context将持有这些状态对象引用,以便把请求委托给状态对象。

    //关灯
    var
    OffLightState = function( light ){ this.light = light; } OffLightState.prototype.buttonWasPress = function(){ console.log('弱光'); this.light.setCurrState = ( this.light.weakLightState ); }
    //弱光
    var WeakLightState = function( light ){ this.light = light; } WeakLightState.prototype.buttonWasPress = function(){ console.log('强光'); this.light.setCurrState = ( this.light.StrongLightState ); } var Light = function() { this.offLightState = new OffLightState( this ); this.weakLightState = new WeakLightState( this ); this.strongLightState = new StrongLightState( this ); this.button = null; }; Light.prototype.init = function(){ var button = document.createElement('button'), self = this; this.button = document.body.appendChild( button ); this.button.innerHTML = '开关'; this.currState = this.offLightState; //设置默认状态 this.button.onclick = function(){ self.currState.buttonWasPress(); } } Light.prototype.setCurrState = function( newState ){ this.currState = newState; }

    为避免一个对象遗漏方法的 技巧是:

    var State = function(){};
    State.prototype.buttonWasPressed = function(){
        throw new Error('父类的buttonWasPressed方法必须被重写');
    }
    var SupperStrongLightState = function( light ){
        this.light = light;
    }
    
    SupperStrongLightState.prototype = new State();
    
    SupperStrongLightState.prototype.buttonWasPressed = function(){  //这里需要重写
    
    }

    JavaScript版本的状态机。

    JS可以很方便的使用委托技术,并不需要事先让一个对象持有另一个对象。下面的状态机通过Function.prototype.call方法直接把请求委托给某在字面量对象来执行。

    var Light = function() {
        this.currState = FSW.off;  //设置当前状态
        this.button = null;
    };
    
    Light.prototype.init = function(){
        var button = document.createElement('button'), self = this;
        this.button = document.body.appendChild( button );
        this.button.innerHTML = '已关灯';
    
        this.button.onclick = function(){
            self.currState.buttonWasPress.call( self );
        }
    }
    var FSW = {
        off:{
            buttonWasPress: function(){
                console.log('关灯');
                this.button.innerHTML = '下一次按我是开灯';
                FSW.currState = FSW.on;
            }
        },
        on:{
            buttonWasPress: function(){
                console.log('开灯');
                this.button.innerHTML = '下一次按我是关灯';
                FSW.currState = FSW.off;
            }
        }
    }
  • 相关阅读:
    1130
    Oracle 数据库常用操作语句大全
    Oracle用sys登陆报:ORA-28009:connection as sys should be as sysdba
    导出数据报ORA-39002: 操作无效 ORA-39070: 无法打开日志文件。 ORA-39087: 目录名 DUMP_DIR 无效
    SGI STL源码stl_bvector.h分析
    SGI STL源码stl_vector.h分析
    CGI 萃取技术 __type_traits
    迭代器iterator和traits编程技法
    智能指针分析及auto_ptr源码
    C++深拷贝和浅拷贝细节理解
  • 原文地址:https://www.cnblogs.com/rainbow661314/p/7087285.html
Copyright © 2020-2023  润新知