前提:因为最近打算学设计模式的状态模式,但不是很明白有限状态机和状态模式之间的关系,索性用C++实现了一个简单案例复习了一下FSM,如果有更好的实现方法,欢迎留言
有限状态机四大要素
- 现态:当前所处状态
- 次态:当条件满足后,即将转移的下一个状态
- 动作:当满足某个事件时执行的动作;动作执行完毕后可以转移到另一个状态或保持原有状态
- 条件:转移状态所需的条件,当满足条件时,会触发一个动作或进行状态转移
C++函数指针实现
案例:学生的日常生活。
- 学生的日常生活包含以下几个状态:起床、上学、吃午饭、做作业、睡觉;
- 每个状态之间进行转移需要执行相应的事件。
我分为以下几个步骤来实现:
- (1)绘制状态转移图
- (2)创建状态转移的FSMItem类
- 枚举:所有状态
State 、所有事件Event ; - 成员变量:现态
_curState 、事件_event 、次态_nextState - 成员函数:动作函数
- (3)创建有限状态机FSM类
- 成员变量:状态转移表
vector<FSMItem*> _fsmTable - 成员函数:初始化状态转移表、状态转移、根据事件执行相应动作
- (4)测试FSM
(1)绘制状态转移图
(2)FSMItem类
class FSMItem
{
friend class FSM;
private:
static void getUp()
{
cout << "student is getting up!" << endl;
}
static void go2School()
{
cout << "student is going to school!" << endl;
}
static void haveLunch()
{
cout << "student is having lunch!" << endl;
}
static void doHomework()
{
cout << "student is doing homework!" << endl;
}
static void sleeping()
{
cout << "student is sleeping!" << endl;
}
public:
enum State
{
GETUP = 0,
GOTOSCHOOL,
HAVELUNCH,
DOHOMEWORK,
SLEEP
};
enum Events
{
EVENT1 = 0,
EVENT2,
EVENT3
};
public:
FSMItem(State curState, Events event, void(*action)(), State nextState)
:_curState(curState), _event(event), _action(action), _nextState(nextState) {}
private:
State _curState;
Events _event;
void (*_action)();
State _nextState;
};
(3)FSM类
class FSM
{
public:
FSM(FSMItem::State curState= FSMItem::GETUP):_curState(curState)
{
initFSMTable();
}
void transferState(FSMItem::State nextState)
{
_curState = nextState;
}
void handleEvent(FSMItem::Events event)
{
FSMItem::State curState = _curState;
void (*action)() = nullptr;
FSMItem::State nextState;
bool flag = false;
for (int i = 0; i < _fsmTable.size(); i++)
{
if (event == _fsmTable[i]->_event && curState == _fsmTable[i]->_curState)
{
flag = true;
action = _fsmTable[i]->_action;
nextState = _fsmTable[i]->_nextState;
break;
}
}
if (flag)
{
if (action)
{
action();
}
transferState(nextState);
}
}
private:
void initFSMTable()
{
_fsmTable.push_back(new FSMItem(FSMItem::GETUP, FSMItem::EVENT1, &FSMItem::getUp, FSMItem::GOTOSCHOOL));
_fsmTable.push_back(new FSMItem(FSMItem::GOTOSCHOOL, FSMItem::EVENT2, &FSMItem::go2School, FSMItem::HAVELUNCH));
_fsmTable.push_back(new FSMItem(FSMItem::HAVELUNCH, FSMItem::EVENT3, &FSMItem::haveLunch, FSMItem::DOHOMEWORK));
_fsmTable.push_back(new FSMItem(FSMItem::DOHOMEWORK, FSMItem::EVENT1, &FSMItem::doHomework, FSMItem::SLEEP));
_fsmTable.push_back(new FSMItem(FSMItem::SLEEP, FSMItem::EVENT2, &FSMItem::sleeping, FSMItem::GETUP));
}
public:
FSMItem::State _curState;
private:
vector<FSMItem*> _fsmTable;
};
(4)测试FSM
#include<iostream>
#include<vector>
using namespace std;
void testEvent(FSMItem::Events& event)
{
switch (event)
{
case FSMItem::EVENT1:
event = FSMItem::EVENT2;
break;
case FSMItem::EVENT2:
event = FSMItem::EVENT3;
break;
case FSMItem::EVENT3:
event = FSMItem::EVENT1;
break;
}
}
int main()
{
FSM *fsm = new FSM();
auto event = FSMItem::EVENT1;
while (1)
{
cout << "event " << event << " is coming..." << endl;
fsm->handleEvent(event);
cout << "fsm current state is " << fsm->_curState << endl;
testEvent(event);
}
return 0;
}
执行效果:
|