上图为例,按上述两种方式实现的模式跳转,如果在原先的三种模式之间再增加多一种模式,就会引起每个模式都会要求改变,并且逻辑混乱,因此更改模式为桥接+中介者访问,将抽象和实现分离,实现之间采用中介者实现模式之间的通信,或者在实现中去管理每个模式之间的通信。方便后续扩展:
cpp
class MaWorkState;
typedef enum _childMastate {
RUN = 0,
STOP,
RUNNING,
STATE_QTY,
}childMastate;
typedef bool(MaWorkState::* Statefunc)(void);
typedef std::function<bool(void)> pcb;
class StateSwitching {
public:
StateSwitching() {};
virtual void DoSate()
{
while (!m_pcontext.empty())
{
m_deque.at(m_pcontext.at(0));
m_pcontext.pop_front();
}
}
template<typename Ty>
void PushEvent(Ty ev)
{
m_pcontext.push_back(ev);
}
protected:
std::deque<int> m_pcontext;
std::deque<pcb> m_deque;
ModuleNormalOutput* m_ModuleNormalOutput;
DdsWaveHandle* m_pDds;
};
/*
class mediator;
class StateSwitching {
public:
StateSwitching() {};
virtual void GenMediator(mediator* media) { m_mediator = media; };
virtual bool ReleaseMode() {}; //这样:不管增加多少模式,都只是实现自己的两个接口就可以,但必须将模式之间的交互隔离,模式之间不产生联系,如果出现关联,抽线一层(使用一个中介者实现关联)
virtual bool CtrlMode() {};
protected:
std::deque<int> m_pcontext;
std::deque<pcb> m_deque;
mediator* m_mediator;
};
class mediator {
public:
virtual void realese(StateSwitching*child1, StateSwitching*child2) =0;
virtual void ctrl(StateSwitching* child1, StateSwitching* child2) =0;
};
class GenMediatorChild :public mediator {
public:
GenMediatorChild():m_swith1(NULL), m_swith2(NULL) {};
virtual void realese(StateSwitching* child1, StateSwitching* child2) {};
virtual void ctrl(StateSwitching* child1, StateSwitching* child2) {};
StateSwitching* m_swith1;
StateSwitching* m_swith2;
};
*/
class NormalStateSwitching :public StateSwitching {
public:
typedef enum NormalState
{
LIST_TO_NORMAL = 0,
SOLAR_TO_NORMAL,
};
NormalStateSwitching() {
m_deque.resize(4);
//m_deque.push_back(std::bind(&NormalStateSwitching::OnListtoNormal,this));
m_deque.at(LIST_TO_NORMAL) = std::bind(&NormalStateSwitching::OnListtoNormal, this);
m_deque.at(SOLAR_TO_NORMAL) = std::bind(&NormalStateSwitching::OnListtoNormal, this);
};
bool OnListtoNormal(void)
{
return true;
}
bool OnSolartoNormal(void)
{
return true;
}
};
class SolarStateSwitching :public StateSwitching { //当增加多一个模式,每一个状态的跳转都需要增加,考虑怎么优化 //换一种实现方式:将自己正在执行的状态在切换模式的时候,针对自己的状态释放
//需要执行的模式做加载,那么每一个模式就只有release,ctrl接口
public:
typedef enum SolarState
{
LIST_TO_SOLAR = 0,
NORMAL_TO_SOLAR,
};
SolarStateSwitching() {
m_deque.resize(4);
//m_deque.push_back(std::bind(&NormalStateSwitching::OnListtoNormal,this));
m_deque.at(LIST_TO_SOLAR) = std::bind(&SolarStateSwitching::OnListtoSolar, this);
m_deque.at(NORMAL_TO_SOLAR) = std::bind(&SolarStateSwitching::OnNormaltoSolar, this);
};
bool OnListtoSolar(void)
{
return true;
}
bool OnNormaltoSolar(void)
{
return true;
}
};
class MaWorkState
{
public:
MaWorkState(StateSwitching* state) :m_StateSwitching(state) {
m_FuncState[RUN] = &MaWorkState::start;
m_FuncState[STOP] = &MaWorkState::stop;
m_FuncState[RUNNING] = &MaWorkState::running;
};
~MaWorkState() {};
virtual bool DoWorkPrepare(int smode) {
return true;
}
virtual bool DoWorkDone(childMastate py) {
m_StateSwitching->DoSate();
(this->*m_FuncState[py])();
return true;
}
protected:
virtual bool start(void) = 0;
virtual bool stop(void) = 0;
virtual bool running(void) = 0;
ModuleNormalOutput* m_ModuleNormalOutput;
DdsWaveHandle* m_pDds;
RUN_MODE m_mode;
public:
Statefunc m_FuncState[STATE_QTY];
StateSwitching* m_StateSwitching;
};
class MaNormalStart :public MaWorkState
{
public:
MaNormalStart(StateSwitching* state) :MaWorkState(state) {
m_mode = NORM_MODE;
};
virtual bool DoWorkPrepare(RUN_MODE smode) { //当前用户切换了工作模式
if(m_mode!=smode)
m_StateSwitching->PushEvent<NormalStateSwitching::NormalState>(NormalStateSwitching::NormalState(smode));
return true;
}
protected:
virtual bool start(void) {
printf("normalstart\n"); return true;
};
virtual bool stop(void) { return true; };
virtual bool running(void) { return true; };
private:
};
class SolarStart :public MaWorkState
{
public:
SolarStart(StateSwitching* state) :MaWorkState(state) {
m_mode = SOLAR_MODE;
};
virtual bool DoWorkPrepare(RUn_MODE smode) { return true; }
protected:
virtual bool start(void) {
printf("SolarStart\n"); return true;
};
virtual bool stop(void) { return true; };
virtual bool running(void) { return true; };
private:
};
class ListStart :public MaWorkState
{
public:
ListStart(StateSwitching* state) :MaWorkState(state) {
m_mode = LIST_MODE;
};
virtual bool DoWorkPrepare(RUN_MODE smode) { return true; }
protected:
virtual bool start(void) {
printf("SolarStart\n"); return true;
};
virtual bool stop(void) { return true; };
virtual bool running(void) { return true; };
private:
WAVE_LIB_TYPE m_eLibType;
};
int main()
{
std::deque<MaWorkState*> m_deque;
m_deque.push_back(new MaNormalStart(new NormalStateSwitching()));
m_deque.push_back(new SolarStart(new SolarStateSwitching()));
m_deque.push_back(new ListStart(new ListStateSwitching()));
//增加多一个cell模式,只需要增加CellStart,和CellStateSwitching类,用户调用接口不变
//m_deque.push_back(new CellStart(new CellStateSwitching()));
m_deque.at(m_eRunmode)->DoWorkPrepare(m_SysCtrlMode); //用户设置模式m_SysCtrlMode,上一次记录的模式m_eRunmode
m_deque.at(m_eRunmode)->DoWorkDone(RUN); //当前模式需要操作的状态
}