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

    主站蜘蛛池模板: 亚洲国产精品VA在线看黑人| 噼里啪啦电影在线观看免费高清 | 四虎永久精品免费观看| 亚洲影视一区二区| 免费精品无码AV片在线观看| 亚洲精品中文字幕乱码三区 | 最近2019中文免费字幕在线观看| 亚洲中文字幕无码一久久区| 两个人看的www免费高清 | 亚洲男人天堂影院| 91成人免费在线视频| 亚洲av无码电影网| 成人A级毛片免费观看AV网站| 亚洲欧美黑人猛交群| 好大好硬好爽免费视频| 无人视频免费观看免费视频| 亚洲一本大道无码av天堂| 三根一起会坏掉的好痛免费三级全黄的视频在线观看 | 最近免费中文字幕中文高清| 国产精品久久久亚洲| 2019中文字幕免费电影在线播放 | 亚洲国产电影av在线网址| 国产99视频精品免费视频76| 久久精品夜色国产亚洲av| 久久九九兔免费精品6| 亚洲日本中文字幕天天更新| 亚洲AV日韩精品一区二区三区| 91在线视频免费观看| 亚洲福利视频一区二区三区| 在线a毛片免费视频观看| 免费福利资源站在线视频| 精品亚洲综合在线第一区| 国产一卡2卡3卡4卡无卡免费视频| 男人的天堂av亚洲一区2区| 伊人久久大香线蕉亚洲| 成人福利免费视频| h视频在线观看免费| 亚洲国产成人超福利久久精品| 国产做床爱无遮挡免费视频| 免费无码一区二区三区| 国产精品亚洲专区无码唯爱网|