状态模式

/***********************************
* 文件名:   状态模式
* 创建人:   陈泽丹
* 创建时间: 20100904
* 反省: 状态中心模块是用来把外部输入的控
制和自身达到某条件引起的控制,集中到一块。

状态模式宜仅用于状态路径的开闭,至于其它
操作,宜交于其它专门类完成。
另外, 状态点可能较多,一个简单的方法是在
接口类处把全部路径否定了,子类实现时,再需
要什么路径开通什么路径。


状态中心和状态的交叉引用问题可用前向声明
解决(前向声明和其声明的对象所处文件无关)


本实例实现状图变化情况如下:
----------Starting--Runing--Fighting--Stoping--System

Starting----- 0 ----  1 ------- 0 ------ 1 ----- 0

Runing------- 0 ----  0 ------- 1 ------ 1 ----- 1

Fighting----- 0 ----  1 ------- 0 ------ 1 ----- 0

Stoping------ 0 ----  0 ------- 0 ------ 0 ----- 0

System------- 1 ----  1 ------- 0 ------ 1 ----- 0
***********************************/


#pragma once
#include <iostream>


class State;
class StateCenter
{
public:
 StateCenter(void);
 virtual ~StateCenter(void);


 //Main dialog<new game, load game, seting, help, ...>
 bool toStarting();
 //playing
 bool toRuning();
 //fighting
 bool toFighting();
 //stoping
 bool toStoping();
 //playing dialog<replay, save game, load game, seting, help, ...>
 bool toSystem();
 //do something
 bool handle();

public:
 bool setState(State* i_pState);
 State* m_pStarting;
 State* m_pRuning;
 State* m_pFighting;
 State* m_pStoping;
 State* m_pSystem;


private:
 State* m_pState;
};

 

////////////////////////////////

 

#pragma once
#include <iostream>

using namespace std;

 

class StateCenter;
class State
{
public:
 State(StateCenter* i_pStateCenter):m_pStateCenter(i_pStateCenter){}
 virtual ~State(void){ m_pStateCenter = NULL; }

 virtual bool toStarting(){ return false; }
 virtual bool toRuning(){ return false; }
 virtual bool toFighting(){ return false; }
 virtual bool toStoping(){ return false; }
 virtual bool toSystem(){ return false; }
 virtual bool handle(){ return false; }

protected:
 StateCenter* m_pStateCenter;
};

////////////////////////////////////////////////////////

 

#pragma once
#include "State.h"

 

class Starting: public State
{
public:
 Starting(StateCenter* i_pStateCenter);
 virtual ~Starting(void);

 
 //new game, load game
 bool toRuning();
 //game over
 bool toStoping();
 //do something(seting, help, ...)
 bool handle();
};

/////////////////////////////////////////////////

 

#pragma once
#include "State.h"


//playing
class Runing: public State
{
public:
 Runing(StateCenter* i_pStateCenter);
 virtual ~Runing(void);

 //fight
 bool toFighting();
 //game over
 bool toStoping();
 //playing dialog<replay, save game, load game, seting, help, ...>
 bool toSystem();
 //play
 bool handle();
};

 

/////////////////////////////////////////

 

#pragma once
#include "State.h"

class Fighting: public State
{
public:
 Fighting(StateCenter* i_pStateCenter);
 virtual ~Fighting(void);

 //succeed
 bool toRuning();
 //failed
 bool toStoping();
 //do something
 bool handle();
};

/////////////////////////////

 

#pragma once
#include "State.h"


class System: public State
{
public:
 System(StateCenter* i_pStateCenter);
 virtual ~System(void);

 //replay
 bool toStarting();
 //play
 bool toRuning();
 //game over
 bool toStoping();
 //playing dialog<replay, save game, load game, seting, help, ...>
 bool handle();
};

 

/////////////////////////////////

 

#pragma once
#include "State.h"

 

class Stoping: public State
{
public:
 Stoping(StateCenter* i_pStateCenter);
 virtual ~Stoping(void);

 //game over
 bool handle();
};

 

////////////////////////////////////////////////

 

#include <iostream>
#include "StateCenter.h"

using namespace std;

 

void main()
{
 for(int i=0; i<10; i++)
 {
  cout<<"---------- "<<i<<" ----------/n"<<endl;
  StateCenter stateCenter;
  stateCenter.setState(stateCenter.m_pStarting);
  cout<<"/n-------------------------------------"<<endl;
  cout<<endl;
 }
 system("pause");
}

 

///////////////////////////////////////////////////////////////////////////////

 

#include "StateCenter.h"
#include "State.h"
#include "Starting.h"
#include "Runing.h"
#include "Fighting.h"
#include "Stoping.h"
#include "System.h"

 

 

 

StateCenter::StateCenter(void):m_pStarting(NULL),m_pRuning(NULL),m_pFighting(NULL),
 m_pStoping(NULL),m_pSystem(NULL)
{
 m_pStarting = new Starting(this);
 m_pRuning = new Runing(this);
 m_pFighting = new Fighting(this);
 m_pStoping = new Stoping(this);
 m_pSystem = new System(this);
}

StateCenter::~StateCenter(void)
{
 if( NULL != m_pStarting){ delete m_pStarting; m_pStarting = NULL; }
 if( NULL != m_pRuning){ delete m_pRuning; m_pRuning = NULL; }
 if( NULL != m_pFighting){ delete m_pFighting; m_pFighting = NULL; }
 if( NULL != m_pStoping){ delete m_pStoping; m_pStoping = NULL; }
 if( NULL != m_pSystem){ delete m_pSystem; m_pSystem = NULL; }
 setState(NULL);
}

//Main dialog<new game, load game, seting, help, ...>
bool StateCenter::toStarting()
{
 if( NULL == m_pState)
  return false;
 return m_pState->toStarting();
}
//playing
bool StateCenter::toRuning()
{
 if( NULL == m_pState)
  return false;
 return m_pState->toRuning();
}
//fighting
bool StateCenter::toFighting()
{
 if( NULL == m_pState)
  return false;
 return m_pState->toFighting();
}
//stoping
bool StateCenter::toStoping()
{
 if( NULL == m_pState)
  return false;
 return m_pState->toStoping();
}
//playing dialog<replay, save game, load game, seting, help, ...>
bool StateCenter::toSystem()
{
 if( NULL == m_pState)
  return false;
 return m_pState->toSystem();
}
//do something
bool StateCenter::handle()
{
 if( NULL == m_pState)
  return false;
 return m_pState->handle();
}

//public property for accidents
bool StateCenter::setState(State* i_pSate)
{
 m_pState = i_pSate;
 handle();
 return true;
}

你可能感兴趣的:(状态模式)