状态模式: 允许对象在内部状态改变时改变它的行为,对象看起来好像修改了它的类。状态模式将状态封装成独立的类,并将动作委托到代表当前状态的对象。状态模式使用组合通过简单引用不同的状态对象来造成类改变的假象。
状态模式和策略模式有相同的类图,但它们的意图不同,策略模式会用行为和算法来配置Context类。状态模式允许Context随着状态的改变而改变其行为。
源代码:
</pre><pre name="code" class="java">package state; /** * 状态接口 * @author Arvon * */ public interface State { /** * 放入25分钱 */ public void insertQuarter(); /** * 取出25分钱 */ public void ejectQuarter(); /** * 扭动曲轴 */ public void turnCrank(); /** * 分配口香糖 */ public void dispense(); /** * 重新初始化口香糖机 */ public void refill(); }
package state; import java.util.Random; public class HasQuarterState implements State { Random randomWinner = new Random(System.currentTimeMillis()); GumballMachine gumballMachine; public HasQuarterState(GumballMachine gumballMachine) { this.gumballMachine = gumballMachine; } @Override public void insertQuarter() { System.out.println("You can't insert another quarter"); } @Override public void ejectQuarter() { System.out.println("Quarter returned"); gumballMachine.setState(gumballMachine.getNoQuarterState()); } @Override public void turnCrank() { System.out.println("You turned..."); int winner = randomWinner.nextInt(10); boolean isWinner = (winner == 0) && (gumballMachine.getCount() > 1); gumballMachine.setState(isWinner?gumballMachine.getWinnerState():gumballMachine.getSoldState()); } @Override public void dispense() { System.out.println("please turn the crank,or No gumball will be dispensed"); } @Override public void refill() { } }
package state; public class NoQuarterState implements State { GumballMachine gumballMachine; public NoQuarterState(GumballMachine gumballMachine) { this.gumballMachine = gumballMachine; } @Override public void insertQuarter() { System.out.println("You inserted a quarter"); gumballMachine.setState(gumballMachine.getHasQuarterState()); } @Override public void ejectQuarter() { System.out.println("You haven't inserted a quarter"); } @Override public void turnCrank() { System.out.println("You turned, but there's no quarter"); } @Override public void dispense() { System.out.println("You need to pay first"); } @Override public void refill() { } }
package state; /** * 售罄状态 * @author Administrator * */ public class SoldOutState implements State { GumballMachine mGumballMachine; public SoldOutState(GumballMachine mGumballMachine) { super(); this.mGumballMachine = mGumballMachine; } @Override public void insertQuarter() { // TODO Auto-generated method stub System.out.println("You can't insert a quarter, the machine is sold out"); } @Override public void ejectQuarter() { // TODO Auto-generated method stub System.out.println("You can't eject, you haven't inserted a quarter yet"); } @Override public void turnCrank() { // TODO Auto-generated method stub System.out.println("You turned, but there are no gumballs"); } @Override public void dispense() { // TODO Auto-generated method stub System.out.println("No gumball dispensed"); } @Override public void refill() { mGumballMachine.setState(mGumballMachine.getNoQuarterState()); } }
package state; public class SoldState implements State { private GumballMachine gumballMachine; public SoldState(GumballMachine gumballMachine) { this.gumballMachine = gumballMachine; } @Override public void insertQuarter() { System.out.println("Please wait, we're already giving you a gumball"); } @Override public void ejectQuarter() { System.out.println("Sorry, you already turned the crank"); } @Override public void turnCrank() { System.out.println("Turning twice doesn't get you another gumball!"); } @Override public void dispense() { gumballMachine.releaseBall(); if (gumballMachine.getCount() > 0) gumballMachine.setState(gumballMachine.getNoQuarterState()); else { System.out.println("Oops, out of gumballs!"); gumballMachine.setState(gumballMachine.getSoldOutState()); } } @Override public void refill() { // TODO Auto-generated method stub } }
package state; public class WinnerState implements State { private GumballMachine gumballMachine; public WinnerState(GumballMachine gumballMachine) { this.gumballMachine = gumballMachine; } @Override public void insertQuarter() { System.out.println("Please wait, we're already giving you a Gumball"); } @Override public void ejectQuarter() { System.out.println("Please wait, we're already giving you a Gumball"); } @Override public void turnCrank() { System.out.println("Turning again doesn't get you another gumball!"); } @Override public void dispense() { gumballMachine.releaseBall(); if (gumballMachine.getCount() == 0) gumballMachine.setState(gumballMachine.getSoldOutState()); else { gumballMachine.releaseBall(); System.out.println("YOU'RE A WINNER! You got two gumballs for your quarter"); if (gumballMachine.getCount() > 0) gumballMachine.setState(gumballMachine.getNoQuarterState()); else { System.out.println("Oops, out of gumballs!"); gumballMachine.setState(gumballMachine.getSoldOutState()); } } } @Override public void refill() { // TODO Auto-generated method stub } }
package state; /** * GumballMachine has soldOutState... * @author Administrator * */ 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 State getSoldOutState() { return soldOutState; } public State getSoldState() { return soldState; } public State getWinnerState() { return winnerState; } public State getNoQuarterState() { return noQuarterState; } public State getHasQuarterState() { return hasQuarterState; } public void setState(State noQuarterState) { state = noQuarterState; } public void insertQuarter() { state.insertQuarter(); } public void ejectQuarter() { state.ejectQuarter(); } public void turnCrank() { state.turnCrank(); state.dispense(); } public void releaseBall() { System.out.println("A gumball comes rolling out the slot..."); if (count != 0) count = count - 1; } public int getCount() { return count; } 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 state; public class StateTest { public static void main(String[] args) { GumballMachine mGumballMachine = new GumballMachine(10); System.out.println(mGumballMachine); mGumballMachine.ejectQuarter(); for(int i=0;i<6;i++){ mGumballMachine.insertQuarter(); mGumballMachine.turnCrank(); } System.out.println(mGumballMachine); } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/wen942467928/article/details/48153431