# state diagram and state machine
###### tags: `state diagram` `state machine`
---
![](https://miro.medium.com/max/1188/1*2_eNcTH_I2R1Gc3FuYwIjA.png)
---
定義狀態
```kotlin=
enum class MatchState {
Created,
Pending,
Failed,
Matched,
}
enum class Command {
Create,
Like,
Dislike,
RewindToCreated,
RewindToLiked,
RewindToDisliked,
Match,
UnMatch,
}
```
---
狀態指令
```kotlin=
data class StateTransition(
val currentState: MatchState,
val command: Command
)
```
---
核對表
```kotlin=
class MatchEntity:MatchCommand{
private var currentState = MatchState.Created
private val transitionChecker = mapOf(
StateTransition(MatchState.Created, Command.Like) to MatchState.Pending,
StateTransition(MatchState.Created, Command.Dislike) to MatchState.Failed,
StateTransition(MatchState.Pending, Command.Match) to MatchState.Matched,
StateTransition(MatchState.Pending, Command.UnMatch) to MatchState.Failed,
StateTransition(MatchState.Failed, Command.RewindToCreated) to MatchState.Created,
StateTransition(MatchState.Failed, Command.RewindToLiked) to MatchState.Pending,
StateTransition(MatchState.Failed, Command.RewindToDisliked) to MatchState.Failed,
)
...
}
```
---
狀態切換檢查
```kotlin=
class MatchEntity:MatchCommand{
...
private fun changeState(command: Command){
val newTransition = StateTransition(currentState, command)
transitionChecker[newTransition]?.let {
currentState = it
} ?: throw Exception("not support exception")
}
}
```
---
介面
```kotlin=
interface MatchCommand{
fun like()
fun dislike()
fun match()
fun unMatch()
fun rewindToCreated()
fun rewindToLiked()
fun rewindToDisliked()
}
```
---
實作
```kotlin=
class MatchEntity:MatchCommand{
...
override fun match() {
changeState(Command.Match)
}
...
}
```
---
```kotlin=
fun main() {
val match = MatchEntity()
//tryFail(match)
tryMatch(match)
}
fun tryFail(match:MatchEntity){
match.like() //pending
match.unMatch() //unmatch
}
fun tryMatch(match:MatchEntity){
match.dislike() //failed
match.rewindToLiked() //pending
match.match() //matched
}
enum class MatchState {
Created,
Pending,
Failed,
Matched,
}
enum class Command {
Create,
Like,
Dislike,
RewindToCreated,
RewindToLiked,
RewindToDisliked,
Match,
UnMatch,
}
data class StateTransition(
val currentState: MatchState,
val command: Command
)
class MatchEntity:MatchCommand{
private var currentState = MatchState.Created
private val transitionChecker = mapOf(
StateTransition(MatchState.Created, Command.Like) to MatchState.Pending,
StateTransition(MatchState.Created, Command.Dislike) to MatchState.Failed,
StateTransition(MatchState.Pending, Command.Match) to MatchState.Matched,
StateTransition(MatchState.Pending, Command.UnMatch) to MatchState.Failed,
StateTransition(MatchState.Failed, Command.RewindToCreated) to MatchState.Created,
StateTransition(MatchState.Failed, Command.RewindToLiked) to MatchState.Pending,
StateTransition(MatchState.Failed, Command.RewindToDisliked) to MatchState.Failed,
)
private fun changeState(command: Command){
val newTransition = StateTransition(currentState, command)
transitionChecker[newTransition]?.let {
currentState = it
} ?: throw Exception("not support exception")
}
override fun like() {
changeState(Command.Like)
println(currentState.name)
}
override fun dislike() {
changeState(Command.Dislike)
println(currentState.name)
}
override fun match() {
changeState(Command.Match)
println(currentState.name)
}
override fun unMatch() {
changeState(Command.UnMatch)
println(currentState.name)
}
override fun rewindToCreated() {
changeState(Command.RewindToCreated)
println(currentState.name)
}
override fun rewindToLiked() {
changeState(Command.RewindToLiked)
println(currentState.name)
}
override fun rewindToDisliked() {
changeState(Command.RewindToDisliked)
println(currentState.name)
}
}
interface MatchCommand{
fun like()
fun dislike()
fun match()
fun unMatch()
fun rewindToCreated()
fun rewindToLiked()
fun rewindToDisliked()
}
```
[link](https://medium.com/wenchin-rolls-around/states-and-state-machines-8c9519018cd6)
[閒談軟體架構:State 與語言](https://medium.com/%E9%96%92%E8%AB%87%E8%BB%9F%E9%AB%94%E6%9E%B6%E6%A7%8B/%E9%96%92%E8%AB%87%E8%BB%9F%E9%AB%94%E6%9E%B6%E6%A7%8B-%E8%81%8A%E8%81%8A-state-%E8%88%87%E8%AA%9E%E8%A8%80-c28f332173de)
[軟體設計方法論:狀態機之生老病死](https://r23456999.medium.com/%E8%BB%9F%E9%AB%94%E8%A8%AD%E8%A8%88%E6%96%B9%E6%B3%95%E8%AB%96-%E7%8B%80%E6%85%8B%E6%A9%9F%E4%B9%8B%E7%94%9F%E8%80%81%E7%97%85%E6%AD%BB-dc3d6e92cd01)
{"metaMigratedAt":"2023-06-16T21:15:45.626Z","metaMigratedFrom":"Content","title":"state diagram and state machine","breaks":"true","contributors":"[{\"id\":\"b07ca9a3-1ebc-4263-a0c7-51e30de31ed0\",\"add\":5880,\"del\":829}]"}