杨文龙的博客

粗缯大布裹生涯,腹有诗书气自华。


  • 首页

  • 分类

  • 关于

  • 归档

  • 标签

  • 站点地图

JAVA状态机

发表于 2017-06-16 | 分类于 design

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还是喜欢分支语句。微笑,简洁明快。

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

测试代码的输出:
Current State:0
nothing
start Fan
start Cool
stop Cool
stop Fan
start Fan
在此基础上,可以花10分钟练习一下,采用状态模式修改上述代码。我们使用enum编写状态类层次。其结构如下:

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

(本来是应该将State1作为Aircon1的内部类的。放在外边,power()等需要添加参数Aircon1,变为power(Aircon1 ac)).
现在,丰富有限状态机的细节,增添④动作(action),如事件(event)相应的动作和状态的动作。

1
2

为此,在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);

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

阅读全文 »

plantuml

发表于 2017-06-11 | 分类于 design

本文给出了适用PlantUML的示例。

阅读全文 »

plantuml

发表于 2017-06-11 | 分类于 design

本文给出了适用PlantUML的示例。

阅读全文 »

first article

发表于 2017-06-08 | 分类于 db

Hello World

发表于 2017-06-08 | 分类于 db

Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hexo, you can find the answer in troubleshooting or you can ask me on GitHub.

Quick Start

Create a new post

1
$ hexo new "My New Post"

More info: Writing

Run server

1
$ hexo server

More info: Server

Generate static files

1
$ hexo generate

More info: Generating

Deploy to remote sites

1
$ hexo deploy

More info: Deployment

techyang

techyang

5 日志
2 分类
7 标签
GitHub
© 2017 techyang
由 Hexo 强力驱动
主题 - NexT.Pisces