1、确定输入(也可以为事件),根据输入列出可能出现的状态
2、制状态迁移表
3、写状态迁移框架
switch(input)
{
case input0:
input0_func(cur_state);
break;
case input1:
input1_func(cur_state);
break;
......
}4、实现状态迁移函数
void input0_func(int * next_state)
{
int cur_state;
cur_state = *next_state;
switch(cur_state)
{
case state0:
case state2:
action;
*next_state = statex;
}
}
void input1_func(int * next_state)
{
int cur_state;
cur_state = *next_state;
switch(cur_state)
{
case state0:
action;
*next_state = statex;
}
}
void input2_func(int * next_state)
{
int cur_state;
cur_state = *next_state;
switch(cur_state)
{
case state0:
case state1:
action;
*next_state = statex;
}
}
本文介绍了一种实用的状态机设计方法,包括定义输入事件、创建状态迁移表、编写状态迁移框架及实现具体的状态迁移函数等内容。该方法适用于软件开发中需要处理复杂状态变化的场景。
464

被折叠的 条评论
为什么被折叠?



