有限状态机(FSM)的Java 演示

版权声明:本文为博主原创文章,未经博主允许不得转载。

目录(?)[+]

本文从简单的例子入手,逐步演变成非常复杂的程序。

简明 状态模式(5.8)中,状态之间的变换由外界控制,或者说,多种状态是分割的、无关的。状态模式最有趣的地方正是讨论其状态的变迁。

1.引子

空调(air-condition)的遥控器有两个按钮(更多的按钮奋斗在后面的例子中引入),power/电源键和cool/制冷键。空调的运行呈现3个状态,停止/Off、仅送风/FanOnly、制冷/Cool。起始状态为Off,状态变化图如下所示。

这是简化的有限状态机(Finite State Machine、FSM或者Finite State Automata)图形,使用了状态图的3个元素:①气泡,表示状态(state);②连接状态的箭头表示转换(transition);③箭头上的标记前者为事件(event)。

状态的转换,看图说话。按power键,则Off→FanOnly、Cool→Off等;按cool,则Off→Off (没有画出来,喜欢全面一点就自己画吧)。

对于这种简单的状态的转换,yqj2065还是喜欢分支语句微笑,简洁明快。

 

[java] view plain copy

扫描二维码关注公众号,回复: 9459320 查看本文章
  1. 例程 4-5 简洁明快  
  2. package property.state.stateMachine;  
  3. import static tool.Print.*;//pln  
  4. /** 
  5.  * 空调Aircon。简单的模型: 
  6.  * 遥控器有两个按钮(更多的按钮在下面的例子中引入),power电源键和cool制冷键。 
  7.  * 空调的运行呈现3个状态,停止/Off、仅送风/FanOnly、制冷/Cool。 
  8.  * 起始状态为Off 
  9.  * @author (yqj2065)  
  10.  * @version 0.1 
  11.  */  
  12. public class Aircon0{  
  13.     // off,FanOnly,AC  
  14.     private int state=0;//起始状态为Off  
  15.     public int getState(){return state;}  
  16.     //两个Action  
  17.     public void power(){//按power键  
  18.         if(state==0){//off  
  19.             state=1;  
  20.             pln("start Fan");   
  21.         }else if(state==1){//FanOnly  
  22.             state=0;  
  23.             pln("stop Fan");   
  24.         }else{  
  25.             state=0;  
  26.             pln("stop Cool");   
  27.         }  
  28.     }  
  29.   
  30.     public void cool(){//按制冷键  
  31.         if(state==0){//off  
  32.             pln("nothing");  
  33.         }else if(state==1){//FanOnly  
  34.             state=2;  
  35.             pln("start Cool");   
  36.         }else{  
  37.             state=1;  
  38.             pln("stop Cool");   
  39.         }  
  40.     }  
  41. }  
  42. package property.state.stateMachine;  
  43. public class ACCtrl{  
  44.     public static void test(){  
  45.         Aircon0 ac = new Aircon0();//power() cool()  
  46.         System.out.println("Current State:" + ac.getState());  
  47.         ac.cool();  
  48.         ac.power();  
  49.         ac.cool();  
  50.         ac.cool();  
  51.         ac.power();  
  52.         ac.power();  
  53.     }  
  54. }  

测试代码的输出:

 

Current State:0
nothing
start Fan
start Cool
stop Cool
stop Fan
start Fan

在此基础上,可以花10分钟练习一下,采用状态模式修改上述代码。我们使用enum编写状态类层次。其结构如下:

 

[java] view plain copy

  1. 例程 4 6 enum State  
  2.   
  3. enum State0{  
  4.     OFF{  
  5.         @Override void power(){              
  6.         }  
  7.         @Override void power(){  
  8.         }  
  9.     },FANONLY{  
  10.     },  
  11.     COOL{ };  
  12.     public abstract void power();  
  13.     public abstract void cool();  
  14. }  

(本来是应该将State1作为Aircon1的内部类的。放在外边,power()等需要添加参数Aircon1,变为power(Aircon1 ac)).

现在,丰富有限状态机的细节,增添④动作(action),如事件(event)相应的动作和状态的动作。

为此,在enum State1中,除了状态模式 提取的接口外,添加了状态机的各种动作/action methode
    void entry(Aircon1 ac){pln("→"+ac.state.name());}
    void exit(Aircon1 ac){p(ac.state.name()+"→ ");}
    void startCool(){        p("start Cool");    }
    void stopCool(){        p("stop Cool");    }
    void startFan(){        p("start Fan");    }
    void stopFan(){        p("stop Fan");    }  

每个power(Aircon1 ac)、cool(Aircon1 ac)的方法体结构都是:

            this.exit(ac);
            //如果有的话,事件(event)相应的动作,如stopFan();
            ac.state =OFF; //下一个状态
            ac.state.entry(ac); 

 

 

