https://github.com/loyayz/simple-statemachine
简单通用的状态机,解决业务中的状态流转问题。
https://github.com/loyayz/simple-statemachine
state-machine statemachine
Last synced: 5 months ago
JSON representation
简单通用的状态机,解决业务中的状态流转问题。
- Host: GitHub
- URL: https://github.com/loyayz/simple-statemachine
- Owner: loyayz
- License: mit
- Created: 2021-02-26T06:56:25.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-02-28T03:31:15.000Z (over 5 years ago)
- Last Synced: 2023-07-01T12:09:41.166Z (almost 3 years ago)
- Topics: state-machine, statemachine
- Language: Java
- Homepage:
- Size: 10.7 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# simple-statemachine
[](https://mvnrepository.com/artifact/com.loyayz/simple-statemachine)
简单通用的状态机,解决业务中的状态流转问题。
## 1 实现原理
[实现原理](https://blog.csdn.net/significantfrank/article/details/104996419)
## 2 安装
```xml
com.loyayz
simple-statemachine
1.0.1
```
## 3 快速开始
### 3.1 定义状态机
```java
// 状态
enum States {
STATE1, STATE2, STATE3,STATE4
}
// 事件
enum Events {
EVENT1, EVENT2, EVENT3
}
// 状态机
public void define() {
StatemachineBuilder.create()
// STATE1 -> STATE2 on EVENT1
.newTransition()
.from(States.STATE1)
.to(States.STATE2)
.on(Events.EVENT1)
.when((ctx) -> true)
.then((from, to, event, ctx) -> System.out.println("ctx [" + ctx + "], " + from + " -> " + to + " on " + event))
.build()
.register("my_first_statemachine");
}
```
### 3.2 使用
```java
// 获取状态机
Statemachine statemachine = StatemachineFactory.get("my_first_statemachine");
// 执行状态机
statemachine.execute(States.STATE1, Events.EVENT1, "test");
```
[使用示例](https://github.com/loyayz/simple-sample)