⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gumballmachine.java

📁 深入浅出设计模式
💻 JAVA
字号:
package headfirst.proxy.gumballmonitor;public class GumballMachine {	State soldOutState;	State noQuarterState;	State hasQuarterState;	State soldState;	State winnerState; 	State state = soldOutState;	int count = 0; 	String location; 	public GumballMachine(String location, int count) {		soldOutState = new SoldOutState(this);		noQuarterState = new NoQuarterState(this);		hasQuarterState = new HasQuarterState(this);		soldState = new SoldState(this);		winnerState = new WinnerState(this);		this.count = count; 		if (count > 0) {			state = noQuarterState;		} 		this.location = location;	} 	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;		}	} 	public int getCount() {		return count;	}	public void refill(int count) {		this.count = count;		state = noQuarterState;	}    public State getState() {        return state;    }     public String getLocation() {        return location;    }     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();	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -