简单有限状态机(FSM)的实现

通用Gate方式的FSM接口,

//-------------------------------------------
#include "IGateState.h"
#include <assert.h>
#include <iostream.h>
//-------------------------------------------

//-------------------------------------------
class IGateFSM 
{
public:
 IGateFSM();
 virtual ~IGateFSM();

public:

 inline void SetState(IGateState * state) {_state = state;}

 virtual void OnEvent(ENUM_EVENT event);
 
private:
 IGateState * _state;
};
//-------------------------------------------

// 派生类的实现,以地铁十字专门为模型,提供Lock/UnLock等与业务相关的方法

//-------------------------------------------

class VGateFSM : public IGateFSM
{
public:
 VGateFSM() {}
 virtual ~VGateFSM() {}

public:

 void Lock();
 void UnLock();
 void Alarm();
 void Thanks();

private:
 bool m_IsLock;  

};
//-------------------------------------------

//================================
// ´代表状态类的抽象接口
class IGateFSM;

// 事件描述
enum ENUM_EVENT
{
 EVENT_COIN,  // ͶÈëÓ²±Òʼþ
 EVENT_PASS  // ´©Ô½Ê¼þ
};

class IGateState 
{
public:
 IGateState();
 virtual ~IGateState();

public:
 
 virtual void OnEvent(ENUM_EVENT event, IGateFSM * fsm) = 0;

};
//------------------------------------------

// State模式的实现类,代表两种状态

class VGateLock : public IGateState
{
public:
 static IGateState * Instance();

public:
 virtual void OnEvent(ENUM_EVENT event, IGateFSM * fsm);
 
private:
 VGateLock(){}

 static VGateLock * _instance;

};
//------------------------------------------
class VGateUnLock : public IGateState
{
public:
 static IGateState * Instance();

public:
 virtual void OnEvent(ENUM_EVENT event, IGateFSM * fsm);

private:
 VGateUnLock(){}

 static  VGateUnLock * _instance;

};

// FSM的实现

//-------------------------------------------
IGateFSM::IGateFSM()
{
 _state = NULL;
}
IGateFSM::~IGateFSM()
{
}
//-------------------------------------------
void IGateFSM::OnEvent(ENUM_EVENT event)
{
 assert(_state != NULL);

 _state->OnEvent(event,this);
}
//-------------------------------------------
void VGateFSM::Lock()
{
 this->m_IsLock = true;

 cout<<"Lock the gate."<<endl;
}
//-------------------------------------------
void VGateFSM::UnLock()
{
 this->m_IsLock = true;
 
 cout<<"Unlock the gate."<<endl;
}
//-------------------------------------------
void VGateFSM::Alarm()
{
 cout<<"You should insert a coin."<<endl;
}
//-------------------------------------------
void VGateFSM::Thanks()
{
 cout<<"Thank You for pay again."<<endl;
}
//-------------------------------------------

// state的实现部分

VGateLock * VGateLock::_instance  = NULL;
VGateUnLock * VGateUnLock::_instance = NULL;
void VGateLock::OnEvent(ENUM_EVENT event, IGateFSM * fsm)
{
 VGateFSM * vfsm = (VGateFSM *) fsm;

 if (event == EVENT_PASS)
 {
  vfsm->Alarm();
  return ;
 }
 
 if (event == EVENT_COIN)
  vfsm->UnLock();

 vfsm->SetState(VGateUnLock::Instance());

}
//-------------------------------------------
void VGateUnLock::OnEvent(ENUM_EVENT event, IGateFSM * fsm)
{
 VGateFSM * vfsm = (VGateFSM *) fsm;

 if (event == EVENT_COIN)
 {
  vfsm->Thanks();
  return ;
 }
 
 if (event == EVENT_PASS)
  vfsm->Lock();

 vfsm->SetState(VGateLock::Instance());
}
//-------------------------------------------
IGateState * VGateLock::Instance()
{
 if (_instance == NULL)
  _instance = new VGateLock();

 return _instance;
}
//-------------------------------------------
IGateState * VGateUnLock::Instance()
{
 if (_instance == NULL)
  _instance = new VGateUnLock();

 return _instance;
}
//-------------------------------------------

测试程序:

int main(int argc, char* argv[])
{
 printf("FSM Demo:/n/n");

 VGateFSM * _fsm = new VGateFSM();

 _fsm->SetState(VGateLock::Instance());

 _fsm->OnEvent(EVENT_PASS);
 _fsm->OnEvent(EVENT_COIN);
 _fsm->OnEvent(EVENT_COIN);
 _fsm->OnEvent(EVENT_PASS);

 delete _fsm;

 printf("/n");
 return 0;
}


可以看到输出为:

You should insert a coin.

Unlock the gate

Thank You for pay again.

Lock the gate.

与预期结果一样

小结:

有限状态机是OO中的一种建模方法,是以状态对象角度来分析解决问题,最主要的是通过建立抽象的状态类,以及相关的实现类,把状态的处

理,转换在派生类中实现,从而实现比较良好的逻辑与实现分开。

另:结构上与Strategy,Observer都类似,看来模式也不用记得那么死,其实都有相通的地方。。

  UML图片咋粘贴不上来?怪。

阅读更多
个人分类: C/C++
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