状态模式可以控制状态的转换, 未使用设计模式时, 程序会非常繁杂.
具体方法:
1. 状态转换类.
/** * @time 2014年7月11日 */ package state; /** * @author C.L.Wang * */ 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() { //输入25分钱 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() { //退回25分钱 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 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--; if (count == 0) { System.out.println("Opps, 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("waitring for turn of crank."); } else if (state == SOLD) { result.append("delivering a gumball."); } result.append("\n"); return result.toString(); } }
本栏目更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/project/
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索system
, result
, state
, else
, out
, println
State模式
state pattern、logback pattern 详解、urlpattern 设置详解、android pattern详解、lockpatternview详解,以便于您获取更多的相关知识。
时间: 2024-09-30 12:24:14