狀態設計模式

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();

    }
相關文章
相關標籤/搜索