表示是原创啊,但是是在总结了各路大婶知识的基础上:
#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;
}
欢迎大家提建议。