状态模式----C++实现

状态模式

定义:

允许一个对象在其内部状态改变时改变它的行为。对象看起来似乎修改了它的类。

实用性:

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

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

 

优点:

1、  将与特定状态相关的行为局部化,并且将不同状态的行为分割开来。

2、  使得状态转换显示化。

3、  状态对象可以被共享。

 

结构:

实现:

 

class Context;

 

class State

{

public:

       State(string name):m_name(name)

       { }

       virtual void Handle(Context *pContext)

       { }

 

       virtual~State()

       {     }

       string GetStateName()

       {

              returnm_name;

       }

protected:

       voidChangeState(Context *pContext,State *pState);

private:

       string m_name;

};

 

class Context

{

public:

       voidRequest()

       {

        m_pCurrentState->Handle(this);

       }

 

       voidSetCurrentState(State *pState)

       { 

              cout<<"设置当前状态为 :"<<pState->GetStateName()<<endl;

              m_pCurrentState = pState;

       }

 

       State *GetCurrentState()

       {

              cout<<"当前状态为 :"<<m_pCurrentState->GetStateName()<<endl;

              returnm_pCurrentState;

       }

 

protected:

       friend class State;

       void  ChangeState(State *pState)

       {

              cout<<"当前状态由 : "<<m_pCurrentState->GetStateName()<<" 转变为状态"<<pState->GetStateName()<<endl;

              m_pCurrentState = pState;

       }

private:

       State   *m_pCurrentState;

};

 

voidState::ChangeState(Context *pContext,State *pState)

{

       pContext->ChangeState(pState);

}

 

 //状态子类可以共享,所以用单例模式,这里就忽略内存释放的问题了

class ConcreteStateA;

class ConcreteStateC:public State

{

public:

       staticState* GetStateAInstance()

       {

              if(m_pInstance == NULL)

              {

                     m_pInstance = new ConcreteStateC("状态 C");

              }

              returnm_pInstance;

       }

       voidHandle(Context *pContext);

private:

       ConcreteStateC(string name):State(name)

       {   }

       staticConcreteStateC *m_pInstance;

};

 

class ConcreteStateB:public State

{

public:

       staticState* GetStateAInstance()

       {

              if(m_pInstance == NULL)

              {

                     m_pInstance = new ConcreteStateB("状态 B");

              }

              returnm_pInstance;

       }

       voidHandle(Context *pContext)

       {

              cout<<"状态 B 处理的业务逻辑"<<endl;

              ChangeState(pContext,ConcreteStateC::GetStateAInstance());

       }

 

private:

       ConcreteStateB(string name):State(name)

       {  }

       staticConcreteStateB *m_pInstance;

};

 

class ConcreteStateA:public State

{

public:

       staticState* GetStateAInstance()

       {

              if(m_pInstance == NULL)

              {

                     m_pInstance = new ConcreteStateA("状态 A");

              }

              returnm_pInstance;

       }

       voidHandle(Context *pContext)

       {

              cout<<"状态 A 处理的业务逻辑"<<endl;

              ChangeState(pContext,ConcreteStateB::GetStateAInstance());

       }

 

private:

       ConcreteStateA(string name):State(name)

       { }

       staticConcreteStateA *m_pInstance;

};

 

voidConcreteStateC::Handle(Context *pContext)

{

       cout<<"状态 C 处理的业务逻辑"<<endl;

       ChangeState(pContext,ConcreteStateA::GetStateAInstance());

}

 

ConcreteStateA*ConcreteStateA::m_pInstance = NULL;

ConcreteStateB*ConcreteStateB::m_pInstance = NULL;

ConcreteStateC*ConcreteStateC::m_pInstance = NULL;

 

       Context *pContext = new Context;

       pContext->SetCurrentState(ConcreteStateA::GetStateAInstance());

       pContext->Request();

       pContext->Request();

       pContext->Request();


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。