75 lines
1.2 KiB
C++
75 lines
1.2 KiB
C++
#pragma once
|
|
#include "common.hpp"
|
|
|
|
class State;
|
|
class Context // 上下文环境类,负责具体状态的切换
|
|
{
|
|
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();
|
|
|
|
};
|