⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 state.cpp

📁 Factory,abstract Factory,Builder,Prototype,Singleton,Adapt,Bridge等20种常见的设计模式(含代码)
💻 CPP
字号:
/********************************************************************
	created:	2006/08/05
	filename: 	State.cpp
	author:		李创
                http://www.cppblog.com/converse/

	purpose:	State模式的演示代码
*********************************************************************/

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

Context::Context(State* pState)
	: m_pState(pState)
{

}

Context::~Context()
{
	delete m_pState;
	m_pState = NULL;
}

void Context::Request()
{
	if (NULL != m_pState)
	{
		m_pState->Handle(this);
	}
}

void Context::ChangeState(State *pState)
{
	if (NULL != m_pState)
	{
		delete m_pState;
		m_pState = NULL;
	}
	
	m_pState = pState;
}

void ConcreateStateA::Handle(Context* pContext)
{
	std::cout << "Handle by ConcreateStateA\n";
	
	if (NULL != pContext)
	{
		pContext->ChangeState(new ConcreateStateB());
	}
}

void ConcreateStateB::Handle(Context* pContext)
{
	std::cout << "Handle by ConcreateStateB\n";

	if (NULL != pContext)
	{
		pContext->ChangeState(new ConcreateStateA());
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -