想想你上班的情況, 上班狀態好, 中午想睡覺, 下午漸漸進入狀況, 如果工作完成便可以準時下班, 否則就只能加班煎熬. 而這其實是一種狀態的變化, 不同的時間會有不同的狀態. 接著我們將用代碼來模擬真實人生, 藉此來介紹有名的狀態模式 (State Design Pattern).
工作模式 - 函數版 :
第一版程式簡單的用 if/else 來判斷時間做出不同狀態的反應, 範例代碼如下 :
- WorkStatus.java :
- package chk16.v1;
- public class WorkStatus {
- static int Hour = 0;
- static boolean WorkFinished = false; // 任務完成標記
- public static void WriteProgram(){
- if(Hour < 12) {
- System.out.printf("當前時間: {%d} 上午工作, 精神百倍! ^^\n", Hour);
- }
- else if(Hour < 13)
- {
- System.out.printf("當前時間: {%d} 餓了, 午飯; 覺得睏, 午休... ><\n", Hour);
- }
- else if(Hour < 17)
- {
- System.out.printf("當前時間: {%d} 下午狀態不錯, 繼續努力! ╰_╯\n", Hour);
- }
- else
- {
- if(WorkFinished)
- {
- System.out.printf("當前時間: {%d} 下班回家. ^^\n", Hour);
- }
- else
- {
- if(Hour < 21)
- {
- System.out.printf("當前時間: {%d} 加班喔, 好想睡. //(ㄒoㄒ)// \n", Hour);
- }
- else
- {
- System.out.printf("當前時間: {%d} 不行了, 快睡著了zzz x__x \n", Hour);
- }
- }
- }
- }
- public static void main(String args[]) {
- Hour = 9;
- WriteProgram();
- Hour = 10;
- WriteProgram();
- Hour = 12;
- WriteProgram();
- Hour = 13;
- WriteProgram();
- Hour = 14;
- WriteProgram();
- Hour = 17;
- WriteProgram();
- WorkFinished = false;
- Hour = 19;
- WriteProgram();
- Hour = 22;
- WriteProgram();
- }
- }
接著觀察執行結果如預期, 但是我們寫的是 OO 耶, 這樣傳統程序導向的程式碼是會被笑掉大牙的, 所以接著我們封裝了時間與工作狀態屬性到 Work 類別...
工作狀態 - OO 導向版 :
這裡用 OO 封裝了時間與工作狀態到類別 Work :
- Work.java : 第二版真實人生
- package chk16.v2;
- public class Work {
- private int hour;
- private boolean isFinished = false;
- public void WriteProgram()
- {
- if(hour < 12) {
- System.out.printf("當前時間: {%d} 上午工作, 精神百倍! ^^\n", hour);
- }
- else if(hour < 13)
- {
- System.out.printf("當前時間: {%d} 餓了, 午飯; 覺得睏, 午休... ><\n", hour);
- }
- else if(hour < 17)
- {
- System.out.printf("當前時間: {%d} 下午狀態不錯, 繼續努力! ╰_╯\n", hour);
- }
- else
- {
- if(isFinished)
- {
- System.out.printf("當前時間: {%d} 下班回家. ^^\n", hour);
- }
- else
- {
- if(hour < 21)
- {
- System.out.printf("當前時間: {%d} 加班喔, 好想睡. //(ㄒoㄒ)// \n", hour);
- }
- else
- {
- System.out.printf("當前時間: {%d} 不行了, 快睡著了zzz x__x \n", hour);
- }
- }
- }
- }
- public int getHour() {
- return hour;
- }
- public void setHour(int hour) {
- this.hour = hour;
- WriteProgram();
- }
- public boolean isFinished() {
- return isFinished;
- }
- public void setFinished(boolean isFinished) {
- this.isFinished = isFinished;
- }
- public static void main(String args[]) {
- Work emergencyProject = new Work();
- emergencyProject.setHour(9);
- emergencyProject.setHour(10);
- emergencyProject.setHour(12);
- emergencyProject.setHour(13);
- emergencyProject.setHour(14);
- emergencyProject.setHour(17);
- emergencyProject.setHour(19);
- emergencyProject.setHour(22);
- }
- }
這裡我們觀察到方法 WriteProgram() 仍然存在的太多的 if/else. 考慮 物件導向設計其實是希望做到程式碼的責任分解. 因而 Work 類別違背了 "單一職責原則 (SRP)" 與 "開放-封閉原則 (OCP)", 幸運的是前人(GoF)已經為我們針對這類問題提供了解決方案, 那就是 "狀態模式".
狀態模式 :
我們先來看看狀態模式的定義 :
The State Pattern allows an object to alter its behavior when its internal state changes. The object will appear to change its class.
狀態模式主要解決的是當控制一個物件狀態轉換的條件運算式過於複雜時的情況. 把狀態的判斷邏輯轉移到表示不同狀態的一系列類別當中, 就可以把複雜的判斷邏輯簡化 . 當然如果狀態判斷很簡單, 那就沒必要用 "狀態模式" 了. 底下是其 UML 圖 :
狀態模式的好處是將與特定狀態相關的行為局部化, 並且將不同狀態的行為分割開來. 也就是將所有與狀態相關的程式碼都存在於某個 ConcreteState 中, 所已透過定義新的子類別就可以很容易的增加新的狀態和轉換. 說白了就是為了消除龐大的條件分支敘述. 因此狀態模式透過把各種狀態轉移邏輯到 IState 的 Concrete 類別之間, 來減少相互間的依賴.
工作狀態 - 採用 State Pattern :
接著我們來看看採用了 State Pattern 的版本 :
- IState.java :
- package chk16.v4;
- public interface IState {
- public void WriteProgram(Work w);
- }
- AfternoonState.java :
- package chk16.v4.states;
- import chk16.v4.IState;
- import chk16.v4.Work;
- public class AfternoonState implements IState{
- @Override
- public void WriteProgram(Work w) {
- if(w.getHour()<17)
- {
- System.out.printf("當前時間: {%d} 下午狀態不錯, 繼續努力! ╰_╯\n", w.getHour());
- }
- else
- {
- // 超過 17 點, 則轉入傍晚狀態.
- w.setState(new EveningState()); w.WriteProgram();
- }
- }
- }
- EveningState.java :
- package chk16.v4.states;
- import chk16.v4.IState;
- import chk16.v4.Work;
- public class EveningState implements IState {
- @Override
- public void WriteProgram(Work w) {
- if(w.isFinished())
- {
- // 完成任務, 則轉入下班狀態
- w.setState(new RestState()); w.WriteProgram();
- }
- else
- {
- if(w.getHour()<21)
- {
- System.out.printf("當前時間: {%d} 加班喔, 好想睡. //(ㄒoㄒ)// \n", w.getHour());
- }
- else
- {
- // 超過 21 點, 則進入睡眠工作狀態.
- w.setState(new SleepingState()); w.WriteProgram();
- }
- }
- }
- }
- MorningState.java :
- package chk16.v4.states;
- import chk16.v4.IState;
- import chk16.v4.Work;
- public class MorningState implements IState{
- @Override
- public void WriteProgram(Work w) {
- if(w.getHour()<12)
- {
- System.out.printf("當前時間: {%d} 上午工作, 精神百倍! ^^\n", w.getHour());
- }
- else
- {
- // 超過 12 點, 則轉入中午工作狀態
- w.setState(new NoonState()); w.WriteProgram();
- }
- }
- }
- NoonState.java :
- package chk16.v4.states;
- import chk16.v4.IState;
- import chk16.v4.Work;
- public class NoonState implements IState{
- @Override
- public void WriteProgram(Work w) {
- if(w.getHour()<13)
- {
- System.out.printf("當前時間: {%d} 餓了, 午飯; 覺得睏, 午休... ><\n", w.getHour());
- }
- else
- {
- // 超過 13 點, 則進入下午工作模式.
- w.setState(new AfternoonState()); w.WriteProgram();
- }
- }
- }
- RestState.java :
- package chk16.v4.states;
- import chk16.v4.IState;
- import chk16.v4.Work;
- public class RestState implements IState{
- @Override
- public void WriteProgram(Work w) {
- System.out.printf("當前時間: {%d} 下班回家. ^^\n", w.getHour());
- }
- }
- SleepingState.java :
- package chk16.v4.states;
- import chk16.v4.IState;
- import chk16.v4.Work;
- public class SleepingState implements IState {
- @Override
- public void WriteProgram(Work w) {
- if(w.isFinished())
- {
- // 完成工作就下班
- w.setState(new RestState()); w.WriteProgram();
- }
- else
- {
- System.out.printf("當前時間: {%d} 不行了, 快睡著了zzz x__x \n", w.getHour());
- }
- }
- }
- Work.java : State Pattern UML 裡的 Context
- package chk16.v4;
- import chk16.v4.states.MorningState;
- public class Work {
- private int hour;
- private boolean isFinished = false;
- private IState current;
- public Work(){current = new MorningState();}
- public void WriteProgram(){WriteProgram(hour);}
- public void WriteProgram(int h) {
- this.hour = h;
- current.WriteProgram(this);
- }
- public int getHour() {
- return hour;
- }
- public void setHour(int hour) {
- this.hour = hour;
- }
- public boolean isFinished() {
- return isFinished;
- }
- public void setFinished(boolean isFinished) {
- this.isFinished = isFinished;
- }
- public IState getState() {
- return current;
- }
- public void setState(IState current) {
- this.current = current;
- }
- public static void main(String args[]) {
- Work emgyProjects = new Work();
- emgyProjects.WriteProgram(9);
- emgyProjects.WriteProgram(10);
- emgyProjects.WriteProgram(12);
- emgyProjects.WriteProgram(13);
- emgyProjects.WriteProgram(14);
- emgyProjects.WriteProgram(17);
- emgyProjects.WriteProgram(19);
- emgyProjects.WriteProgram(22);
- }
- }
沒有留言:
張貼留言