设计模式
1.状态模式
目的:实现一个自动机
1.所有状态类实现一个State接口,包含了各自状态下可以完成的动作。
2.Context记录当前状态,同时它拥有各个状态类的实例。
3.状态类又有Context对象,这样Context与状态类相关联。
eg:糖果机项目
0:待机
1:投入一元硬币
2:转动把手(或者选择放弃,退回硬币)
3:滑落一颗糖果
4:待机(根据机器内糖果库存情况,是否提示售罄)
状态图:
动作:插入硬币,返回硬币,转动把手,分发糖果
状态:准备好状态,有硬币状态,售出状态,售完状态
状态接口:
public interface State {
public void insertCoin();
public void returnCoin();
public void turnCrank();
public void dispense();
public void printstate();
}
状态实现:
public class OnReadyState implements State {
private CandyMachine mCandyMachine; //与Context相关联
public OnReadyState(CandyMachine mCandyMachine)
{
this.mCandyMachine=mCandyMachine;
}
@Override
public void insertCoin() {
// TODO Auto-generated method stub
System.out
.println("you have inserted a coin,next,please turn crank!");
mCandyMachine.setState(mCandyMachine.mHasCoin);
}
@Override
public void returnCoin() {
// TODO Auto-generated method stub
System.out.println("you haven't inserted a coin yet!");
}
@Override
public void turnCrank() {
// TODO Auto-generated method stub
System.out.println("you turned,but you haven't inserted a coin!");
}
@Override
public void dispense() {
// TODO Auto-generated method stub
}
@Override
public void printstate() {
// TODO Auto-generated method stub
System.out.println("***OnReadyState***");
}
}
public class HasCoin implements State {
private CandyMachine mCandyMachine;
public HasCoin(CandyMachine mCandyMachine) {
this.mCandyMachine = mCandyMachine;
}
@Override
public void insertCoin() {
// TODO Auto-generated method stub
System.out.println("you can't insert another coin!");
}
@Override
public void returnCoin() {
// TODO Auto-generated method stub
System.out.println("coin return!");
mCandyMachine.setState(mCandyMachine.mOnReadyState);
}
@Override
public void turnCrank() {
// TODO Auto-generated method stub
System.out.println("crank turn...!");
Random ranwinner=new Random();
int winner=ranwinner.nextInt(10);
if(winner==0)
{
mCandyMachine.setState(mCandyMachine.mWinnerState);
}else
{
mCandyMachine.setState(mCandyMachine.mSoldState);
}
}
@Override
public void dispense() {
}
@Override
public void printstate() {
// TODO Auto-generated method stub
System.out.println("***HasCoin***");
}
}
public class SoldState implements State {
private CandyMachine mCandyMachine;
public SoldState(CandyMachine mCandyMachine)
{
this.mCandyMachine=mCandyMachine;
}
@Override
public void insertCoin() {
// TODO Auto-generated method stub
System.out.println("please wait!we are giving you a candy!");
}
@Override
public void returnCoin() {
// TODO Auto-generated method stub
System.out.println("you haven't inserted a coin yet!");
}
@Override
public void turnCrank() {
// TODO Auto-generated method stub
System.out
.println("we are giving you a candy,turning another get nothing,!");
}
@Override
public void dispense() {
// TODO Auto-generated method stub
mCandyMachine.releaseCandy();
if (mCandyMachine.getCount() > 0) {
mCandyMachine.setState(mCandyMachine.mOnReadyState);
} else {
System.out.println("Oo,out of candies");
mCandyMachine.setState(mCandyMachine.mSoldOutState);
}
}
@Override
public void printstate() {
// TODO Auto-generated method stub
System.out.println("***SoldState***");
}
}
public class SoldOutState implements State {
private CandyMachine mCandyMachine;
public SoldOutState(CandyMachine mCandyMachine)
{
this.mCandyMachine=mCandyMachine;
}
@Override
public void insertCoin() {
// TODO Auto-generated method stub
System.out.println("you can't insert coin,the machine sold out!");
}
@Override
public void returnCoin() {
// TODO Auto-generated method stub
System.out
.println("you can't return,you haven't inserted a coin yet!");
}
@Override
public void turnCrank() {
// TODO Auto-generated method stub
System.out.println("you turned,but there are no candies!");
}
@Override
public void dispense() {
// TODO Auto-generated method stub
}
@Override
public void printstate() {
// TODO Auto-generated method stub
System.out.println("***SoldOutState***");
}
}
public class WinnerState implements State {
private CandyMachine mCandyMachine;
public WinnerState(CandyMachine mCandyMachine) {
this.mCandyMachine = mCandyMachine;
}
@Override
public void insertCoin() {
// TODO Auto-generated method stub
System.out.println("please wait!we are giving you a candy!");
}
@Override
public void returnCoin() {
// TODO Auto-generated method stub
System.out.println("you haven't inserted a coin yet!");
}
@Override
public void turnCrank() {
// TODO Auto-generated method stub
System.out
.println("we are giving you a candy,turning another get nothing,!");
}
@Override
public void dispense() {
// TODO Auto-generated method stub
mCandyMachine.releaseCandy();
if (mCandyMachine.getCount() == 0) {
mCandyMachine.setState(mCandyMachine.mSoldOutState);
} else {
System.out.println("you are a winner!you get another candy!");
mCandyMachine.releaseCandy();
if (mCandyMachine.getCount() > 0) {
mCandyMachine.setState(mCandyMachine.mOnReadyState);
} else {
System.out.println("Oo,out of candies");
mCandyMachine.setState(mCandyMachine.mSoldOutState);
}
}
}
@Override
public void printstate() {
// TODO Auto-generated method stub
System.out.println("***WinnerState***");
}
}
Context类:
public class CandyMachine {
State mSoldOutState;
State mOnReadyState;
State mHasCoin;
State mSoldState;
State mWinnerState;
private State state; //当前状态
private int count = 0;
public CandyMachine(int count) {
this.count = count;
mSoldOutState = new SoldOutState(this); //关联各个状态
mOnReadyState = new OnReadyState(this);
mHasCoin = new HasCoin(this);
mSoldState = new SoldState(this);
mWinnerState = new WinnerState(this);
if (count > 0) {
state = mOnReadyState;
} else {
state = mSoldOutState;
}
}
public void setState(State state) {
this.state = state;
}
public void insertCoin() {
state.insertCoin();
}
public void returnCoin() {
state.returnCoin();
}
public void turnCrank() {
state.turnCrank();
state.dispense();
}
void releaseCandy() {
// TODO Auto-generated method stub
if (count > 0) {
count = count - 1;
System.out.println("a candy rolling out!");
}
}
public int getCount() {
return count;
}
public void printstate() {
state.printstate();
}
}
随当前动作,state对象不停的切换,意味着状态的改变,客户并不会直接和State对象交互,而是抽出了共有的行为,将动作交给具体的状态类去执行,实现解耦,利于状态的扩展。