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

    主站蜘蛛池模板: 免费人成视频在线播放| 国外亚洲成AV人片在线观看| 亚洲欧美日韩自偷自拍| 中文字幕av无码无卡免费| 亚洲精品mv在线观看| 19禁啪啪无遮挡免费网站| 亚洲精品自在线拍| 亚洲电影在线免费观看| 亚洲欧洲综合在线| 99久久99这里只有免费费精品| 亚洲乱码无限2021芒果| 国产va免费精品观看精品| 亚洲中文无码亚洲人成影院| 免费高清在线影片一区| 亚洲AV成人精品日韩一区| 深夜国产福利99亚洲视频| 久香草视频在线观看免费| 国产综合精品久久亚洲| 久久免费观看国产精品88av| 亚洲视频免费观看| 日韩精品福利片午夜免费观着| 亚洲色偷偷综合亚洲AV伊人蜜桃| 国产美女被遭强高潮免费网站| 羞羞的视频在线免费观看| 国产性爱在线观看亚洲黄色一级片 | 午夜不卡久久精品无码免费| 久久亚洲熟女cc98cm| 2021久久精品免费观看| 亚洲美国产亚洲AV| 亚洲一区精品伊人久久伊人| 91精品成人免费国产| 久久久久亚洲av无码专区喷水 | 亚洲午夜久久久影院| 久久黄色免费网站| 亚洲另类图片另类电影| 国产精品成人无码免费| www一区二区www免费| 久久青青草原亚洲av无码app| 精品久久久久国产免费| 黄色三级三级三级免费看| 亚洲精品国产精品乱码在线观看|