有码有真相:
#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,我怎么相处这样的代码。。。。。。