state patten 读书笔记

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

1。状态模式允许一个对象基于内部状态而拥有不同的行为。

2。和程序状态机(PSM)不同,状态模式用类代表状态。

3。Context会将行为委托给当前状态对象。

4。通过每个状态封装进一个类,我们把以后需要做的任何改变局部化。

5。状态模式和策略模式有相同的类图,但是他们的意图不同。

6。策略模式通常会用行为或算法来配置Context类。

7。状态模式允许Context随着状态的改变而改变行为。

8。状态转换可以由State类或者Context类控制。

9。使用状态模式通常会导致设计中类的数目大量增加。

10。状态类可以被多个Context实例共享。

package pattern;

import java.util.Random;

public class GumBallMachineTestDrive {
 public static void main(String[] args) {
  GumballMachine gumballMachine = new GumballMachine(5);
  System.out.println(gumballMachine);

  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();
  gumballMachine.ejectQuarter();

  System.out.println(gumballMachine);

  gumballMachine.insertQuarter();
  gumballMachine.insertQuarter();
  gumballMachine.turnCrank();
  gumballMachine.insertQuarter();
  gumballMachine.turnCrank();
  gumballMachine.insertQuarter();
  gumballMachine.turnCrank();
  gumballMachine.insertQuarter();
  gumballMachine.insertQuarter();
  gumballMachine.turnCrank();
  gumballMachine.insertQuarter();
  gumballMachine.turnCrank();
  gumballMachine.insertQuarter();
  gumballMachine.turnCrank();
  System.out.println(gumballMachine);

 }
}

interface State {
 public void insertQuarter();

 public void ejectQuarter();

 public void turnCrank();

 public void dispense();
 public void refill(int count);
}

class NoQuarterState implements State {
 GumballMachine gumballMachine;

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

 public void insertQuarter() {
  System.out.println("You inserted a quarter");
  gumballMachine.setState(this.gumballMachine.getHasQuarterState());
 }

 public void ejectQuarter() {
  System.out.println("You haven't inserted a quarter");
 }

 public void turnCrank() {
  System.out.println("You need to pay first");
 }

 public void dispense() {
  System.out.println("You need to pay first");
 }
 
 public void refill(int count) {
  System.out.println("need't to refill first");
 }
}

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("You can't insert another quarter");
 }

 public void ejectQuarter() {
  System.out.println("Quarter returned");
  this.gumballMachine.setState(this.gumballMachine.getNoQuarterState());
 }

 public void turnCrank() {
  System.out.println("You turned...");
  int winner = randomWinner.nextInt(3);

  if ((winner == 0) && this.gumballMachine.getCount() > 1) {
   System.out.println("winner=" + winner + "    count="
     + this.gumballMachine.getCount());
   this.gumballMachine.setState(this.gumballMachine.getWinnerState());
  } else {
   this.gumballMachine.setState(this.gumballMachine.getSoldState());
  }
 }

 public void dispense() {
  System.out.println("No gumball dispensed");
 }
 public void refill(int count) {
  System.out.println("need't to refill first");
 }
}

class SoldOutState implements State {
 GumballMachine gumballMachine;

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

 public void insertQuarter() {
  refill(5);
 }

 public void ejectQuarter() {
  System.out.println("There are no a quarter");
 }

 public void turnCrank() {
  refill(5);
 }

 public void dispense() {
  refill(5);
 }
 public void refill(int count) {
  System.out.println("refill 5 count");
  this.gumballMachine.setCount(count);
  this.gumballMachine.setState(this.gumballMachine.getHasQuarterState());
 }
}

class SoldState implements State {
 GumballMachine gumballMachine;

 public SoldState(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("Sorry ,you already turned the crank");
 }

 public void turnCrank() {
  System.out.println("Turning twice doesn't get you another gumball!");
 }

 public void dispense() {
  this.gumballMachine.releaseBall();
  if (this.gumballMachine.getCount() > 0) {
   this.gumballMachine.setState(this.gumballMachine
     .getNoQuarterState());
  } else {
   System.out.println("Oops, out of gumballs");
   this.gumballMachine.setState(this.gumballMachine.getSoldOutState());
  }
 }
 public void refill(int count) {
  System.out.println("need't to refill first");
 }
}

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("Sorry ,you already turned the crank");
 }

 public void turnCrank() {
  System.out.println("Turning twice doesn't get you another gumball!");
 }

 public void dispense() {
  System.out
    .println("YOU'RE A WINNER! You get two gumballs for your quarter");
  this.gumballMachine.releaseBall();
  if (this.gumballMachine.getCount() == 0) {
   this.gumballMachine.setState(this.gumballMachine.getSoldOutState());
  } else {
   this.gumballMachine.releaseBall();
   if (this.gumballMachine.getCount() > 0) {
    this.gumballMachine.setState(this.gumballMachine
      .getNoQuarterState());

   } else {
    System.out.println("Oops, out of gumballs");
    this.gumballMachine.setState(this.gumballMachine
      .getSoldOutState());
   }
  }
 }
 public void refill(int count) {
  System.out.println("need't to refill first");
 }
}

class GumballMachine {
 State soldOutState;
 State noQuarterState;
 State hasQuarterState;
 State soldState;
 State state;
 State winnerState;
 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();
 }

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

 public String toString() {
  System.out.println(this.getState());

  return this.getState().toString();
 }

 void releaseBall() {
  System.out.println("A gumball come rolling out the slot ...");
  if (count != 0) {
   count = count - 1;
  }
 }

 public State getSoldOutState() {
  return soldOutState;
 }

 public void setSoldOutState(State soldOutState) {
  this.soldOutState = soldOutState;
 }

 public State getNoQuarterState() {
  return noQuarterState;
 }

 public void setNoQuarterState(State noQuarterState) {
  this.noQuarterState = noQuarterState;
 }

 public State getHasQuarterState() {
  return hasQuarterState;
 }

 public void setHasQuarterState(State hasQuarterState) {
  this.hasQuarterState = hasQuarterState;
 }

 public State getSoldState() {
  return soldState;
 }

 public void setSoldState(State soldState) {
  this.soldState = soldState;
 }

 public int getCount() {
  return count;
 }

 public void setCount(int count) {
  this.count = count;
 }

 public State getState() {
  return state;
 }

 public State getWinnerState() {
  return winnerState;
 }

public void setWinnerState(State winnerState) {
  this.winnerState = winnerState;
 }
}

你可能感兴趣的:(设计模式,算法,读书)