首页 > 代码库 > 设计模式 - 状态模式(state pattern) 未使用状态模式 详解

设计模式 - 状态模式(state pattern) 未使用状态模式 详解

状态模式(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 stub
		GumballMachine 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 #2004
Inventory: 5 gumballs
Machine 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 #2004
Inventory: 4 gumballs
Machine 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 #2004
Inventory: 4 gumballs
Machine 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 #2004
Inventory: 2 gumballs
Machine 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 gumballs

Mighty Gumball, Inc.
Java-enabled Standing Gumball Model #2004
Inventory: 0 gumballs
Machine is sold out.