设计模式学习专栏十一--------状态模式

设计模式学习专栏十一--------状态模式

名称: 状态模式 (State)

价值观念: 通过改变对象内部的状态来帮助对象控制自己的行为

场景


设计一个万能糖果机 , 我们希望设计尽可能有弹性 , 而且将来我们可能要为它增加更多的行为~

image

刚开始的设计方式

public class GumballMachine {
 
    final static int SOLD_OUT = 0;
    final static int NO_QUARTER = 1;
    final static int HAS_QUARTER = 2;
    final static int SOLD = 3;
 
    int state = SOLD_OUT;
    int count = 0;
  
    public GumballMachine(int count) {
        this.count = count;
        if (count > 0) {
            state = NO_QUARTER;
        }
    }
  
    public void insertQuarter() {
        if (state == HAS_QUARTER) {
            System.out.println("You can't insert another quarter");
        } else if (state == NO_QUARTER) {
            state = HAS_QUARTER;
            System.out.println("You inserted a quarter");
        } else if (state == SOLD_OUT) {
            System.out.println("You can't insert a quarter, the machine is sold out");
        } else if (state == SOLD) {
            System.out.println("Please wait, we're already giving you a gumball");
        }
    }
    
    public void ejectQuarter() {
        if (state == HAS_QUARTER) {
            System.out.println("Quarter returned");
            state = NO_QUARTER;
        } else if (state == NO_QUARTER) {
            System.out.println("You haven't inserted a quarter");
        } else if (state == SOLD) {
            System.out.println("Sorry, you already turned the crank");
        } else if (state == SOLD_OUT) {
            System.out.println("You can't eject, you haven't inserted a quarter yet");
        }
    }
    ......
}

我们的第一版设计完成了 , 发现每个动作下都需要判断当前的状态,然后做出相应的动作.

新功能引入 , 我们加入了一个新的游戏状态 , 当曲柄被转动时,有10%的几率掉下来的是两颗糖果(赢家状态)

此时我们会发现第一版设计中. 每个动作下 都需要新增条件判断 , 违反了对修改关闭的原则. 程序出错概率大大提升.

如何解决


分析程序扩展时的 可变部分与不变部分

insertCoin returnCoin trunCrank dispense
OnReadyState - - - -
HasCoin - - - -
SoldState - - - -
SoldOutState - - - -
WinnerState *** *** *** ***

不变部分: 从横向来看。 用户能执行的操作都是一样的。 (插入硬币,按下退币按钮,拉下把手)

变化部分: 从横向看。如果糖果工厂新增的状态, 对于用户每一种动作,糖果机的响应都是不同的。都要做出对应的修改

将可变部分抽取出来: 每一种状态都会执行4种操作,糖果机具体的操作与当前状态有关。 因此将状态与该状态下的对应的动作行为抽取出来形成接口。让每一个状态都实现该接口。

image

状态模式总览


定义:允许对象在内部状态在改变时改变它的行为,对象看起来好像改变了它的类
(将状态与该状态下的行为封装成独立的类,并将动作委托到代表当前状态的对象)

  • 模式的理解

    • 类图

      image
    • 角色

      • 上下文Context
      • 封装状态及该状态下行为的 State
      • 具体的状态实习那类ConcreteState
    • 细节

      • 状态模式允许一个对象基于内部状态而拥有不同的行为
      • 通过把每个状态封装进一个类, 我们把以后需要做的任何改变都局部化了 (改变这个状态下的行为)
      • 状态模式与策略模式有相同的类图 ,但是它们的意图不同
        • 策略模式通常会用行为或算法来配置Context类
        • 状态模式允许Context随着状态的改变而改变行为
      • 状态转换可以由State类(某个行为后改变状态)或者Context(外部设置状态setState)类控制.
      • 使用状态模式通常会导致设计中的类的数据大量增量(状态类)

核心代码部分

  • 上下文

    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;
          } 
      }
        //上下文提供改变状态的接口
        void setState(State state) {
          this.state = state;
      }
     
      public void insertQuarter() {
          state.insertQuarter();  //将行为委托给状态对象来处理
      }
     
      public void ejectQuarter() {
          state.ejectQuarter();
      }
     
      public void turnCrank() {
          state.turnCrank();
          state.dispense();
      }
     
      void releaseBall() {
          System.out.println("A gumball comes rolling out the slot...");
          if (count != 0) {
              count = count - 1;
          }
      }
     
      int getCount() {
          return count;
      }
     
      void refill(int count) {
          this.count += count;
          System.out.println("The gumball machine was just refilled; it's new count is: " + this.count);
          state.refill();
      }
    
        getter and setter...
    }
    
  • 状态接口

    public interface State {
     
      public void insertQuarter();
      public void ejectQuarter();
      public void turnCrank();
      public void dispense();
      
      public void refill();
    }
    
  • 具体的状态
public class WinnerState implements State {
    GumballMachine gumballMachine;
 
    public WinnerState(GumballMachine gumballMachine) {
        this.gumballMachine = gumballMachine;
    }
 
    public void insertQuarter() {
        System.out.println("Please wait, we're already giving you a Gumball");
    }
 
    public void ejectQuarter() {
        System.out.println("Please wait, we're already giving you a Gumball");
    }
 
    public void turnCrank() {
        System.out.println("Turning again doesn't get you another gumball!");
    }
 
    public void dispense() {
        gumballMachine.releaseBall();
        if (gumballMachine.getCount() == 0) {
            //通过上下文改变状态
            gumballMachine.setState(gumballMachine.getSoldOutState());
        } else {
            gumballMachine.releaseBall();
            System.out.println("YOU'RE A WINNER! You got two gumballs for your quarter");
            if (gumballMachine.getCount() > 0) {
                gumballMachine.setState(gumballMachine.getNoQuarterState());
            } else {
                System.out.println("Oops, out of gumballs!");
                gumballMachine.setState(gumballMachine.getSoldOutState());
            }
        }
    }
 
    public void refill() { }
    
    public String toString() {
        return "despensing two gumballs for your quarter, because YOU'RE A WINNER!";
    }
}
  • 主程序

    public class GumballMachineTestDrive {
    
      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);
      }
    }
    
  • 输出结果

    Mighty Gumball, Inc.
    Java-enabled Standing Gumball Model #2004
    Inventory: 10 gumballs
    Machine is waiting for quarter
    
    You inserted a quarter
    You turned...
    A gumball comes rolling out the slot...
    You inserted a quarter
    You turned...
    A gumball comes rolling out the slot...
    
    Mighty Gumball, Inc.
    Java-enabled Standing Gumball Model #2004
    Inventory: 8 gumballs
    Machine is waiting for quarter
    

参考

​ 书籍: HeadFirst设计模式

​ 代码参考地址: 我就是那个地址

你可能感兴趣的:(设计模式学习专栏十一--------状态模式)