<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    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 閱讀(321) 評論(0)  編輯  收藏 所屬分類: Design Pattern

    主站蜘蛛池模板: 亚洲av无码偷拍在线观看| 久久久久久a亚洲欧洲aⅴ| 亚洲无成人网77777| 国内少妇偷人精品视频免费| 亚洲精品国产自在久久| 国产精品手机在线亚洲| 国产网站免费观看| 美女视频黄a视频全免费网站色 | 99视频免费在线观看| 亚洲国产婷婷六月丁香| 黄网站免费在线观看| 午夜影视日本亚洲欧洲精品一区| 美女被cao网站免费看在线看| 亚洲男人的天堂在线播放| 中文字幕免费视频一| 成人免费大片免费观看网站| 亚洲欧洲日产国码在线观看| 在线视频免费观看爽爽爽| 国产亚洲玖玖玖在线观看| 免费萌白酱国产一区二区| 国产精品内射视频免费| 亚洲免费精彩视频在线观看| 又黄又爽又成人免费视频| 亚洲国产成人精品无码区二本 | 免费a级毛片18以上观看精品| 欧亚一级毛片免费看| 亚洲va无码手机在线电影| 无码日韩人妻av一区免费| 美女被免费网站在线视频免费| 亚洲香蕉成人AV网站在线观看| 亚洲一区二区三区免费视频| 永久免费看bbb| 你懂的在线免费观看| 亚洲黄色在线电影| 国产免费拔擦拔擦8x| 免费91麻豆精品国产自产在线观看 | 久久午夜伦鲁片免费无码| 四虎影永久在线高清免费| 中文字幕免费在线看线人动作大片 | 亚洲国产综合91精品麻豆| 免费黄色app网站|