悲剧,具体把多态用在数组上

有码有真相:

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


class AbstractEvent
{
public:
	virtual char* Event(char* apMsg)=0;
};
class AbstractMsgHandler
{
public:
	virtual void Handler(char* apAnswer)=0;
};
class AbstractEventPool
{
public:
	virtual void Register(AbstractEvent* apAbstractEvent)=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;
	}
};

typedef map<int,AbstractEvent*> EventMap;

class ConcreteEventPool:public AbstractEventPool
{
public:
	void Register(const int& aEventEnum,AbstractEvent* apAbstractEvent)
	{
		EventMap::iterator lIter = m_EventMap.find(aEventEnum);
		if (m_EventMap.end() != lIter)
		{
			m_EventMap.insert(std::make_pair(aEventEnum,apAbstractEvent));
		}
	}
private:
	EventMap m_EventMap;
};


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 = "Hello World!";
	lpMsgSender->SendMsg(lpMsg);
	return 0;
}

oh,no,我怎么相处这样的代码。。。。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值