js 디자인 모델 - 상태 모델 보충
 
     
     
     
    // OffLightState:
var OffLightState = function( light ){ 
    this.light = light;
};
OffLightState.prototype.buttonWasPressed = function(){ 
    console.log( ' ' ); // offLightState   
    this.light.setState( this.light.weakLightState );//   weakLightState
};
// WeakLightState:
var WeakLightState = function( light ){ 
    this.light = light;
};
WeakLightState.prototype.buttonWasPressed = function(){ 
    console.log( ' ' ); // weakLightState   
    this.light.setState( this.light.strongLightState ); //  strongLightState
};
// StrongLightState:
var StrongLightState = function( light ){ 
    this.light = light;
};
StrongLightState.prototype.buttonWasPressed = function(){
    console.log( ' ' ); // strongLightState  
    this.light.setState( this.light.offLightState ); //   offLightState
};
/*******************   Light  ,  ******************/
var Light = function(){
    this.offLightState = new OffLightState( this ); 
    this.weakLightState = new WeakLightState( this ); 
    this.strongLightState = new StrongLightState( this ); 
    this.button = null;
};
/********************     light   ************************/
Light.prototype.init = function(type){
    var button = document.createElement( 'button' ),
    self = this;
    
    this.currState = this[type];
    this.button.onclick = function(){ 
        self.currState.buttonWasPressed();
    } 
};
Light.prototype.setState = function( newState ){
    this.currState = newState; 
};
var light = new Light(); 
a = function(type){
    light.init(type); 
}
 
 
 상기 코드를 통해 알 수 있듯이 상태 모델은 나타날 수 있는 모든 상태를 단독 클래스로 봉인하고 이런 상태와 관련된 행위는 모두 이 클래스의 내부에 봉인된다.
모든 상황을 먼저 생각하고 어떤 상황이 생겼을 때 일치하는 셈이다.
참조 문서:https://juejin.im/post/5c23725bf265da61764aedc7
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
GSuite 계정으로 Slack에 로그인하면Gsuite의 계정으로 Slack에 로그인할 수 있으므로 그 때의 상황을 메모했습니다. ②Gsuite는 비즈니스 플랜 ③ 구글은 모두 로그아웃한 상태에서 작업할 것 설정 저장을 누르면 평소에 익숙한 구글의 로그인 화...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.