设计模式 - 状态模式(state pattern) 未使用状态模式 详解
来源:互联网 发布:a一300美工介刀 编辑:程序博客网 时间:2024/06/03 18:21
状态模式(state pattern) 未使用状态模式 详解
本文地址: http://blog.csdn.net/caroline_wendy
状态模式可以控制状态的转换, 未使用设计模式时, 程序会非常繁杂.
具体方法:
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();}}
2. 测试代码.
/** * @time 2014年7月11日 */package state;/** * @author C.L.Wang * */public class GumballMachineTestDrive {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubGumballMachine gumballMachine = new GumballMachine(5);System.out.println(gumballMachine);gumballMachine.insertQuarter();gumballMachine.turnCrank();System.out.println(gumballMachine);gumballMachine.insertQuarter();gumballMachine.ejectQuarter();gumballMachine.turnCrank();System.out.println(gumballMachine);gumballMachine.insertQuarter();gumballMachine.turnCrank();gumballMachine.insertQuarter();gumballMachine.turnCrank();gumballMachine.ejectQuarter();System.out.println(gumballMachine);gumballMachine.insertQuarter();gumballMachine.insertQuarter();gumballMachine.turnCrank();gumballMachine.insertQuarter();gumballMachine.turnCrank();gumballMachine.insertQuarter();gumballMachine.turnCrank();System.out.println(gumballMachine);}}
3. 输出.
Mighty Gumball, Inc.Java-enabled Standing Gumball Model #2004Inventory: 5 gumballsMachine is waiting for quarter.You inserted a quarter.You turned...A gumball comes rolling out the slot.Mighty Gumball, Inc.Java-enabled Standing Gumball Model #2004Inventory: 4 gumballsMachine is waiting for quarter.You inserted a quarter.Quarter returned.You turned but there's no quarter.Mighty Gumball, Inc.Java-enabled Standing Gumball Model #2004Inventory: 4 gumballsMachine is waiting for quarter.You inserted a quarter.You turned...A gumball comes rolling out the slot.You inserted a quarter.You turned...A gumball comes rolling out the slot.You haven't inserted a quarter.Mighty Gumball, Inc.Java-enabled Standing Gumball Model #2004Inventory: 2 gumballsMachine is waiting for quarter.You inserted a quarter.You can't insert another quarter.You turned...A gumball comes rolling out the slot.You inserted a quarter.You turned...A gumball comes rolling out the slot.Opps, out of gumballs.You can't insert a quarter, the machine is sold out.You turned, but there are no gumballsMighty Gumball, Inc.Java-enabled Standing Gumball Model #2004Inventory: 0 gumballsMachine is sold out.
2 0
- 设计模式 - 状态模式(state pattern) 未使用状态模式 详解
- 设计模式 - 状态模式(state pattern) 详解
- 设计模式 - State Pattern(状态模式)
- 设计模式 - State Pattern(状态模式)
- 设计模式之State Pattern 状态模式。
- 设计模式24:State Pattern (状态模式)
- 设计模式-状态模式(State Pattern)
- 设计模式之状态模式(State Pattern)
- 设计模式-状态模式(state pattern)
- 设计模式之状态模式---State Pattern
- Java设计模式--状态模式【State Pattern】
- 设计模式【状态模式State Pattern】
- 【设计模式】状态模式(State Pattern)
- 状态模式(State Pattern)
- 状态模式(State Pattern)
- 状态模式(State Pattern)
- 状态模式(State Pattern)
- 状态模式【STATE PATTERN 】
- 总结整理Matlab的plot函数用法
- yii2.0安装
- drupal7 如何自定义内容部链接,并预留hook接口(自定义视图字段比较有用)
- 自己搜集的js面试题
- linux命令分割文件
- 设计模式 - 状态模式(state pattern) 未使用状态模式 详解
- 不仅带给你美妙的视觉感受,更承载着你的整个身体
- 的非官方的和烦得很梵蒂冈梵蒂冈发的
- sildingmenu使用
- MPC8309 NAND_SPL uboot初始化流程
- Spring Web Flow 2.0 入门
- poj 网络最大流模板
- 安卓源代码下面的docs文件夹
- java request获取请求路径的信息