#ifndef DESIGN_PATTERNS_BEHAVIORAL_STATE_MODEL_H
#define DESIGN_PATTERNS_BEHAVIORAL_STATE_MODEL_H
#include <iostream>
enum class LiftStateCode
{
OPENNING,
CLOSEING,
RUNNING,
STOPING
};
class CStateModeContext;
class CLiftState
{
public:
CLiftState(void) {}
virtual ~CLiftState(void) {}
void SetContext(CStateModeContext* pContext);
virtual void Open() = 0;
virtual void Close() = 0;
virtual void Run() = 0;
virtual void Stop() = 0;
protected:
CStateModeContext* m_pContext;
};
class COpenningState :public CLiftState
{
public:
COpenningState(void) {}
~COpenningState(void) {}
void Open();
void Close();
void Run();
void Stop();
};
class CCloseingState :public CLiftState
{
public:
CCloseingState(void) {}
~CCloseingState(void) {}
void Open();
void Close();
void Run();
void Stop();
};
class CRunningState :public CLiftState
{
public:
CRunningState(void) {}
~CRunningState(void) {}
void Open();
void Close();
void Run();
void Stop();
};
class CStoppingState :public CLiftState
{
public:
CStoppingState(void) {}
~CStoppingState(void) {}
void Open();
void Close();
void Run();
void Stop();
};
class CStateModeContext
{
public:
CStateModeContext(void);
~CStateModeContext(void);
CLiftState* GetLiftState();
void Open();
void Close();
void Run();
void Stop();
void SetLiftState(LiftStateCode enStateCode);
private:
CLiftState* m_pLiftState;
//定义出所有的电梯状态
COpenningState* m_pOpenningState;
CCloseingState* m_pCloseingState;
CRunningState* m_pRunningState;
CStoppingState* m_pStoppingState;
};
void NewDoStateIt();
void TestStateModel();
#endif //
#include "design_patterns_behavioral_state_model.h"
#include <iostream>
using namespace std;
void CLiftState::SetContext(CStateModeContext* pContext)
{
m_pContext = pContext;
}
//打开电梯门
void COpenningState::Open()
{
cout << "电梯门开启..." << endl;
}
//开启当然可以关闭了,我就想测试一下电梯门开关功能
void COpenningState::Close()
{
//状态修改
m_pContext->SetLiftState(LiftStateCode::CLOSEING);
//动作委托为CloseState来执行
m_pContext->GetLiftState()->Close();
}
//门开着电梯就想跑,这电梯,吓死你!
void COpenningState::Run()
{
//do nothing
}
//开门还不停止?
void COpenningState::Stop()
{
//do nothing
}
//电梯门关了再打开
void CCloseingState::Open()
{
m_pContext->SetLiftState(LiftStateCode::OPENNING);
m_pContext->GetLiftState()->Open();
}
//电梯门关才,这就关闭状态要实现动作
void CCloseingState::Close()
{
cout << "电梯门关闭..." << endl;
}
//电梯门关了就跑
void CCloseingState::Run()
{
this->CLiftState::m_pContext->SetLiftState(LiftStateCode::RUNNING);
this->CLiftState::m_pContext->GetLiftState()->Run();
}
//电梯门关着,我就不按楼层
void CCloseingState::Stop()
{
this->CLiftState::m_pContext->SetLiftState(LiftStateCode::STOPING);
this->CLiftState::m_pContext->GetLiftState()->Stop();
}
//运行的时候开电梯门?电梯不会给你开的。
void CRunningState::Open()
{
//do nothing
}
//电梯关闭?这是肯定了
void CRunningState::Close()
{
//do nothing
}
//具体实现
void CRunningState::Run()
{
cout << "电梯上下跑..." << endl;
}
//这个事绝对是合理,光运行不停止还有谁敢做这个电梯?
void CRunningState::Stop()
{
this->CLiftState::m_pContext->SetLiftState(LiftStateCode::STOPING);
this->CLiftState::m_pContext->GetLiftState()->Stop();
}
//停止状态,开门,那是要的!
void CStoppingState::Open()
{
this->CLiftState::m_pContext->SetLiftState(LiftStateCode::OPENNING);
this->CLiftState::m_pContext->GetLiftState()->Open();
}
//停止状态关门?电梯门本来就是关着的!
void CStoppingState::Close()
{
//do nothing
}
//停止状态再跑起来,正常的很
void CStoppingState::Run()
{
this->CLiftState::m_pContext->SetLiftState(LiftStateCode::RUNNING);
this->CLiftState::m_pContext->GetLiftState()->Run();
}
//停止状态是怎么发生的呢?当然是停止方法执行了。
void CStoppingState::Stop()
{
cout << "电梯停止了..." << endl;
}
CStateModeContext::CStateModeContext(void):m_pLiftState(nullptr)
{
m_pOpenningState = new COpenningState();
m_pCloseingState = new CCloseingState();
m_pRunningState = new CRunningState();
m_pStoppingState = new CStoppingState();
}
CStateModeContext::~CStateModeContext(void)
{
delete m_pOpenningState;
m_pOpenningState = nullptr;
delete m_pCloseingState;
m_pCloseingState = nullptr;
delete m_pRunningState;
m_pRunningState = nullptr;
delete m_pStoppingState;
m_pStoppingState = nullptr;
}
CLiftState* CStateModeContext::GetLiftState()
{
return m_pLiftState;
}
void CStateModeContext::Open()
{
this->m_pLiftState->Open();
}
void CStateModeContext::Close()
{
this->m_pLiftState->Close();
}
void CStateModeContext::Run()
{
this->m_pLiftState->Run();
}
void CStateModeContext::Stop()
{
this->m_pLiftState->Stop();
}
void CStateModeContext::SetLiftState(LiftStateCode enStateCode)
{
switch (enStateCode)
{
case LiftStateCode::CLOSEING:
m_pLiftState = m_pCloseingState;
break;
case LiftStateCode::OPENNING:
m_pLiftState = m_pOpenningState;
break;
case LiftStateCode::RUNNING:
m_pLiftState = m_pRunningState;
break;
case LiftStateCode::STOPING:
m_pLiftState = m_pStoppingState;
break;
}
m_pLiftState->SetContext(this);
}
void NewDoStateIt()
{
CStateModeContext context;
context.SetLiftState(LiftStateCode::CLOSEING);
context.Close();
context.Open();
context.Run();
context.Stop();
context.SetLiftState(LiftStateCode::OPENNING);
context.Close();
context.Open();
context.Run();
context.Stop();
}
void TestStateModel()
{
NewDoStateIt();
}