状态设计模式
package org.springframework.security.crypto.bcrypt; /** * 状态模式测试 */ public interface State { /** * 添加硬币 */ void insertCoin(); /** * 退出硬币 */ void returnCoin(); /** * 转动曲柄 */ void turnCrank(); /** * 发放操作 */ void dispense(); /** * 打印状态 */ void printstate(); }
package org.springframework.security.crypto.bcrypt; /** * 糖果机 */ 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("一块糖果推出!"); } } public int getCount() { return count; } public void printstate() { state.printstate(); } }
package org.springframework.security.crypto.bcrypt; import java.util.Random; /** * 有硬币 */ 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("你不能插入另一枚硬币!"); } @Override public void returnCoin() { // TODO Auto-generated method stub System.out.println("硬币回归!"); mCandyMachine.setState(mCandyMachine.mOnReadyState); } @Override public void turnCrank() { // TODO Auto-generated method stub System.out.println("转弯......!"); 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***"); } }
package org.springframework.security.crypto.bcrypt; /** * 在准备状态 */ public class OnReadyState implements State { private CandyMachine mCandyMachine; public OnReadyState(CandyMachine mCandyMachine){ this.mCandyMachine=mCandyMachine; } @Override public void insertCoin() { System.out.println("你已经插入了一枚硬币,接下来,请转动曲柄!"); mCandyMachine.setState(mCandyMachine.mHasCoin); } @Override public void returnCoin() { // TODO Auto-generated method stub System.out.println("你还没有插入硬币!"); } @Override public void turnCrank() { // TODO Auto-generated method stub System.out.println("你转身,但你还没有插入硬币!"); } @Override public void dispense() { // TODO Auto-generated method stub } @Override public void printstate() { // TODO Auto-generated method stub System.out.println("***OnReadyState***"); } }
package org.springframework.security.crypto.bcrypt; /** * 出售完状态 */ 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("你不能插入硬币,机器卖光了!"); } @Override public void returnCoin() { // TODO Auto-generated method stub System.out.println("你不能回来,你还没有插入硬币!"); } @Override public void turnCrank() { // TODO Auto-generated method stub System.out.println("你转过身来,但没有糖果!"); } @Override public void dispense() { // TODO Auto-generated method stub } @Override public void printstate() { // TODO Auto-generated method stub System.out.println("***SoldState***"); } }
package org.springframework.security.crypto.bcrypt; /** * 售出操作 */ 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("请稍等!我们给你一个糖果!"); } @Override public void returnCoin() { // TODO Auto-generated method stub System.out.println("你还没有插入硬币!"); } @Override public void turnCrank() { // TODO Auto-generated method stub System.out.println("我们给你一个糖果,让另一个什么都没有,!"); } @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***"); } }
package org.springframework.security.crypto.bcrypt; /** * 胜利者状态 */ 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("请稍等!我们给你一个糖果!"); } @Override public void returnCoin() { // TODO Auto-generated method stub System.out.println("你还没有插入硬币!"); } @Override public void turnCrank() { // TODO Auto-generated method stub System.out .println("我们给你一个糖果,让另一个什么都没有,!"); } @Override public void dispense() { // TODO Auto-generated method stub mCandyMachine.releaseCandy(); if (mCandyMachine.getCount() == 0) { mCandyMachine.setState(mCandyMachine.mSoldOutState); } else { System.out.println("你是赢家!你得到另一个糖果!"); mCandyMachine.releaseCandy(); if (mCandyMachine.getCount() > 0) { mCandyMachine.setState(mCandyMachine.mOnReadyState); } else { System.out.println("哦,糖果"); mCandyMachine.setState(mCandyMachine.mSoldOutState); } } } @Override public void printstate() { // TODO Auto-generated method stub System.out.println("***WinnerState***"); } }
@Test public void test01(){ CandyMachine mCandyMachine = new CandyMachine(6); mCandyMachine.printstate(); mCandyMachine.insertCoin(); mCandyMachine.printstate(); mCandyMachine.turnCrank(); mCandyMachine.printstate(); mCandyMachine.insertCoin(); mCandyMachine.printstate(); mCandyMachine.turnCrank(); mCandyMachine.printstate(); }
美梦成真,变为事实。