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

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

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

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

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

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

推荐阅读更多精彩内容

  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 29,864评论 18 399
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 129,388评论 18 137
  • 一、设计模式的分类 总体来说设计模式分为三大类: 创建型模式,共五种:工厂方法模式、抽象工厂模式、单例模式、建造者...
    RamboLI阅读 551评论 0 1
  • 面向对象的六大原则 单一职责原则 所谓职责是指类变化的原因。如果一个类有多于一个的动机被改变,那么这个类就具有多于...
    JxMY阅读 703评论 1 3
  • 《大学》读书笔记(一) 我再读了读《大学》这本经典,上网查了下古代的大学与小学是怎样一回事,《礼记·王...
    天行健君马甲阅读 172评论 0 4
  • 其实 你有千万种理由 可以不遇上我 但你没有 其实 你有千万种理由 可以不关注我 但你没有 其实 你有千万种理由 ...
    80b9415fe545阅读 60评论 0 0
  • 一场突如其来的大雪裹挟着寒风 悄然而至 不消一会就把冷漠的建筑物 装扮的银装素裹 让人仿佛置身于童话世界 在那一刻...
    cherish刺心阅读 73评论 0 0
  • “没有了每周的美工例会,审美的培养也不能落下呀。” 一 图鱼 百分百完美平铺素材网络 www.hituyu.com...
    是心心阅读 108评论 0 0