我原本以為在對鄉村的一切事物都很容易,但是每次我一回頭就有更多變更的請求紛至沓來。我快崩潰了! 當小編讀到這段話的時候,甚是感慨,設計模式的世界真的不是那麼容易弄懂的,好在我還在學習。 基本常識:策略模式和狀態模式是雙胞胎,在出生時才分開。 策略模式是圍繞可以互換的演算法來創建成功業務的。然而,狀態走 ...
我原本以為在對鄉村的一切事物都很容易,但是每次我一回頭就有更多變更的請求紛至沓來。我快崩潰了!----當小編讀到這段話的時候,甚是感慨,設計模式的世界真的不是那麼容易弄懂的,好在我還在學習。
基本常識:策略模式和狀態模式是雙胞胎,在出生時才分開。策略模式是圍繞可以互換的演算法來創建成功業務的。然而,狀態走的是更崇高的路,它通過改變對象內部的狀態來幫助對象控制自己的行為。
我們先來看下對象村的大家,這次又想出了什麼。他們發現只要在糖果機里裝上CPU,就可以增加銷量、通過網路監測庫存,並且能準確地得知客戶的滿意度。所有,就有了下麵的這個設想:
狀態機101
我們如何從狀態圖得到真正的代碼呢?下麵是一個實現狀態機的簡單介紹
- 首先,找出所有的狀態:
我們有:沒有25分錢、有25分錢、糖果售罄、售出糖果四個狀態
- 接下來,創建一個實例變數來持有目前的狀態,然後定義每個狀態的值:
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分錢、轉動曲柄、發放糖果
- 現在,我們創建一個類,它的作用就像是一個狀態機。對每一個動作,我們都創建了一個對應的方法,這些方法利用條件語句來決定在每個狀態內什麼行為是恰當的。比如對“投入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();
}
}
現在,讓我們進入一個小小的內部測試,看下是否符合了要求。
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%的幾率掉下來的是兩顆糖果(多送你一顆),那你應該怎麼做呢?
我們的計劃是這樣的:不要維護我們現有的代碼,我們重寫它以便於將狀態對象封裝在各自的類中,然後在動作發生時委托給當前狀態。所以,我們要做的事情就是:
- 首先,我們定義一個State介面。在這個介面中,糖果機的每個動作都有一個對應的方法
- 然後為機器中的每個狀態實現狀態類。這些將負責在對應的狀態下進行機器的行為
- 最後,我們要擺脫舊的條件代碼,取而代之的方式是,將動作委托到狀態類
你將會看到,我們不僅遵守了設計原則,實際上我們還實現了狀態模式。接下來,我們定義狀態介面和類,如下:
好了,到現在為止,我們下一步肯定是要去實現怎麼改造了,對吧。在這裡呢,給大家先留著,先去思考思考,如何進行改造。思路有了,類圖有了,就看你怎麼想啦。我們下一次揭曉。