行为类模式(八):状态(State)
生活随笔
收集整理的這篇文章主要介紹了
行为类模式(八):状态(State)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
定義
當一個對象的內在狀態改變時允許改變其行為,這個對象看起來像是改變了其類。
狀態模式主要解決的是當控制一個對象狀態的條件表達式過于復雜時的情況。把狀態的判斷邏輯轉移到表示不同狀態的一系列類中,可以把復雜的判斷邏輯簡化。
UML
優點
缺點
應用場景
示例
實現一個電梯運行的程序,電梯的幾個狀態之前可以相互切換,但是狀態的改變有前置條件,比如電梯的開門狀態下是不能上下跑的。
Java
1 public class Main 2 { 3 public static void main(String[] args) 4 { 5 Context context = new Context(); 6 context.setLiftState(new ClosingState()); 7 context.open(); 8 context.close(); 9 context.run(); 10 context.stop(); 11 12 context.open(); 13 context.run(); 14 } 15 16 public static class Context 17 { 18 // 定義出所有的電梯狀態 19 public final static OpenningState openningState = new OpenningState(); 20 public final static ClosingState closingState = new ClosingState(); 21 public final static RunningState runningState = new RunningState(); 22 public final static StoppingState stoppingState = new StoppingState(); 23 24 // 定一個當前電梯狀態 25 private LiftState liftState; 26 27 public LiftState getLiftState() 28 { 29 return liftState; 30 } 31 32 public void setLiftState(LiftState liftState) 33 { 34 this.liftState = liftState; 35 // 把當前的環境通知到各個實現類中 36 this.liftState.setContext(this); 37 } 38 39 public void open() 40 { 41 this.liftState.open(); 42 } 43 44 public void close() 45 { 46 this.liftState.close(); 47 } 48 49 public void run() 50 { 51 this.liftState.run(); 52 } 53 54 public void stop() 55 { 56 this.liftState.stop(); 57 } 58 } 59 60 /** 61 * 定義一個電梯的接口 62 */ 63 public static abstract class LiftState 64 { 65 // 定義一個環境角色,也就是封裝狀態的變換引起的功能變化 66 protected Context context; 67 68 public void setContext(Context _context) 69 { 70 this.context = _context; 71 } 72 73 // 首先電梯門開啟動作 74 public abstract void open(); 75 76 // 電梯門有開啟,那當然也就有關閉了 77 public abstract void close(); 78 79 // 電梯要能上能下,跑起來 80 public abstract void run(); 81 82 // 電梯還要能停下來,停不下來那就扯淡了 83 public abstract void stop(); 84 } 85 86 /** 87 * 在電梯門開啟的狀態下能做什么事情 88 */ 89 public static class OpenningState extends LiftState 90 { 91 @Override 92 public void close() 93 { 94 // 狀態修改 95 super.context.setLiftState(Context.closingState); 96 // 動作委托為CloseState來執行 97 super.context.getLiftState().close(); 98 } 99 100 @Override 101 public void open() 102 { 103 System.out.println("電梯門開啟..."); 104 } 105 106 @Override 107 public void run() 108 { 109 System.out.println("電梯門開啟時不能上下跑!"); 110 } 111 112 @Override 113 public void stop() 114 { 115 // do nothing; 116 } 117 } 118 119 /** 120 * 電梯門關閉以后,電梯可以做哪些事情 121 */ 122 public static class ClosingState extends LiftState 123 { 124 @Override 125 public void close() 126 { 127 System.out.println("電梯門關閉..."); 128 } 129 130 @Override 131 public void open() 132 { 133 super.context.setLiftState(Context.openningState); // 置為門敞狀態 134 super.context.getLiftState().open(); 135 } 136 137 @Override 138 public void run() 139 { 140 super.context.setLiftState(Context.runningState); // 設置為運行狀態; 141 super.context.getLiftState().run(); 142 } 143 144 @Override 145 public void stop() 146 { 147 super.context.setLiftState(Context.stoppingState); // 設置為停止狀態; 148 super.context.getLiftState().stop(); 149 } 150 } 151 152 /** 153 * 電梯在運行狀態下能做哪些動作 154 */ 155 public static class RunningState extends LiftState 156 { 157 @Override 158 public void close() 159 { 160 // do nothing 161 } 162 163 @Override 164 public void open() 165 { 166 // do nothing 167 } 168 169 @Override 170 public void run() 171 { 172 System.out.println("電梯上下跑..."); 173 } 174 175 @Override 176 public void stop() 177 { 178 super.context.setLiftState(Context.stoppingState); 179 super.context.getLiftState().stop(); 180 } 181 } 182 183 /** 184 * 在停止狀態下能做什么事情 185 */ 186 public static class StoppingState extends LiftState 187 { 188 @Override 189 public void close() 190 { 191 // do nothing; 192 } 193 194 @Override 195 public void open() 196 { 197 super.context.setLiftState(Context.openningState); 198 super.context.getLiftState().open(); 199 } 200 201 @Override 202 public void run() 203 { 204 super.context.setLiftState(Context.runningState); 205 super.context.getLiftState().run(); 206 } 207 208 @Override 209 public void stop() 210 { 211 System.out.println("電梯停止了..."); 212 } 213 } 214 } View Code?
轉載于:https://www.cnblogs.com/hammerc/p/4743814.html
總結
以上是生活随笔為你收集整理的行为类模式(八):状态(State)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JavaScript多浏览器兼容问题
- 下一篇: [JZOJ P1291] [DP]添加括