有限状态机 (FSM) 和 Android 的 Java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3776693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Finite State Machine (FSM) and Android's Java
提问by Luigi
I'm willing to develop a soccer game for Android.
我愿意为Android开发一款足球游戏。
Because the complexity of the AI, i really think i need to design it using a FSM (Finite State Machine)and not with a monster switch.
由于 AI 的复杂性,我真的认为我需要使用FSM(有限状态机)而不是怪物开关来设计它。
Googling around i found some FSM written in Java, but nothing explicitly compatible with Android: I wonder if there is someone here that used a FSM before for that platform.
谷歌搜索我发现一些用 Java 编写的 FSM,但没有与 Android 明确兼容:我想知道这里是否有人之前在该平台上使用过 FSM。
回答by Andrey Chausenko
I was looking for a FSM for Android a few months ago but couldn't find anything suitable, so had to create my own.
几个月前我一直在寻找适用于 Android 的 FSM,但找不到合适的东西,所以不得不创建我自己的。
I used it in a few projects by now and quite happy. It makes code much cleaner and is easy to use. If anyone's still looking for a state machine for java, check EasyFlowout.
我现在在几个项目中使用了它并且很高兴。它使代码更加简洁并且易于使用。如果有人仍在寻找 Java 的状态机,请查看EasyFlow。
You can contributeto the project as well.
回答by byeo
Android has an internal Hierarchical State Machine (HSM), which integrates with the Android message Queue.
Android 有一个内部分层状态机 (HSM),它与 Android 消息队列集成。
It is internal, so you're not able to access it explicitly from the SDK.
它是内部的,因此您无法从 SDK 显式访问它。
However, if you follow the terms of the Apache 2.0 licence you can make your own version.
https://android.googlesource.com/platform/frameworks/base/+/gingerbread-release/core/java/com/android/internal/util/HierarchicalStateMachine.java
但是,如果您遵循 Apache 2.0 许可证的条款,您可以制作自己的版本。
https://android.googlesource.com/platform/frameworks/base/+/gingerbread-release/core/java/com/android/internal/util/HierarchicalStateMachine.java
Edit:
This class is now called StateMachine.
编辑:
这个类现在称为StateMachine。
回答by metch
This state machine (FSM) implementation used in Android and provided by Google seems to be a good option. You can get it here on Github.
这种在 Android 中使用并由 Google 提供的状态机 (FSM) 实现似乎是一个不错的选择。你可以在 Github 上得到它。
You can use the StateMachine
class to define states, the initial sate, you can perform tasks when you enter or exit a state, you can send events and delayed events in order to implement timeouts for instance.
您可以使用StateMachine
该类来定义状态,初始状态,您可以在进入或退出状态时执行任务,您可以发送事件和延迟事件以实现例如超时。
I did not find many other alternatives, but this implementation contains all the features I was looking for. This is part of the Android project, so I presume this was good tested. Hope this help.
我没有找到许多其他替代方案,但此实现包含我正在寻找的所有功能。这是 Android 项目的一部分,所以我认为这是很好的测试。希望这有帮助。