State模式源码示例

//参考:李创 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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值