<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

    主站蜘蛛池模板: 亚洲a∨无码精品色午夜| 亚洲一级黄色大片| 美女裸免费观看网站| 免费激情视频网站| 亚洲日韩AV一区二区三区四区| 精品久久8x国产免费观看| 亚洲熟妇无码久久精品| 97免费人妻无码视频| 日韩亚洲不卡在线视频中文字幕在线观看 | 鲁丝片一区二区三区免费| 亚洲色WWW成人永久网址| 久久国产精品免费一区| 亚洲香蕉成人AV网站在线观看| 国产免费AV片在线观看播放| 亚洲性猛交XXXX| 91精品国产免费久久国语蜜臀| 亚洲美女视频网址| 在线免费不卡视频| 日亚毛片免费乱码不卡一区 | 亚洲AV福利天堂一区二区三| 37pao成人国产永久免费视频| 亚洲天堂一区二区三区| 成人免费视频试看120秒| 另类专区另类专区亚洲| 久久久无码精品亚洲日韩蜜桃| 国产成人免费在线| 亚洲aⅴ无码专区在线观看春色 | 国产一区二区三区在线免费 | 中文字幕免费在线看线人动作大片 | 亚洲欧洲精品一区二区三区| 在线播放高清国语自产拍免费| 黄色免费网址在线观看| 亚洲精品成人片在线观看精品字幕| 一区二区在线免费观看| 亚洲色偷偷色噜噜狠狠99网| 亚洲五月午夜免费在线视频| 99免费观看视频| 性色av极品无码专区亚洲 | 成人午夜性A级毛片免费| AAAAA级少妇高潮大片免费看| 亚洲色图校园春色|