Chan Chen Coding...

          Eight: State Design Pattern

          State Design Pattern is mainly for changing state at run-time.

          The Java example below shows how State pattern works.

          The idea behind the example: people normally work harder when they are poor and play more when they are rich. What they do depends on the state in which they are.

          Here is the class diagram. You can compare this with strategy pattern to get better understanding of the difference.

          State classes.

          1
          2
          3
          4
          5
          6
          7
          8
          9
          10
          11
          12
          13
          14
          15
          16
          17
          18
          19
          20
          21
          package com.programcreek.designpatterns.state;
           
          interface State {
             public void saySomething(StateContext sc);
          }
           
          class Rich implements State{
             @Override
             public void saySomething(StateContext sc) {
                System.out.println("I'm rick currently, and play a lot.");
                sc.changeState(new Poor());
             }
          }
           
          class Poor implements State{
             @Override
             public void saySomething(StateContext sc) {
                System.out.println("I'm poor currently, and spend much time working.");
                sc.changeState(new Rich());
             }
          }

          StateContext class

          1
          2
          3
          4
          5
          6
          7
          8
          9
          10
          11
          12
          13
          14
          15
          16
          17
          package com.programcreek.designpatterns.state;
           
          public class StateContext {
             private State currentState;
              
             public StateContext(){
                currentState = new Poor();
             }
              
             public void changeState(State newState){
                this.currentState = newState;
             }
              
             public void saySomething(){
                this.currentState.saySomething(this);
             }
          }

          Main class for testing

          1
          2
          3
          4
          5
          6
          7
          8
          9
          10
          11
          import com.programcreek.designpatterns.*;
           
          public class Main {
             public static void main(String args[]){
                StateContext sc = new StateContext();
                sc.saySomething();
                sc.saySomething();
                sc.saySomething();
                sc.saySomething();
             }
          }

          Result:

          1
          2
          3
          4
          I'm poor currently, and spend much time working.
          I'm rick currently, and play a lot.
          I'm poor currently, and spend much time working.
          I'm rick currently, and play a lot.


          -----------------------------------------------------
          Silence, the way to avoid many problems;
          Smile, the way to solve many problems;

          posted on 2012-11-02 14:56 Chan Chen 閱讀(318) 評論(0)  編輯  收藏 所屬分類: Design Pattern

          主站蜘蛛池模板: 绍兴市| 平远县| 祁东县| 喀什市| 思南县| 梅河口市| 颍上县| 腾冲县| 延庆县| 芦溪县| 绥阳县| 曲靖市| 姚安县| 宝鸡市| 剑河县| 义马市| 嘉祥县| 上高县| 永春县| 搜索| 岗巴县| 孟津县| 宝清县| 镇坪县| 光山县| 溧水县| 玛曲县| 兴国县| 会泽县| 三都| 嵊泗县| 长顺县| 育儿| 仙游县| 青川县| 南皮县| 稷山县| 阜南县| 通化县| 虹口区| 济南市|