观察者模式的通用性调用

1.通用框架接口

#ifndef _OBSERVERIMP_H__
#define _OBSERVERIMP_H__

#include "stdafx.h"
#include <map>
#include <vector>

template <typename ReturnT, typename ParamT>
class ReceiverImplBase;

template <typename ReturnT, typename ParamT>
class ObserverImplBase
{
public:
	virtual void AddReceiver(ReceiverImplBase<ReturnT, ParamT>* receiver) = 0;
	virtual void RemoveReceiver(ReceiverImplBase<ReturnT, ParamT>* receiver) = 0;
	virtual ReturnT Broadcast(ParamT param) = 0;
	virtual ReturnT Notify(ParamT param) = 0;
};

template <typename ReturnT, typename ParamT>
class ReceiverImplBase
{
public:
	virtual void AddObserver(ObserverImplBase<ReturnT, ParamT>* observer) = 0;
	virtual void RemoveObserver() = 0;
	virtual ReturnT Receive(ParamT param) = 0;
	virtual ReturnT Respond(ParamT param, ObserverImplBase<ReturnT, ParamT>* observer) = 0;
};

template <typename ReturnT, typename ParamT>
class ReceiverImpl;

template <typename ReturnT, typename ParamT>
class ObserverImpl : public ObserverImplBase<ReturnT, ParamT>
{
public:
	ObserverImpl()
		: count_(0)
	{}

	virtual ~ObserverImpl()	{}

	virtual void AddReceiver(ReceiverImplBase<ReturnT, ParamT>* receiver)
	{
		if (receiver == NULL)
			return;

		receivers_[count_] = receiver;
		receiver->AddObserver(this);
		count_++;
	}

	virtual void RemoveReceiver(ReceiverImplBase<ReturnT, ParamT>* receiver)
	{
		if (receiver == NULL)
			return;

		typename ReceiversMap::iterator it = receivers_.begin();
		for (; it != receivers_.end(); ++it)
		{
			if (it->second == receiver)
			{
				receivers_.erase(it);
				break;
			}
		}
	}

	virtual ReturnT Broadcast(ParamT param)
	{
		typename ReceiversMap::iterator it = receivers_.begin();
		for (; it != receivers_.end(); ++it)
		{
			it->second->Receive(param);
		}

		return ReturnT();
	}

	virtual ReturnT Notify(ParamT param)
	{
		typename ReceiversMap::iterator it = receivers_.begin();
		for (; it != receivers_.end(); ++it)
		{
			it->second->Respond(param, this);
		}

		return ReturnT();
	}

protected:
	typedef std::map<int, ReceiverImplBase<ReturnT, ParamT>*> ReceiversMap;
	ReceiversMap receivers_;
	int count_;
};


template <typename ReturnT, typename ParamT>
class ReceiverImpl : public ReceiverImplBase<ReturnT, ParamT>
{
public:
	ReceiverImpl() : count_(0)
	{}

	virtual ~ReceiverImpl()	{}

	virtual void AddObserver(ObserverImplBase<ReturnT, ParamT>* observer)
	{
		observers_[count_] = observer;
		count_++;
	}

	virtual void RemoveObserver()
	{
		typename ObserversMap::iterator it = observers_.begin();
		for (; it != observers_.end(); ++it)
		{
			it->second->RemoveReceiver(this);
		}
	}

	virtual ReturnT Receive(ParamT param)
	{
		return ReturnT();
	}

	virtual ReturnT Respond(ParamT param, ObserverImplBase<ReturnT, ParamT>* observer)
	{
		return ReturnT();
	}

protected:
	typedef std::map<int, ObserverImplBase<ReturnT, ParamT>*> ObserversMap;
	ObserversMap observers_;
	int count_;
};

#endif /*_OBSERVERIMP_H__*/

2.观察者实例化

#ifndef _OBSERVEREXAMPLE_H__
#define _OBSERVEREXAMPLE_H__

#include "observerImp.hpp"

typedef struct
{
	int iLength;
	int iWidth;
}ST_ATTR, *PST_ATTR;

