1. 程式人生 > >一個java狀態機樣例的程式碼

一個java狀態機樣例的程式碼

      在UML當中有狀態機檢視,這個狀態機可以用於自動售貨機,自動售票機等等場景,下面是用java程式碼模擬的一個狀態機:

1.狀態機介面

package stateMachine;
/**
 * 狀態機介面
 * @author seacean
 * @date 2013-8-29
 */
public interface State {
	/**
	 * 投入硬幣
	 */
	void insertQuarter();
    /**
     * 根據搖動情況,處理搖動結果,返回處理結果,釋放糖果
     */
	void ejectQuarter();
    /**
     * 轉動搖柄
     */
	void turnCrank();
    /**
     * 機器放出糖果,處理機器內部狀態,返回初始可投幣狀態
     */
	void dispense();
}

2.帶有狀態機的機器

package stateMachine;
/**
 * 機器類,包含多種狀態,處理流程
 * @author seacean
 * @date 2013-8-29
 */
public class Machine {
	//機器本身包含所有的狀態機
	private State soldOutState;
	private State noQuarterState;
	private State hasQuarterState;
	private State soldState;

	private State state; //機器的當前狀態
	private int count = 0;//機器中當前糖果的數量
    /**
     * 初始化機器,引入所有的狀態機,初始化糖果數量,初始化機器狀態
     * @param count
     */
	public Machine(int count) {
		this.soldOutState = new SoldOutState(this);
		this.noQuarterState = new NoQuarterState(this);
		this.hasQuarterState = new HasQuarterState(this);
		this.soldState = new SoldState(this);
		this.count = count;
		if (this.count > 0) {
			this.state = noQuarterState;
		}
	}
    /**
     * 釋放糖果時的內部處理程式
     */
	public void releaseBall() {
		System.out.println("a gumball comes rolling out the solt...");
		if (count > 0) {
			count -= 1;
		}
	}
    
	public void insertQuerter() {
		state.insertQuarter();//加入硬幣
	}

	public void ejectQuarter() {
		state.ejectQuarter();
	}

	public void turnCrank() {
		state.turnCrank();
		state.dispense();
	}

	public State getSoldOutState() {
		return soldOutState;
	}

	public State getNoQuarterState() {
		return noQuarterState;
	}

	public State getHasQuarterState() {
		return hasQuarterState;
	}

	public State getSoldState() {
		return soldState;
	}

	public State getState() {
		return state;
	}

	public int getCount() {
		return count;
	}

	public void setState(State state) {
		this.state = state;
	}
}

3.下面是狀態機的一些實現類

package stateMachine;
/**
 * 機器處於沒有投硬幣的狀態
 * @author seacean
 * @date 2013-8-29
 */
public class NoQuarterState implements State {
	private Machine machine;

	public NoQuarterState(Machine machine) {
		this.machine = machine;
	}

	@Override
	public void insertQuarter() {
		System.out.println("please insert a quarter!");
		machine.setState(machine.getHasQuarterState());
	}

	@Override
	public void ejectQuarter() {
		System.out.println("please insert a quarter!");
	}

	@Override
	public void turnCrank() {
		System.out.println("please insert a quarter!");
	}

	@Override
	public void dispense() {
		System.out.println("please insert a quarter!");
	}

}
package stateMachine;
/**
 * 機器處於有硬幣,有糖果,沒有搖動的狀態
 * @author seacean
 * @date 2013-8-29
 */
public class HasQuarterState implements State {
    private Machine machine;
    
    public HasQuarterState(Machine machine){
    	this.machine=machine;
    }
	@Override
	public void insertQuarter() {
		System.out.println("You can not insert another quarter!");
	}

	@Override
	public void ejectQuarter() {
		System.out.println("Quarter returned!");
		machine.setState(machine.getNoQuarterState());
	}

	@Override
	public void turnCrank() {
		System.out.println("You turned ... ");
        machine.setState(machine.getSoldState());
	}

	@Override
	public void dispense() {
		System.out.println("No gumball dispensed!");
	}

}

package stateMachine;

/**
 * 機器正在出售糖果的狀態
 * 
 * @author seacean
 * @date 2013-8-29
 */
public class SoldState implements State {
	private Machine machine;

	public SoldState(Machine machine) {
		this.machine = machine;
	}

	@Override
	public void insertQuarter() {
		System.out.println("please wait,we are already giving you a gumball!");
	}

	@Override
	public void ejectQuarter() {
		System.out.println("Sorry, you have turned the crank!");
	}

	@Override
	public void turnCrank() {
		System.out.println("Turning twice does not get you another gumball!");
	}

	@Override
	public void dispense() {
		machine.releaseBall();
		if (machine.getCount() > 0) {
			machine.setState(machine.getNoQuarterState());
		} else {
			System.out.println("Out of Gumballs!");
			machine.setState(machine.getSoldOutState());
		}
	}

}

package stateMachine;
/**
 * 機器處於無糖果狀態
 * @author seacean
 * @date 2013-8-29
 */
public class SoldOutState implements State {
    private Machine machine;
	public SoldOutState(Machine machine) {
		this.machine=machine;
	}

	@Override
	public void insertQuarter() {
		System.out.println("Sorry, there is no gumball in the machine!");
	}

	@Override
	public void ejectQuarter() {
		System.out.println("Sorry, there is no gumball in sold!");
	}

	@Override
	public void turnCrank() {
		System.out.println("Sorry, there is no gumball!Turn is no meaning.");
		machine.setState(machine.getNoQuarterState());
	}

	@Override
	public void dispense() {
		System.out.println("Sorry, there is no gumball!");
	}

}

4.下面是測試類

package stateMachine;
//測試類
public class StateMachineTest {
	public static void main(String[] args) {
         Machine machine=new Machine(10);
         for(int i=0;i<11;i++){
         System.out.println(machine);
         machine.insertQuerter();
         machine.turnCrank();
         }
	}
}