一个很酷的事件处理简单模型

表示是原创啊,但是是在总结了各路大婶知识的基础上:

#include <iostream>
#include <memory>
using namespace std;


class AbstractEvent
{
public:
	virtual char* Event(char* apMsg)=0;
};
class AbstractMsgHandler
{
public:
	virtual void Handler(char* apAnswer)=0;
};
class MsgSender
{
public:
	MsgSender(AbstractEvent* apSubscribeEvent,AbstractMsgHandler* apMsgHandler):
	  m_pSubscribeEvent(apSubscribeEvent),m_pMsgHandler(apMsgHandler)
	  {

	  }
	void SendMsg(char* apMsg)
	{
		char* lpAnswer = m_pSubscribeEvent->Event(apMsg);
		m_pMsgHandler->Handler(lpAnswer);
	}
private:
	AbstractEvent* m_pSubscribeEvent;
	AbstractMsgHandler* m_pMsgHandler;
};

class ConcreteEvent:public AbstractEvent
{
public:
	char* Event(char* apMsg)
	{
		return apMsg;
	}
};

class ConcreteMsgHandler:public AbstractMsgHandler
{
	void Handler(char* apAnswer)
	{
		std::cout << apAnswer << std::endl;
	}
};
int main()
{
	std::auto_ptr<AbstractEvent> lpAbstractEvent(new ConcreteEvent());
	std::auto_ptr<AbstractMsgHandler> lpAbstractMsgHandler(new ConcreteMsgHandler());
	std::auto_ptr<MsgSender> lpMsgSender(new MsgSender(lpAbstractEvent.get(),lpAbstractMsgHandler.get()));
	char* lpMsg = "123";
	lpMsgSender->SendMsg(lpMsg);
	return 0;
}

欢迎大家提建议。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值