我本來覺得在對鄉村的一切事物都很容易,可是每次我一回頭就有更多變動的請求紛至沓來。我快崩潰了!----當小編讀到這段話的時候,甚是感慨,設計模式的世界真的不是那麼容易弄懂的,好在我還在學習。java
基本常識:策略模式和狀態模式是雙胞胎,在出生時才分開。策略模式是圍繞能夠互換的算法來建立成功業務的。然而,狀態走的是更崇高的路,它經過改變對象內部的狀態來幫助對象控制本身的行爲。算法
咱們先來看下對象村的你們,此次又想出了什麼。他們發現只要在糖果機裏裝上CPU,就能夠增長銷量、經過網絡監測庫存,而且能準確地得知客戶的滿意度。全部,就有了下面的這個設想:設計模式
咱們如何從狀態圖獲得真正的代碼呢?下面是一個實現狀態機的簡單介紹網絡
咱們有:沒有25分錢、有25分錢、糖果售罄、售出糖果四個狀態app
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;
投入25分錢、退回25分錢、轉動曲柄、發放糖果學習
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 old out"); } else if (state == SOLD) { System.out.println("Please wait, we're already giving you a gumball"); } }
如今咱們來實現糖果機。咱們知道要利用實例變量持有當前的狀態,而後須要處理全部可能發生的動做、行爲和狀態的轉換。咱們須要實現的動過包括:投入25分錢、退回25分錢、轉動曲柄和發放糖果;也要檢查糖果是否售罄。測試
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; } } // 當有25分錢投進來,就會執行這裏 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"); } } // 如今,若是顧客試着退回25分錢 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"); } } // 顧客試着轉動曲柄 public void turnCrank() { if (state == SOLD) { System.out.println("Turning twice doesn't get you another gumball!"); } else if (state == NO_QUARTER) { System.out.println("You turned but there's no quarter"); } else if (state == SOLD_OUT) { System.out.println("You turned, but there are no gumballs"); } else if (state == HAS_QUARTER) { System.out.println("You turned..."); state = SOLD; dispense(); } } // 調用此方法,發放糖果 private void dispense() { if (state == SOLD) { System.out.println("A gumball comes rolling out the slot"); count = count - 1; if (count == 0) { System.out.println("Oops, out of gumballs!"); state = SOLD_OUT; } else { state = NO_QUARTER; } } else if (state == NO_QUARTER) { System.out.println("You need to pay first"); } else if (state == SOLD_OUT) { System.out.println("No gumball dispensed"); } else if (state == HAS_QUARTER) { System.out.println("No gumball dispensed"); } } public void refill(int numGumBalls) { this.count = numGumBalls; state = NO_QUARTER; } public String toString() { StringBuffer result = new StringBuffer(); result.append("\nMighty Gumball, Inc."); result.append("\nJava-enabled Standing Gumball Model #2004\n"); result.append("Inventory: " + count + " gumball"); if (count != 1) { result.append("s"); } result.append("\nMachine is "); if (state == SOLD_OUT) { result.append("sold out"); } else if (state == NO_QUARTER) { result.append("waiting for quarter"); } else if (state == HAS_QUARTER) { result.append("waiting for turn of crank"); } else if (state == SOLD) { result.append("delivering a gumball"); } result.append("\n"); return result.toString(); } }
如今,讓咱們進入一個小小的內部測試,看下是否符合了要求。this
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!"; } }
運行結果比較長,在這裏就不進行打印啦,須要的朋友請自行根據代碼進行執行。設計
如今,客戶要求當曲柄轉動時,有10%的概率掉下來的是兩顆糖果(多送你一顆),那你應該怎麼作呢?code
咱們的計劃是這樣的:不要維護咱們現有的代碼,咱們重寫它以便於將狀態對象封裝在各自的類中,而後在動做發生時委託給當前狀態。因此,咱們要作的事情就是:
你將會看到,咱們不只遵照了設計原則,實際上咱們還實現了狀態模式。接下來,咱們定義狀態接口和類,以下:
好了,到如今爲止,咱們下一步確定是要去實現怎麼改造了,對吧。在這裏呢,給你們先留着,先去思考思考,如何進行改造。思路有了,類圖有了,就看你怎麼想啦。咱們下一次揭曉。