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();

posted @ 2018-12-22 16:22  node-吉利  阅读(662)  评论(0编辑  收藏  举报