State Machine.(状态机)

What is a State Machine?

Any device that changes its state from one to another due to some actions are defined to be state machines. For example an ATM machine, traffic signal, remote control, the computer itself, etc. Most software applications also depend and operate based on the state. There are two types of state machines.

1. Finite state machine – A state machine holding a defined set of states and they work within that. 

2. Infinite state machine – Here the states can be many and cannot be predefined.

Most common state machines are finite ones

Composition of a State Machine

The following things collectively make an effective finite state machine.

State:

A defined set of states. At any point of time the state machine will be in any one of the defined states. For example Red, Green and Yellow in a traffic signal system.

State Transition:

A state machine changing its state from one to another is called State Transition. Usually there will be a state transition table built that will contain the state sequences and also metadata saying which event will cause which state transition.

Triggers:

Triggers are the points that induce the state transition in a state machine.

Events or Actions:

Actions are performed by the state machines when a particular state is attained and exited. There will be Entry and Exit actions for each state.

Guard Condition:

This is the piece of component that validates the transition and ensures that no invalid state changes are carried out.

 

State Transition Table:

The turnstile state machine can be represented by a state transition table, showing for each state the new state and the output (action) resulting from each input

Current StateInputNext StateOutput
LockedcoinUnlockedUnlock turnstile so customer can push through
pushLockedNone
UnlockedcoinUnlockedNone
pushLockedWhen customer has pushed through, lock turnstile

 

State machine 和 软件设计模式里面的 state pattern 是有区别的。

实现 State machine 并不一定需要用 State pattern, 模式。

但是用 state pattern 模式来实现状态机比较清晰和易于理解。

关于State machine . 这里有篇很好的文章:

https://docs.microsoft.com/en-us/dotnet/framework/windows-workflow-foundation/state-machine-workflows

转载于:https://www.cnblogs.com/fdyang/p/4897959.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值