Head First设计模式(12):状态模式

更多的可以参考我的博客,也在陆续更新ing
http://www.hspweb.cn/

状态模式允许对象在内部状态改变时改变它的行为,对象看起来好像修改了它的类。

状态模式:封装基于状态的行为,并将行为委托到当前状态。
策略模式:将可以互换的行为封装起来,然后使用委托的方法,决定使用哪一个行为。
模板方法模式:由子类决定如何实现算法中的某些步骤。

例子是:售货机出售糖果,有10%的概率得到两颗糖果。

1.目录
Head First设计模式(12):状态模式_第1张图片
image
2.package machine

①GumballMachine.java

package machine;
import state.HasQuarterState;
import state.NoQuarterState;
import state.SoldOutState;
import state.SoldState;
import state.State;
import state.WinnerState;

public class GumballMachine {

    State soldOutState;
    State noQuarterState;
    State hasQuarterState;
    State soldState;
    State winnerState;

    State state = soldOutState;
    int count = 0;

    public GumballMachine(int numberGumballs) {
        soldOutState = new SoldOutState(this);
        noQuarterState = new NoQuarterState(this);
        hasQuarterState = new HasQuarterState(this);
        soldState = new SoldState(this);
        winnerState = new WinnerState(this);

        this.count = numberGumballs;
        if (numberGumballs > 0) {
            state = noQuarterState;
        } 
    }

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

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

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

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

    public void releaseBall() {
        System.out.println("糖果正在出货...");
        if (count != 0) {
            count = count - 1;
        }
    }

    public int getCount() {
        return count;
    }

    void refill(int count) {
        this.count = count;
        state = noQuarterState;
    }

    public State getState() {
        return state;
    }

    public State getSoldOutState() {
        return soldOutState;
    }

    public State getNoQuarterState() {
        return noQuarterState;
    }

    public State getHasQuarterState() {
        return hasQuarterState;
    }

    public State getSoldState() {
        return soldState;
    }

    public State getWinnerState() {
        return winnerState;
    }

    public String toString() {
        StringBuffer result = new StringBuffer();
        result.append("\n小明糖果公司, Inc.");
        result.append("\njava糖果状态模式 #2017");
        result.append("\n剩余: " + count + " 颗糖果");
//      if (count != 1) {
//          result.append("s");
//      }
        result.append("\n");
        result.append("机器正在处于  " + state + "\n");
        return result.toString();
    }
}

3.package state

①HasQuarterState.java

package state;

import java.util.Random;

import machine.GumballMachine;

public class HasQuarterState implements State {
    Random randomWinner = new Random(System.currentTimeMillis());
    GumballMachine gumballMachine;

    public HasQuarterState(GumballMachine gumballMachine) {
        this.gumballMachine = gumballMachine;
    }

    public void insertQuarter() {
        System.out.println("你不能再投硬币了");
    }

    public void ejectQuarter() {
        System.out.println("硬币已返回");
        gumballMachine.setState(gumballMachine.getNoQuarterState());
    }

    public void turnCrank() {
        System.out.println("你转下曲柄...");
        int winner = randomWinner.nextInt(10);
        if ((winner == 0) && (gumballMachine.getCount() > 1)) {
            gumballMachine.setState(gumballMachine.getWinnerState());
        } else {
            gumballMachine.setState(gumballMachine.getSoldState());
        }
    }

    public void dispense() {
        System.out.println("已经没有糖果了");
    }

    public String toString() {
        return "正在等待转曲柄...";
    }
}

②NoQuarterState.java

package state;
import machine.GumballMachine;

public class NoQuarterState implements State {
    GumballMachine gumballMachine;

    public NoQuarterState(GumballMachine gumballMachine) {
        this.gumballMachine = gumballMachine;
    }

    public void insertQuarter() {
        System.out.println("你已经投入硬币");
        gumballMachine.setState(gumballMachine.getHasQuarterState());
    }

    public void ejectQuarter() {
        System.out.println("你不能重复投入硬币");
    }

    public void turnCrank() {
        System.out.println("你转下曲柄,但是没有硬币");
     }

    public void dispense() {
        System.out.println("你需要先投入硬币");
    } 

    public String toString() {
        return "等待投入硬币状态";
    }
}

