設(shè)計(jì)模式(九)-狀態(tài)模式

狀態(tài)模式

允許一個(gè)對(duì)象或者類,在其內(nèi)部狀態(tài)修改時(shí)改變它的行為.

  • 圖例
image.png
  • 普通代碼示例
class Battery{
    constructor(){
        this.amount=100
        this.state='success'
    }
    show(){
        if(this.amount==100){
            console.log('顯示綠色')
            this.state='success'
            this.amount=60
        }else if(this.amount==60){
            console.log('顯示黃色')
            this.state='warning'
            this.amount=30
        }else{
            console.log('顯示紅色')
            this.state='danger'
            this.amount=10
        }
    }
}

let batter=new Battery()
batter.show() //顯示綠色
batter.show()//顯示黃色
batter.show()//顯示紅色
  • 狀態(tài)模式優(yōu)化版
//定義單獨(dú)狀態(tài)類,把顯示的邏輯委托給了狀態(tài)對(duì)象,內(nèi)部還需要維護(hù)狀態(tài)變化.
class SuccessState{
   constructor(batter){
      this.batter=batter  
   }
   show(){
       console.log('顯示綠色')
       this.batter.amount=60
       return 'success'
   }
}

class WarningState{
    constructor(batter){
        this.batter=batter  
    }
    show(){
        console.log('顯示黃色')
        this.batter.amount=30
        return 'warning'
    }
}
class DangerState{
    constructor(batter){
        this.batter=batter  
    }
    show(){
        console.log('顯示紅色')
        this.batter.amount=10
        return 'danger'
    }
}

class Battery{
    constructor(){
        this.amount=100
        this.state=new SuccessState(this)
    }
    show(){
        this.state.show()
        if(this.amount==100){
            this.state=new SuccessState(this)
        }else if(this.amount==60){
            this.state=new WarningState(this)
        }else{
            this.state=new DangerState(this)
        }
      }
}

let batter=new Battery()
batter.show() //顯示綠色
batter.show()//顯示黃色
batter.show()//顯示紅色
  • 使用場(chǎng)景
javascript-state-machine庫(kù)
 const StateMachine=require('javascript-state-machine')
 var fsm = new StateMachine({
    init: 'solid',
    transitions: [
      { name: 'melt', from: 'solid', to: 'liquid' },
      { name: 'freeze', from: 'liquid', to: 'solid' },
      { name: 'vaporize', from: 'liquid', to: 'gas' },
      { name: 'condense', from: 'gas', to: 'liquid' }
    ],
    methods: {
      onMelt: function() { console.log('I melted')},
      onFreeze:  function() { console.log('I froze') },
      onVaporize: function() { console.log('I vaporized') },
      onCondense: function() { console.log('I condensed') }
    }
  });
fsm.freeze()
優(yōu)點(diǎn)
1.封裝了轉(zhuǎn)換規(guī)則
2.將所有某個(gè)狀態(tài)相關(guān)的行為放到一個(gè)類中,并可以方便的增加新的狀態(tài).
3.允許狀態(tài)轉(zhuǎn)換邏輯與狀態(tài)對(duì)象合成一體,而不是某一個(gè)巨大的條件語(yǔ)句塊.
4.可以讓多個(gè)對(duì)象或者類共享一個(gè)狀態(tài)對(duì)象.
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 設(shè)計(jì)模式專欄 設(shè)計(jì)模式專欄 1.定義: 當(dāng)一個(gè)對(duì)象的內(nèi)在狀態(tài)發(fā)生改變時(shí)允許改變其行為,這個(gè)對(duì)象看起來(lái)改變了其類 2...
    OneXzgj閱讀 257評(píng)論 0 3
  • 設(shè)計(jì)模式概述 在學(xué)習(xí)面向?qū)ο笃叽笤O(shè)計(jì)原則時(shí)需要注意以下幾點(diǎn):a) 高內(nèi)聚、低耦合和單一職能的“沖突”實(shí)際上,這兩者...
    彥幀閱讀 3,888評(píng)論 0 14
  • 設(shè)計(jì)模式匯總 一、基礎(chǔ)知識(shí) 1. 設(shè)計(jì)模式概述 定義:設(shè)計(jì)模式(Design Pattern)是一套被反復(fù)使用、多...
    MinoyJet閱讀 4,091評(píng)論 1 15
  • 學(xué)習(xí)并理解 23 種設(shè)計(jì)模式 設(shè)計(jì)模式 Design Pattern 是一套被反復(fù)使用、多數(shù)人知曉的、經(jīng)過分類編目...
    Code_Narrator閱讀 1,179評(píng)論 0 1
  • 狀態(tài)模式 State Pattern 定義:運(yùn)行對(duì)象內(nèi)部因?yàn)闋顟B(tài)發(fā)生改變時(shí),改變其行為操作。 主要解決:對(duì)象的行為...
    sssssss_閱讀 341評(píng)論 0 1

友情鏈接更多精彩內(nèi)容