initialize repository of design pattern template
This commit is contained in:
140
src/state.cpp
Normal file
140
src/state.cpp
Normal file
@@ -0,0 +1,140 @@
|
||||
#include "state.hpp"
|
||||
|
||||
void SolidState::behavior(Water* p_water) const
|
||||
{
|
||||
cout << "<EFBFBD><EFBFBD>ǰ״̬Ϊ<EFBFBD><EFBFBD>̬<EFBFBD><EFBFBD><EFBFBD>¶<EFBFBD>Ϊ" << p_water->getTemperature() << "<EFBFBD><EFBFBD>" << endl;
|
||||
}
|
||||
|
||||
bool SolidState::isMatch(int stateInfo) const
|
||||
{
|
||||
return stateInfo <= 0;
|
||||
}
|
||||
|
||||
void Liquidtate::behavior(Water* p_water) const
|
||||
{
|
||||
cout << "<EFBFBD><EFBFBD>ǰ״̬ΪҺ̬<EFBFBD><EFBFBD><EFBFBD>¶<EFBFBD>Ϊ" << p_water->getTemperature() << "<EFBFBD><EFBFBD>" << endl;
|
||||
}
|
||||
|
||||
bool Liquidtate::isMatch(int stateInfo) const
|
||||
{
|
||||
return stateInfo > 0 && stateInfo < 100;
|
||||
}
|
||||
|
||||
void GaseousState::behavior(Water* p_water) const
|
||||
{
|
||||
cout << "<EFBFBD><EFBFBD>ǰ״̬Ϊ<EFBFBD><EFBFBD>̬<EFBFBD><EFBFBD><EFBFBD>¶<EFBFBD>Ϊ" << p_water->getTemperature() << "<EFBFBD><EFBFBD>" << endl;
|
||||
}
|
||||
|
||||
bool GaseousState::isMatch(int stateInfo) const
|
||||
{
|
||||
return stateInfo >= 100;
|
||||
}
|
||||
|
||||
Water::Water()
|
||||
{
|
||||
addState(new SolidState("<EFBFBD><EFBFBD>̬"));
|
||||
addState(new Liquidtate("Һ̬"));
|
||||
addState(new GaseousState("<EFBFBD><EFBFBD>̬"));
|
||||
|
||||
setTemperature(25);
|
||||
}
|
||||
|
||||
int Water::getTemperature()
|
||||
{
|
||||
return getStateInfo();
|
||||
}
|
||||
|
||||
void Water::setTemperature(int temperature)
|
||||
{
|
||||
setStateInfo(temperature);
|
||||
}
|
||||
|
||||
void Water::riseTemperature(int step)
|
||||
{
|
||||
setTemperature(getTemperature() + step);
|
||||
}
|
||||
|
||||
void Water::reduceTemperature(int step)
|
||||
{
|
||||
setTemperature(getTemperature() - step);
|
||||
}
|
||||
|
||||
void Water::behavior()
|
||||
{
|
||||
State* p_state = getState();
|
||||
p_state->behavior(this);
|
||||
}
|
||||
|
||||
string State::getName()
|
||||
{
|
||||
return m_name;
|
||||
}
|
||||
|
||||
Context::Context()
|
||||
{
|
||||
m_stateInfo = 0;
|
||||
p_curState = nullptr;
|
||||
}
|
||||
|
||||
Context::~Context()
|
||||
{
|
||||
vector<State*>::iterator iter = p_states.begin();
|
||||
for (; iter != p_states.end(); iter++)
|
||||
{
|
||||
delete (*iter); // <20>ͷ<EFBFBD><CDB7>ڴ<EFBFBD>
|
||||
}
|
||||
p_states.clear();
|
||||
}
|
||||
|
||||
void Context::addState(State* state)
|
||||
{
|
||||
vector<State*>::iterator iter = find(p_states.begin(), p_states.end(), state);
|
||||
if (iter == p_states.end())
|
||||
{
|
||||
p_states.push_back(state);
|
||||
}
|
||||
}
|
||||
|
||||
bool Context::changeState(State* state)
|
||||
{
|
||||
vector<State*>::iterator iter = find(p_states.begin(), p_states.end(), state);
|
||||
if (iter == p_states.end())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (p_curState == nullptr)
|
||||
{
|
||||
cout << "<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>Ϊ " << state->getName() << endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << "<EFBFBD><EFBFBD> " << p_curState->getName() << " <20><>Ϊ " << state->getName() << endl;
|
||||
}
|
||||
p_curState = state;
|
||||
return true;
|
||||
}
|
||||
|
||||
State* Context::getState()
|
||||
{
|
||||
return p_curState;
|
||||
}
|
||||
|
||||
void Context::setStateInfo(int stateInfo)
|
||||
{
|
||||
m_stateInfo = stateInfo;
|
||||
for (vector<State*>::iterator iter = p_states.begin(); iter != p_states.end(); iter++)
|
||||
{
|
||||
if ((*iter)->isMatch(stateInfo))
|
||||
{
|
||||
changeState((*iter));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int Context::getStateInfo()
|
||||
{
|
||||
return m_stateInfo;
|
||||
}
|
Reference in New Issue
Block a user