在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();="" }="" <="" code="">