1. 程式人生 > >ES設計模式之狀態模式

ES設計模式之狀態模式

物件有不同的狀態,不同的狀態對應不同的動作。 這樣也有確定就是有很多冗餘的程式碼,但是這些程式碼是比較容易維護的的, 如果你的程式碼需要修改,不是寫一次就不用了,這還是很有必要的。 注意每一個狀態都具有相同的介面。我感覺這是一種浪費,因為有些狀態之間是不能直接切換的。但是不容加上防止後期出錯。

程式碼如下:這段程式碼比較有意思需要注意。水果機的建構函式。

class GumballMachine{ constructor(number){ this.SOLD_OUT = new SoldoutState(this); this.NO_QUARER = new NoQuarterState(this); this.HAS_QUARTER = new HasQuarterState(this); this.SOLD = new SoldState(this); this.count = number; if(this.count > 0){ this.state = this.NO_QUARER; } this.getNoQuarterState = function(){ return this.NO_QUARER; }

this.getHasQuarterState = function(){ return this.HAS_QUARTER; } this.getSoldState = function(){ return this.SOLD; } this.getSoldoutState = function(){ return this.SOLD_OUT; } this.setState = function(state){ this.state = state; }

}

insertQuarter(){ this.state.insertQuarter() }

ejectQuarter(){ this.state.ejectQuarter() }

trunCrank(){ this.state.trunCrank() this.state.dispense() }

releaseBall(){ console.log(‘A gumball comes rolling out the slot…’); if(this.count != 0){ this.count -= 1; } }

getCount(){ return this.count; } }

// 糖果機裡沒有硬幣

class NoQuarterState{ constructor(GumballMachine){ this.bumballMachine = GumballMachine; }

insertQuarter(){ console.log(“you insert a quarter!”);

// console.log(this.bumballMachine);

this.bumballMachine.setState(this.bumballMachine.getHasQuarterState()) }

ejectQuarter(){ console.log(“you don’t insert a querter!”); }

trunCrank(){ console.log(“your turnned,but there is no quarter!”); }

dispense(){ console.log(“you need to pay first!”); } }

class HasQuarterState{ constructor(GumballMachine){ this.bumballMachine = GumballMachine; } insertQuarter(){ console.log(“you can’t insert another quarter!”); } ejectQuarter(){ console.log(“querter return!”); this.bumballMachine.setState(this.bumballMachine.getNoQuarterState()) } trunCrank(){ console.log(“you turned…”) this.bumballMachine.setState(this.bumballMachine.getSoldState()) } dispense(){ console.log(“no gumball dispensed”); } }

class SoldState{ constructor(GumballMachine){ this.bumballMachine = GumballMachine; } insertQuarter(){ console.log(“please waite ,you are already giving you a gumbail!”); } ejectQuarter(){ console.log(“sorry,you already turned the crank”); } trunCrank(){ console.log(“turn twice dosen’t get you another gumbail”); } dispense(){ this.bumballMachine.releaseBall(); if(this.bumballMachine.getCount() > 0){ this.bumballMachine.setState(this.bumballMachine.getNoQuarterState()) }else{ console.log(“Oops,out of gumballs”); this.bumballMachine.setState(this.bumballMachine.getSoldoutState()) } } }

class SoldoutState{ constructor(GumballMachine){ this.bumballMachine = GumballMachine; } insertQuarter(){ console.log(“please waite,the gumbail is sold out!”); } ejectQuarter(){ console.log(“sorry,you don’t input a quarter”); } trunCrank(){ console.log(“please stop,there has no gumbail!”); } dispense(){ console.log(“there is no gumbail,you will get nothing”); } }

var bumballMachine = new GumballMachine(3); bumballMachine.insertQuarter(); bumballMachine.ejectQuarter(); bumballMachine.insertQuarter(); bumballMachine.trunCrank(); bumballMachine.ejectQuarter();