//参考:李创 http://www.cppblog.com/converse
// State.cpp : Defines the entry point for the console application.
//
# include "stdafx.h"
# include <iostream >
/*
|-----------| |-----------|
| Context |<>------------>| IState |
|-----------| |-----------|
|
|
|-------------|------------|
| |
|------------------| |------------------|
| ConcreateStateA | | ConcreateStateB |
|------------------| |------------------|
*/
class IState;
//Context为客户持有的对象,该对象内聚一个State指针指向具体的状态类;
//Request接口响应用户的请求,并转交给它所持有的类去Handle;
//ChangeState接口给具体的状态类使用,以使得Context能根据需要改变自己的状态。
class Context
{
public :
Context(IState * pState);
~Context();
void Request();
void ChangeState(IState *pState);
void PrintState();
private :
IState * m_pState;
};
//状态类接口
class IState
{
public :
virtual ~IState(){}; //纯虚函数是必须的。
virtual void Handle(Context * pContext) = 0; //这个请求会改变状态
virtual void PrintOut() = 0; //这个请求则不会改变状态
};
//具体状态类A
class ConcreateStateA : public IState
{
public :
virtual void Handle(Context * pContext);
virtual void PrintOut();
};
//具体状态类B
class ConcreateStateB : public IState
{
public :
virtual void Handle(Context * pContext);
virtual void PrintOut();
};
void ConcreateStateA : :Handle( Context * pContext )
{
printf(( "Handled by state A: => B./n"));
if (NULL != pContext)
{
pContext - >ChangeState( new ConcreateStateB()); //StateB => StateA
}
}
void ConcreateStateA : :PrintOut()
{
printf(( "I am state A./n"));
}
void ConcreateStateB : :Handle( Context * pContext )
{
printf(( "Handled by state B: => A./n"));
if (NULL != pContext)
{
pContext - >ChangeState( new ConcreateStateA()); //StateA => StateB
}
}
void ConcreateStateB : :PrintOut()
{
printf(( "I am state B./n"));
}
//对Context的请求,都转交给它内部所持有的状态类来处理。
void Context : :Request()
{
if (NULL != m_pState)
{
m_pState - >Handle( this);
}
}
Context : : ~Context()
{
delete m_pState; m_pState = NULL;
}
//状态的改变
void Context : :ChangeState( IState *pState )
{
if (NULL != m_pState)
{
delete m_pState; m_pState = NULL;
}
m_pState = pState;
}
//状态初始化
Context : :Context( IState * pState ) : m_pState(pState)
{
}
void Context : :PrintState()
{
if (NULL != m_pState)
{
m_pState - >PrintOut();
}
}
//客户代码
int _tmain( int argc, _TCHAR * argv[])
{
IState * pState = new ConcreateStateA();
Context * pContext = new Context(pState);
pContext - >PrintState();
pContext - >Request();
pContext - >PrintState();
pContext - >Request();
pContext - >PrintState();
pContext - >Request();
pContext - >PrintState();
return 0;
}
//
# include "stdafx.h"
# include <iostream >
/*
|-----------| |-----------|
| Context |<>------------>| IState |
|-----------| |-----------|
|
|
|-------------|------------|
| |
|------------------| |------------------|
| ConcreateStateA | | ConcreateStateB |
|------------------| |------------------|
*/
class IState;
//Context为客户持有的对象,该对象内聚一个State指针指向具体的状态类;
//Request接口响应用户的请求,并转交给它所持有的类去Handle;
//ChangeState接口给具体的状态类使用,以使得Context能根据需要改变自己的状态。
class Context
{
public :
Context(IState * pState);
~Context();
void Request();
void ChangeState(IState *pState);
void PrintState();
private :
IState * m_pState;
};
//状态类接口
class IState
{
public :
virtual ~IState(){}; //纯虚函数是必须的。
virtual void Handle(Context * pContext) = 0; //这个请求会改变状态
virtual void PrintOut() = 0; //这个请求则不会改变状态
};
//具体状态类A
class ConcreateStateA : public IState
{
public :
virtual void Handle(Context * pContext);
virtual void PrintOut();
};
//具体状态类B
class ConcreateStateB : public IState
{
public :
virtual void Handle(Context * pContext);
virtual void PrintOut();
};
void ConcreateStateA : :Handle( Context * pContext )
{
printf(( "Handled by state A: => B./n"));
if (NULL != pContext)
{
pContext - >ChangeState( new ConcreateStateB()); //StateB => StateA
}
}
void ConcreateStateA : :PrintOut()
{
printf(( "I am state A./n"));
}
void ConcreateStateB : :Handle( Context * pContext )
{
printf(( "Handled by state B: => A./n"));
if (NULL != pContext)
{
pContext - >ChangeState( new ConcreateStateA()); //StateA => StateB
}
}
void ConcreateStateB : :PrintOut()
{
printf(( "I am state B./n"));
}
//对Context的请求,都转交给它内部所持有的状态类来处理。
void Context : :Request()
{
if (NULL != m_pState)
{
m_pState - >Handle( this);
}
}
Context : : ~Context()
{
delete m_pState; m_pState = NULL;
}
//状态的改变
void Context : :ChangeState( IState *pState )
{
if (NULL != m_pState)
{
delete m_pState; m_pState = NULL;
}
m_pState = pState;
}
//状态初始化
Context : :Context( IState * pState ) : m_pState(pState)
{
}
void Context : :PrintState()
{
if (NULL != m_pState)
{
m_pState - >PrintOut();
}
}
//客户代码
int _tmain( int argc, _TCHAR * argv[])
{
IState * pState = new ConcreateStateA();
Context * pContext = new Context(pState);
pContext - >PrintState();
pContext - >Request();
pContext - >PrintState();
pContext - >Request();
pContext - >PrintState();
pContext - >Request();
pContext - >PrintState();
return 0;
}