③SoldOutState.java

package state;
import machine.GumballMachine;

public class SoldOutState implements State {
    GumballMachine gumballMachine;

    public SoldOutState(GumballMachine gumballMachine) {
        this.gumballMachine = gumballMachine;
    }

    public void insertQuarter() {
        System.out.println("你不能再投入硬币了,糖果已经卖完了。");
    }

    public void ejectQuarter() {
        System.out.println("无法退回硬币,因为没有硬币投入");
    }

    public void turnCrank() {
        System.out.println("你转下曲柄,但是已经没有糖果了");
    }

    public void dispense() {
        System.out.println("已经没有糖果了");
    }

    public String toString() {
        return "糖果卖完了状态";
    }
}

④SoldState.java

package state;
import machine.GumballMachine;

public class SoldState implements State {
    GumballMachine gumballMachine;

    public SoldState(GumballMachine gumballMachine) {
        this.gumballMachine = gumballMachine;
    }

    public void insertQuarter() {
        System.out.println("请等待,糖果正在出货...");
    }

    public void ejectQuarter() {
        System.out.println("对不起,你已经转过曲柄了");
    }

    public void turnCrank() {
        System.out.println("转两次曲柄不会给你两颗糖果");
    }

    public void dispense() {
        gumballMachine.releaseBall();
        if (gumballMachine.getCount() > 0) {
            gumballMachine.setState(gumballMachine.getNoQuarterState());
        } else {
            System.out.println("oh!糖果已经卖完了");
            gumballMachine.setState(gumballMachine.getSoldOutState());
        }
    }

    public String toString() {
        return "分配糖果状态";
    }
}

⑤State.java

package state;

public interface State {

    public void insertQuarter();
    public void ejectQuarter();
    public void turnCrank();
    public void dispense();
}

⑥WinnerState.java

package state;
import machine.GumballMachine;

public class WinnerState implements State {
    GumballMachine gumballMachine;

    public WinnerState(GumballMachine gumballMachine) {
        this.gumballMachine = gumballMachine;
    }

    public void insertQuarter() {
        System.out.println("请等待,糖果正在出货...");
    }

    public void ejectQuarter() {
        System.out.println("请等待,糖果正在出货...");
    }

    public void turnCrank() {
        System.out.println("再次转曲柄不会得到糖果");
    }

    public void dispense() {
        System.out.println("恭喜你!中奖了!你获得两颗糖果");
        gumballMachine.releaseBall();
        if (gumballMachine.getCount() == 0) {
            gumballMachine.setState(gumballMachine.getSoldOutState());
        } else {
            gumballMachine.releaseBall();
            if (gumballMachine.getCount() > 0) {
                gumballMachine.setState(gumballMachine.getNoQuarterState());
            } else {
                System.out.println("Oops, out of gumballs!");
                gumballMachine.setState(gumballMachine.getSoldOutState());
            }
        }
    }

    public String toString() {
        return "奖励你两颗糖果,因为你中奖了";
    }
}

4.package test

①test.java

package test;
import machine.GumballMachine;

public class test {

    public static void main(String[] args) {
        GumballMachine gumballMachine = 
            new GumballMachine(10);

        System.out.println(gumballMachine);

        gumballMachine.insertQuarter();
        gumballMachine.turnCrank();
        gumballMachine.insertQuarter();
        gumballMachine.turnCrank();

        System.out.println(gumballMachine);

        gumballMachine.insertQuarter();
        gumballMachine.turnCrank();
        gumballMachine.insertQuarter();
        gumballMachine.turnCrank();

        System.out.println(gumballMachine);

        gumballMachine.insertQuarter();
        gumballMachine.turnCrank();
        gumballMachine.insertQuarter();
        gumballMachine.turnCrank();

        System.out.println(gumballMachine);

        gumballMachine.insertQuarter();
        gumballMachine.turnCrank();
        gumballMachine.insertQuarter();
        gumballMachine.turnCrank();

        System.out.println(gumballMachine);

        gumballMachine.insertQuarter();
        gumballMachine.turnCrank();
        gumballMachine.insertQuarter();
        gumballMachine.turnCrank();

        System.out.println(gumballMachine);
    }
}

你可能感兴趣的:(Head First设计模式(12):状态模式)