首页 > 代码库 > 有限状态自动机FSM实现(二)--State模式

有限状态自动机FSM实现(二)--State模式

基于switch结构的有限状态自动机的实现已经在前一篇博文中给出,请猛击。


State模式适用于如下情况:

1)一个对象的行为取决于它的状态,并且必须在运行时根据状态改变它的行为。

2)一个操作中含有庞大的多分支的条件语句,且这些分支依赖于该对象的状态。


State模式包含如下对象:

Context对象

1)定义客户感兴趣的接口。

2)维护一个State子类的实例,这个实例定义当前状态。


State状态对象

定义一个接口封装与Context的一个特定状态相关的行为。


以下图为例:TCPConnection对象即Context对象, TCPState对象即State状态对象。


下面是门的有限状态自动机对应的结构图:



源代码如下:

//fsm.h

#pragma  once
#include <string>
using namespace std;

enum DoorEvent
{
	EVENT_UNKOWN		   =   0,
	EVENT_OPEN_DOOR        =   1,
	EVENT_CLOSE_DOOR       =   2,	
	EVENT_LOCK_DOOR        =   3,
	EVENT_UNLOCK_DOOR      =   4
};

class Context;

class State
{
public:
	State(string stateStr="STATE_UNKOWN"):m_stateStr(stateStr){}
	virtual ~State(){}
	string getStateStr(){return m_stateStr;}

	virtual void Handle(Context* pContext, DoorEvent event) = 0;

protected:
	string m_stateStr;
};

class  StateOpened : public State
{
public:
	StateOpened() : State("STATE_OPENED"){}

	void Handle(Context* pContext, DoorEvent event);
};

class StateClosed: public State
{
public:
	StateClosed():State("STATE_CLOSED"){}

	void Handle(Context* pContext, DoorEvent event);
};

class StateLocked: public State
{
public:	
	StateLocked() : State("STATE_LOCKED"){}

	void Handle(Context* pContext, DoorEvent event);
};

class StateUnlocked: public State
{
public:
	StateUnlocked() : State("STATE_UNLOCKED"){}

	void Handle(Context* pContext, DoorEvent event);
};

class Context
{
public:
	Context(State* pState){m_state = pState;}
	~Context(){}
	void Request(DoorEvent event){m_state->Handle(this, event);}
	void ChangeState(State *pState){m_state = pState;}
	string getStateStr(void){return m_state->getStateStr();}

private:
	State *m_state;
};


//fsm.cpp

#include <iostream>
#include <iomanip>
using namespace std;
#include "fsm.h"
#include <Windows.h>

const char *DoorEventStr[] = 
{
	"EVENT_UNKOWN",
	"EVENT_OPEN_DOOR",
	"EVENT_CLOSE_DOOR",	
	"EVENT_LOCK_DOOR",
	"EVENT_UNLOCK_DOOR"
};

void StateOpened::Handle(Context* pContext, DoorEvent event)
{
	switch(event)
	{
	case EVENT_CLOSE_DOOR:
		pContext->ChangeState(new StateClosed);
		break;

	default:
		break;
	}
}

void StateClosed::Handle(Context* pContext, DoorEvent event)
{
	switch(event)
	{
	case EVENT_LOCK_DOOR:
		pContext->ChangeState(new StateLocked);
		break;

	case EVENT_OPEN_DOOR:
		pContext->ChangeState(new StateOpened);
		break;

	default:
		break;
	}
}

void StateLocked::Handle(Context* pContext, DoorEvent event)
{
	switch(event)
	{
	case EVENT_UNLOCK_DOOR:
		pContext->ChangeState(new StateUnlocked);
		break;

	default:
		break;
	}
}


void StateUnlocked::Handle(Context* pContext, DoorEvent event)
{
	switch(event)
	{
	case EVENT_OPEN_DOOR:
		pContext->ChangeState(new StateOpened);
		break;

	case EVENT_LOCK_DOOR:
		pContext->ChangeState(new StateLocked);
		break;

	default:
		break;
	}
}	


int main(int argc, char* argv[])
{
	DoorEvent event = EVENT_UNKOWN;  

	Context *pContext = new Context(new StateOpened);
	
	int index = 0;
	while(index < 10)
	{  
		switch(index%4+1)
		{  
			case 1: event = EVENT_OPEN_DOOR; break;  
			case 2: event = EVENT_CLOSE_DOOR; break;  
			case 3: event = EVENT_LOCK_DOOR; break;  
			case 4: event = EVENT_UNLOCK_DOOR; break;  
			default: break;  
		}

		index++;  

		string lastStat = pContext->getStateStr();

		pContext->Request(event);

		cout<<setiosflags(ios::left) 
			<<setw(15)<<lastStat
			<<" - "
			<<setw(20)<<DoorEventStr[event]
			<<" - "
			<<setw(15)<<pContext->getStateStr()<<endl;

		Sleep(1);
	}

	return 0;
}


参考文献:

《设计模式——可复用面向对象软件的基础》

http://blog.csdn.net/uyghfjhh/article/details/19651225

http://blog.csdn.net/xiexievv/article/details/6733470