<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

    主站蜘蛛池模板: 成人影片麻豆国产影片免费观看 | 亚洲伊人成无码综合网 | 无码日韩人妻av一区免费| 中文字幕在线免费| 99热在线免费观看| 91精品免费不卡在线观看| 18禁美女裸体免费网站 | 一个人免费观看日本www视频| 羞羞漫画登录页面免费| 国产亚洲综合精品一区二区三区| 亚洲av综合日韩| 视频一区在线免费观看| 一区二区三区精品高清视频免费在线播放 | 青青草a免费线观a| 成人毛片18女人毛片免费视频未| 久久久久国色AV免费看图片| 毛片a级毛片免费观看免下载| 狠狠久久永久免费观看| 免费在线视频一区| 亚洲毛片av日韩av无码| 国产成人无码综合亚洲日韩| 久久久久亚洲AV片无码下载蜜桃| 亚洲视频小说图片| 亚洲砖码砖专无区2023| 免费亚洲视频在线观看| 久久毛片免费看一区二区三区| 鲁丝片一区二区三区免费| **一级一级毛片免费观看| 午夜免费福利影院| 亚洲一区精品伊人久久伊人 | 免费观看大片毛片| 亚洲人妻av伦理| 亚洲成a人片77777老司机| 亚洲一级大黄大色毛片| 风间由美在线亚洲一区| 国产一级淫片a免费播放口| 免费H网站在线观看的| 又爽又黄无遮挡高清免费视频 | 国产伦一区二区三区免费| 久久久无码精品亚洲日韩软件| 日韩亚洲AV无码一区二区不卡|