《Head.First设计模式》的学习笔记(14)--状态模式
意图:允许对象在内部状态改变时改变她的行为,对象看起来好像修改了它类。
结构:
我们是不是发现它和策略模式很像啊?这个我们将后面仔细介绍这两个模式共同点和不同点,现在我们还是先看例子。
我们现在有一个糖果机,有一下几个状态:
我们先来看我们平常的设计会如何进行实现:
public class GumballMachine { final static int SOLD_OUT = 0; final static int NO_QUARTER = 1; final static int HAS_QUARTER = 2; final static int SOLD = 3; int state = SOLD_OUT; int count = 0; public GumballMachine(int count) { this.count = count; if (count > 0) { state = NO_QUARTER; } } public void insertQuarter() { if (state == HAS_QUARTER) { System.out.println("You can't insert another quarter"); } else if (state == NO_QUARTER) { state = HAS_QUARTER; System.out.println("You inserted a quarter"); } else if (state == SOLD_OUT) { System.out.println("You can't insert a quarter, the machine is sold out"); } else if (state == SOLD) { System.out.println("Please wait, we're already giving you a gumball"); } } public void ejectQuarter() { if (state == HAS_QUARTER) { System.out.println("Quarter returned"); state = NO_QUARTER; } else if (state == NO_QUARTER) { System.out.println("You haven't inserted a quarter"); } else if (state == SOLD) { System.out.println("Sorry, you already turned the crank"); } else if (state == SOLD_OUT) { System.out.println("You can't eject, you haven't inserted a quarter yet"); } } public void turnCrank() { if (state == SOLD) { System.out.println("Turning twice doesn't get you another gumball!"); } else if (state == NO_QUARTER) { System.out.println("You turned but there's no quarter"); } else if (state == SOLD_OUT) { System.out.println("You turned, but there are no gumballs"); } else if (state == HAS_QUARTER) { System.out.println("You turned..."); state = SOLD; dispense(); } } public void dispense() { if (state == SOLD) { System.out.println("A gumball comes rolling out the slot"); count = count - 1; if (count == 0) { System.out.println("Oops, out of gumballs!"); state = SOLD_OUT; } else { state = NO_QUARTER; } } else if (state == NO_QUARTER) { System.out.println("You need to pay first"); } else if (state == SOLD_OUT) { System.out.println("No gumball dispensed"); } else if (state == HAS_QUARTER) { System.out.println("No gumball dispensed"); } } public void refill(int numGumBalls) { this.count = numGumBalls; state = NO_QUARTER; } public String toString() { StringBuffer result = new StringBuffer(); result.append("\nMighty Gumball, Inc."); result.append("\nJava-enabled Standing Gumball Model #2004\n"); result.append("Inventory: " + count + " gumball"); if (count != 1) { result.append("s"); } result.append("\nMachine is "); if (state == SOLD_OUT) { result.append("sold out"); } else if (state == NO_QUARTER) { result.append("waiting for quarter"); } else if (state == HAS_QUARTER) { result.append("waiting for turn of crank"); } else if (state == SOLD) { result.append("delivering a gumball"); } result.append("\n"); return result.toString(); } }
这个代码我们又看到不易扩展性,不稳定性,而且设计原则中不遵守“开放-关闭”原则,不符合面向对象,状态转换不明显,我们只能在if-else中看到,还有我们未能将改变部分进行包装起来。
例如我们现在添加一个需求,我们希望在添加一种赢家状态,就是转动把柄时,除了有机会正常获得一个糖果,还有10%的赢家机会获得2个糖果。
首先我们将状态图先画出来:
这时候我们就要去修改转动把柄的代码,这样就会混乱无比
我们新的设计是,我们将状态对象分装在各自的类中,然后在动作发生时当前的状态。
先看这些状态的类图:
我们先来实现其中一个状态,没有25分状态:
package headfirst.state.gumballstatewinner; public class NoQuarterState implements State { GumballMachine gumballMachine; public NoQuarterState(GumballMachine gumballMachine) { this.gumballMachine = gumballMachine; } public void insertQuarter() { System.out.println("You inserted a quarter"); gumballMachine.setState(gumballMachine.getHasQuarterState()); } public void ejectQuarter() { System.out.println("You haven't inserted a quarter"); } public void turnCrank() { System.out.println("You turned, but there's no quarter"); } public void dispense() { System.out.println("You need to pay first"); } public String toString() { return "waiting for quarter"; } }
看这个代码我们知道,我们将类图的上下文传入状态中,然后在通过传入的状态一旦实施改变状态的动作就是通过下面类似的一条语句进行修改:
gumballMachine.setState(gumballMachine.getHasQuarterState());
接着我们来看糖果机的代码:
package headfirst.state.gumballstatewinner; public class GumballMachine { State soldOutState; State noQuarterState; State hasQuarterState; State soldState; State winnerState; State state = soldOutState; int count = 0; public GumballMachine(int numberGumballs) { soldOutState = new SoldOutState(this); noQuarterState = new NoQuarterState(this); hasQuarterState = new HasQuarterState(this); soldState = new SoldState(this); winnerState = new WinnerState(this); this.count = numberGumballs; if (numberGumballs > 0) { state = noQuarterState; } } public void insertQuarter() { state.insertQuarter(); } public void ejectQuarter() { state.ejectQuarter(); } public void turnCrank() { state.turnCrank(); state.dispense(); } void setState(State state) { this.state = state; } void releaseBall() { System.out.println("A gumball comes rolling out the slot..."); if (count != 0) { count = count - 1; } } int getCount() { return count; } void refill(int count) { this.count = count; state = noQuarterState; } public State getState() { return state; } public State getSoldOutState() { return soldOutState; } public State getNoQuarterState() { return noQuarterState; } public State getHasQuarterState() { return hasQuarterState; } public State getSoldState() { return soldState; } public State getWinnerState() { return winnerState; } public String toString() { StringBuffer result = new StringBuffer(); result.append("\nMighty Gumball, Inc."); result.append("\nJava-enabled Standing Gumball Model #2004"); result.append("\nInventory: " + count + " gumball"); if (count != 1) { result.append("s"); } result.append("\n"); result.append("Machine is " + state + "\n"); return result.toString(); } }
我们可以看到在初始化糖果机,我们已经糖果机本身传入各个对象中,这样状态就可以通过糖果机调用各种动作时通过状态修改糖果机各种状态。最后我们来看新提的需求增加一个赢家模式,现在很明白我们就是要增加一个新的状态:
package headfirst.state.gumballstatewinner; public class WinnerState implements State { GumballMachine gumballMachine; public WinnerState(GumballMachine gumballMachine) { this.gumballMachine = gumballMachine; } public void insertQuarter() { System.out.println("Please wait, we're already giving you a Gumball"); } public void ejectQuarter() { System.out.println("Please wait, we're already giving you a Gumball"); } public void turnCrank() { System.out.println("Turning again doesn't get you another gumball!"); } public void dispense() { System.out.println("YOU'RE A WINNER! You get two gumballs for your quarter"); gumballMachine.releaseBall(); if (gumballMachine.getCount() == 0) { gumballMachine.setState(gumballMachine.getSoldOutState()); } else { gumballMachine.releaseBall(); if (gumballMachine.getCount() > 0) { gumballMachine.setState(gumballMachine.getNoQuarterState()); } else { System.out.println("Oops, out of gumballs!"); gumballMachine.setState(gumballMachine.getSoldOutState()); } } } public String toString() { return "despensing two gumballs for your quarter, because YOU'RE A WINNER!"; } }
其中在发放糖果方法中,我们实现赢家模式(其中我们随机的代码是在有硬币的状态下的扭转把柄的方法中,这边就不详细列出来了。)。
总结:
1.Context会将行为委托给当前状态对象,然后由状态对象执行相应的方法。
2.状态转换可以由Context或者State进行控制。
3.状态类可以被多个Context实例进行共享。
4.与策略模式的不同点与相同点
相同点:两边的类图是相同的一致的。
不同点:策略模式在设置算法族中的其中某一个算法时,就不会去在Context进行主动进行改变,而是由客户进行改变。而状态模式会随着时间进行改变。由State或者Context进行改变。