c++桥接模式,中介者模式应用实现状态跳转

上图为例,按上述两种方式实现的模式跳转,如果在原先的三种模式之间再增加多一种模式,就会引起每个模式都会要求改变,并且逻辑混乱,因此更改模式为桥接+中介者访问,将抽象和实现分离,实现之间采用中介者实现模式之间的通信,或者在实现中去管理每个模式之间的通信。方便后续扩展:

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);               //当前模式需要操作的状态
}
相关推荐
爱吃喵的鲤鱼12 分钟前
linux进程的状态之环境变量
linux·运维·服务器·开发语言·c++
7年老菜鸡40 分钟前
策略模式(C++)三分钟读懂
c++·qt·策略模式
Ni-Guvara1 小时前
函数对象笔记
c++·算法
似霰1 小时前
安卓智能指针sp、wp、RefBase浅析
android·c++·binder
芊寻(嵌入式)1 小时前
C转C++学习笔记--基础知识摘录总结
开发语言·c++·笔记·学习
獨枭1 小时前
C++ 项目中使用 .dll 和 .def 文件的操作指南
c++
霁月风1 小时前
设计模式——观察者模式
c++·观察者模式·设计模式
橘色的喵1 小时前
C++编程:避免因编译优化引发的多线程死锁问题
c++·多线程·memory·死锁·内存屏障·内存栅栏·memory barrier
何曾参静谧2 小时前
「C/C++」C/C++ 之 变量作用域详解
c语言·开发语言·c++
AI街潜水的八角2 小时前
基于C++的决策树C4.5机器学习算法(不调包)
c++·算法·决策树·机器学习