设计模式例程
This commit is contained in:
74
inc/state.hpp
Normal file
74
inc/state.hpp
Normal file
@ -0,0 +1,74 @@
|
||||
#pragma once
|
||||
#include "common.hpp"
|
||||
|
||||
class State;
|
||||
class Context // <20><><EFBFBD><EFBFBD><EFBFBD>Ļ<EFBFBD><C4BB><EFBFBD><EFBFBD>࣬<EFBFBD><E0A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬<D7B4><CCAC><EFBFBD>л<EFBFBD>
|
||||
{
|
||||
public:
|
||||
Context();
|
||||
~Context();
|
||||
|
||||
void addState(State* state);
|
||||
bool changeState(State* state);
|
||||
State* getState();
|
||||
void setStateInfo(int stateInfo);
|
||||
int getStateInfo();
|
||||
|
||||
private:
|
||||
State* p_curState;
|
||||
vector<State*> p_states;
|
||||
int m_stateInfo;
|
||||
};
|
||||
|
||||
class Water;
|
||||
|
||||
class State
|
||||
{
|
||||
public:
|
||||
State(string name) :m_name(name) {};
|
||||
virtual void behavior(Water* p_water) const = 0;
|
||||
virtual bool isMatch(int stateInfo) const = 0;
|
||||
string getName();
|
||||
|
||||
private:
|
||||
string m_name;
|
||||
};
|
||||
|
||||
|
||||
class SolidState :public State
|
||||
{
|
||||
public:
|
||||
SolidState(string name) :State(name) {};
|
||||
void behavior(Water* p_water) const;
|
||||
bool isMatch(int stateInfo) const;
|
||||
};
|
||||
|
||||
class Liquidtate :public State
|
||||
{
|
||||
public:
|
||||
Liquidtate(string name) :State(name) {};
|
||||
void behavior(Water* p_water) const;
|
||||
bool isMatch(int stateInfo) const;
|
||||
};
|
||||
|
||||
class GaseousState :public State
|
||||
{
|
||||
public:
|
||||
GaseousState(string name) :State(name) {};
|
||||
void behavior(Water* p_water) const;
|
||||
bool isMatch(int stateInfo) const;
|
||||
};
|
||||
|
||||
class Water :public Context
|
||||
{
|
||||
public:
|
||||
Water();
|
||||
|
||||
int getTemperature();
|
||||
void setTemperature(int temperature);
|
||||
void riseTemperature(int step);
|
||||
void reduceTemperature(int step);
|
||||
|
||||
void behavior();
|
||||
|
||||
};
|
Reference in New Issue
Block a user