using ObserverManager = ObserverImpl<bool, PST_ATTR>;
using ReceiverManager = ReceiverImpl<bool, PST_ATTR>;

class CObserverExample : public ObserverManager
{
public:
	static CObserverExample& GetInstance() 
	{ 
		static CObserverExample s_objObserverExample;
		return s_objObserverExample;
	}

	void GetReceiverInfo(PST_ATTR pstAttr)
	{
		printf("Respond:Get Receiver Respond---length:%d width:%d\n", pstAttr->iLength, pstAttr->iWidth);
	}
private:
	CObserverExample() {};
	CObserverExample(const CObserverExample&) = delete;//禁类赋值
	CObserverExample& operator=(const CObserverExample&) = delete;//禁类拷贝
};

#endif /*_OBSERVEREXAPLE_H__*/

3.接收者实例化

#ifndef _RECEIVEREXAMPLE_H__
#define _RECEIVEREXAMPLE_H__

#include "observerExample.hpp"

class CReceiverExampleBase : public ReceiverManager
{
public:
	virtual bool Receive(PST_ATTR pstAttr)
	{
		return true;
	}

	virtual bool Respond(PST_ATTR pstAttr, ObserverImplBase<bool, PST_ATTR>* observer)
	{
		return true;
	}

public:
	CReceiverExampleBase() { CObserverExample::GetInstance().AddReceiver(this); };
	virtual ~CReceiverExampleBase() = default;
};

class CReceiverExample1 : public CReceiverExampleBase
{
public:
	virtual bool Receive(PST_ATTR pstAttr)
	{
		printf("Receive:Receiver Example 1---length:%d  width:%d\n", pstAttr->iLength, pstAttr->iWidth);
		return true;
	}

	virtual bool Respond(PST_ATTR pstAttr, ObserverImplBase<bool, PST_ATTR>* observer)
	{
		PST_ATTR pstDstAttr = new ST_ATTR;
		memcpy(pstDstAttr, pstAttr, sizeof (ST_ATTR));
		pstDstAttr->iLength = 20;

		CObserverExample* pobjObserverExample = static_cast<CObserverExample*>(observer);
		pobjObserverExample->GetReceiverInfo(pstDstAttr);

		delete pstDstAttr;
		pstDstAttr = nullptr;

		return true;
	}
public:
	CReceiverExample1() {};
	virtual ~CReceiverExample1() = default;
};

class CReceiverExample2 : public CReceiverExampleBase
{
public:
	virtual bool Receive(PST_ATTR pstAttr)
	{
		printf("Receive:Receiver Example 2---length:%d  width:%d\n", pstAttr->iLength, pstAttr->iWidth);
		return true;
	}

	virtual bool Respond(PST_ATTR pstAttr, ObserverImplBase<bool, PST_ATTR>* observer)
	{
		PST_ATTR pstDstAttr = new ST_ATTR;
		memcpy(pstDstAttr, pstAttr, sizeof (ST_ATTR));
		pstDstAttr->iWidth = 10;

		CObserverExample* pobjObserverExample = static_cast<CObserverExample*>(observer);
		pobjObserverExample->GetReceiverInfo(pstDstAttr);

		delete pstDstAttr;
		pstDstAttr = nullptr;

		return true;
	}
public:
	CReceiverExample2() {};
	virtual ~CReceiverExample2() = default;
};

#endif /*_RECEIVEREXAMPLE_H__*/

4.实例调用

#include "stdafx.h"
#include "receiverExample.hpp"

int _tmain(int argc, _TCHAR* argv[])
{
	ST_ATTR stAttr = {10, 5};

	CReceiverExample1 objReceiverExample1;
	CReceiverExample2 objReceiverExample2;

	CObserverExample::GetInstance().Broadcast(&stAttr);//广播数据给receiver处理
	CObserverExample::GetInstance().Notify(&stAttr);//广播数据给receiver处理并将处理结果返回给observer

	return 0;
}

5.显示

 

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页