菜鸟笔记
提升您的技术认知

状态机 java简单实现-ag真人游戏

介绍

有限状态机通常用于模拟序列逻辑,换句话说,就是用于代表和控制执行流程。

有限状态机所需条件:

  • 一个物体只有固定的几种状态(例如交通灯只有绿灯、黄灯和红灯三个状态)。
  • 有固定的变化顺序(例如交通灯只能绿灯->黄灯->红灯->绿灯循环)
  • 物体同一时间点上只有一种状态。

例子

这里拿一个空调做例子,图是用了别人的,这里说明下下面三个状态的意思:

off:空调关机

fanonly:空调仅吹风

cool:空调制冷

原始的解决办法

stateenum

用枚举类定义三种状态


package org.example.origin;
public enum stateenum {
power_off, fan_only, cool
}

   

originaction

presspoweroff() 表示点开关按钮,presscool() 表示点击制冷按钮。


package org.example.origin;
public class originaction {
private stateenum stateenum;
public originaction() {
this.stateenum = stateenum.power_off;
}
public void presspoweroff() {
// 关机->吹风
if (stateenum == stateenum.power_off) {
this.stateenum = stateenum.fan_only;
system.out.println("power on succeed.");
} else {
// 吹风或制冷->关机
this.stateenum = stateenum.power_off;
system.out.println("power off succeed.");
}
}
public void presscool() {
if (stateenum == stateenum.power_off) {
system.out.println("cool failed because power off.");
} else if (stateenum == stateenum.fan_only) {
// 吹风->制冷
this.stateenum = stateenum.cool;
system.out.println("open cool succeed.");
} else {
// 制冷->吹风
this.stateenum = stateenum.fan_only;
system.out.println("close cool succeed");
}
}
}

   

origintest

编写测试类


package org.example.origin;
public class origintest {
public static void main(string[] args) {
originaction originaction = new originaction();
originaction.presscool();
originaction.presspoweroff();
originaction.presscool();
originaction.presscool();
originaction.presspoweroff();
}
}

   

执行结果:

cool failed because power off.
power on succeed.
open cool succeed.
close cool succeed
power off succeed.

使用有限状态机解决

finitestatemachineenum

这里使用了 java 的枚举类,确保了类型约束。


package org.example.state;
public enum finitestatemachineenum {
power_off {
// 关机->吹风
@override
public finitestatemachineenum presspoweroff() {
system.out.println("power on succeed.");
return finitestatemachineenum.fan_only;
}
// 无效
@override
public finitestatemachineenum presscool() {
system.out.println("cool failed because power off.");
return finitestatemachineenum.power_off;
}
},
fan_only {
// 吹风->关机
@override
public finitestatemachineenum presspoweroff() {
system.out.println("power off succeed.");
return finitestatemachineenum.power_off;
}
// 吹风->制冷
@override
public finitestatemachineenum presscool() {
system.out.println("open cool succeed.");
return finitestatemachineenum.cool;
}
},
cool {
// 制冷->关机
@override
public finitestatemachineenum presspoweroff() {
system.out.println("power off succeed.");
return finitestatemachineenum.power_off;
}
// 制冷->吹风
@override
public finitestatemachineenum presscool() {
system.out.println("close cool succeed");
return finitestatemachineenum.fan_only;
}
};
public abstract finitestatemachineenum presspoweroff();
public abstract finitestatemachineenum presscool();
}

   

finitestatemachinetest

编写测试类


package org.example.state;
public class finitestatemachinetest {
public static void main(string[] args) {
finitestatemachineenum stateenum = finitestatemachineenum.power_off;
stateenum.presscool()
.presspoweroff()
.presscool()
.presscool()
.presspoweroff();
}
}

   

执行结果:

cool failed because power off.
power on succeed.
open cool succeed.
close cool succeed
power off succeed.

网站地图