[java] view plain copy

  1. package property.state.stateMachine;  
  2. import static tool.Print.*;//pln  
  3. /** 
  4.  * 本来是应该将State1作为Aircon1的内部类的。现在放在外边, 
  5.  * power()等需要变为power(Aircon1 ac) 
  6.  */  
  7. public enum State1{  
  8.     OFF{  
  9.         @Override void exit(Aircon1 ac){super.exit(ac);startFan();}  
  10.         @Override void power(Aircon1 ac){  
  11.             this.exit(ac);  
  12.             ac.state =FANONLY;              
  13.             ac.state.entry(ac);   
  14.         }  
  15.         @Override void cool(Aircon1 ac){  
  16.             pln("nothing");  
  17.         }  
  18.     },FANONLY{  
  19.         @Override void power(Aircon1 ac){  
  20.             this.exit(ac);  
  21.             stopFan();  
  22.             ac.state =OFF;  
  23.             ac.state.entry(ac);   
  24.         }  
  25.         @Override void cool(Aircon1 ac){  
  26.             this.exit(ac);  
  27.             ac.state =COOL;  
  28.             ac.state.entry(ac);  
  29.         }  
  30.     },  
  31.     COOL{  
  32.         @Override void exit(Aircon1 ac){super.exit(ac);stopCool();}  
  33.         @Override void entry(Aircon1 ac){startCool();super.entry(ac);}  
  34.         @Override void power(Aircon1 ac){              
  35.             this.exit(ac);  
  36.             stopFan();  
  37.             ac.state =OFF;  
  38.             ac.state.entry(ac);   
  39.         }  
  40.         @Override void cool(Aircon1 ac){  
  41.             this.exit(ac);  
  42.             ac.state =FANONLY;  
  43.             ac.state.entry(ac);    
  44.         }  
  45.     };  
  46.     //状态模式 提取的接口  
  47.     abstract void power(Aircon1 ac);  
  48.     abstract void cool(Aircon1 ac);  
  49.     //状态机的各种动作action methode  
  50.     void entry(Aircon1 ac){pln("→"+ac.state.name());}  
  51.     void exit(Aircon1 ac){p(ac.state.name()+"→ ");}  
  52.     void startCool(){        p("start Cool");    }  
  53.     void stopCool(){        p("stop Cool");    }  
  54.     void startFan(){        p("start Fan");    }  
  55.     void stopFan(){        p("stop Fan");    }      
  56. }  

 

空调Aircon1的修改版本。

 

[java] view plain copy

  1. package property.state.stateMachine;  
  2. import static tool.Print.*;//pln  
  3. /** 
  4.  * 空调Aircon1。使用状态模式重构Aircon0,使用enum State1编写状态类层次。 
  5.  * @author (yqj2065)  
  6.  * @version 0.1 
  7.  */  
  8. public class Aircon1{  
  9.     State1 state= State1.OFF;//private改默认,删除getState()。  
  10.     //两个Action  
  11.     public void power(){//按power键  
  12.         state.power(this);  
  13.     }  
  14.     public void cool(){//按制冷键  
  15.         state.cool(this);  
  16.     }  
  17.     /** 
  18.      * ACCtrl的代码。 
  19.      */  
  20.     public static void test(){  
  21.         Aircon1 ac = new Aircon1();  
  22.         System.out.println("Current State:" + ac.state.name());  
  23.         ac.cool();  
  24.         ac.power();  
  25.         ac.cool();  
  26.         ac.cool();  
  27.         ac.power();  
  28.         ac.power();  
  29.         ac.power();  
  30.           
  31.     }  
  32. }  

对应测试操作的输出:“OFF→”表示离开OFF状态,而“→FANONLY”...

 

Current State:OFF
nothing
OFF→ start Fan→FANONLY
FANONLY→ start Cool→COOL
COOL→ stop Cool→FANONLY
FANONLY→ stop Fan→OFF
OFF→ start Fan→FANONLY
FANONLY→ stop Fan→OFF
 

 

2.分层状态机

对于状态较多的状态机,通常将具有许多公共的特性的状态合并到一起。例如FANONLY和COOL构成的Running状态

 

状态机中的hierarchical states,我们可以使用组合模式处理。(还没有单独写组合模式,尴尬)。但是,又不一定能够完美地使用组合模式,例如Running到Off,所有的Running的内部状态在PoverEvent时都转化到OFF,很好;OFF到Running,不是所有Running的内部状态都要调用其entry。在使用enum(不好搞类层次)时,使用责任链吧。

 

楼主画图中、考虑更多按钮中....

发布了6 篇原创文章 · 获赞 44 · 访问量 11万+

猜你喜欢

转载自blog.csdn.net/sinat_29255093/article/details/77528021