C++ 状态机

#include "stdlib.h"
#include
#include
#include "conio.h"
using namespace  std;
enum SateID
{
char_a=0,
char_b=0,
char_c=0,
};
class baseState
{
public:
virtual  void getChar()=0;
};
class  StateA
{
public:
virtual void getChar()
{
cout<<"A State "< }
protected:
private:
};
class  StateB
{
public:
virtual void getChar()
{
cout<<"B State "< }
protected:
private:
};
class  StateC
{
public:
virtual void getChar()
{
cout<<"C State "< }
protected:
private:
};
class  StateEngic
{
public:
void  init()

baseState* p=(baseState*)(new StateA());
m_pbaseArr.insert(make_pair('a',p));
p=(baseState*)(new StateB());
m_pbaseArr.insert(make_pair('b',p));
p=(baseState*)(new StateC());
m_pbaseArr.insert(make_pair('c',p));
}
void ChangeState()
{
cout<<"StateEngic State "< char ch;
while (1)
{
ch=getch();
if (ch=='q')
{
break;
}
else if (m_pbaseArr.find(ch)!=m_pbaseArr.end())
{
m_pbaseArr.find(ch)->second->getChar();
}
 
}
}
protected:
baseState* m_pState;
map m_pbaseArr;
private:
};
int main()
{
StateEngic StateInstace;
StateInstace.init();
StateInstace.ChangeState();
system("pause");
return 0;
}

你可能感兴趣的:(设计模式)