//forwarder-recevier.h
#pragma once
// 设计模式: 转发器--接收器(Forwarder--Receiver)
//message
class Message
{
public:
Message();
Message(string strSender, string strData);
~Message();
private:
string m_strSender;
string m_strData;
};
//Entry
class Entry
{
public:
Entry(string strName) { m_strName = strName; }
~Entry(){}
private:
string m_strName;//标识唯一的一个值
};
//Registry
class Registry
{
public:
static Registry& Instance()
{
static Registry aRegistry;
return &aRegistry;
}
bool Put(const string& strKey, const Entry& aEntry);
bool Get(const string& strKey, Entry& aEntry) const;
private:
Registry() {};
~Registry() {};
private:
map<string, Entry> m_mapIdToEntry;
};
//Forwarder
class Forwarder
{
public:
Forwarder(string strName);
~Forwarder();
void SendMsg(string strDest, Message theMsg);
private:
string m_strName; //对等体的名字标识(strkey)
};
//Receiver
class Receiver
{
public:
Receiver(string strName);
~Receiver();
Message ReceiveMsg();
private:
string m_strName; //对等体的名字标识(strkey)
};
//对等体
class Server
{
Receiver r;
Forwarder f;
public:
Server(string strKeyR, string strKeyF);
~Server();
void Run();
};
////////////////////////////
//forwarder-recevier.cpp
#include "stdafx.h"
#include "forward_recevier.h"
//message
Message::Message()
{}
Message::Message(string strSender, string strData)
: m_strSender(strSender)
, m_strData(strData)
{}
Message::~Message()
{}
//Registry
bool Registry::Put(const string& strKey, const Entry& aEntry)
{
if(m_mapIdToEntry.find(strKey) == m_mapIdToEntry.end())
{
m_mapIdToEntry[strKey] = aEntry;
return true;
}
return false;
}
bool Registry::Get(const string& strKey, Entry& aEntry) const
{
if(m_mapIdToEntry.find(strKey) != m_mapIdToEntry.end())
{
aEntry = m_mapIdToEntry[strKey];
return true;
}
else
{
return false;
}
}
//Forwarder
Forwarder::Forwarder(string strName)
: m_strName(strName)
{}
Forwarder::~Forwarder()
{}
void Forwarder::SendMsg(string strDest, Message theMsg)
{
//marshal message;
//deliver message;
}
//Receiver
Receiver::Receiver(string strName)
: m_strName(strName)
{}
Receiver::~Receiver()
{}
Message Receiver::ReceiveMsg()
{
//recevie message;
//unmarshal message;
//return message;
}
//对等体
Server::Server(string strKeyR, string strKeyF)
{
r = Recevier(strKeyR);
f = Forwarder(strKeyF);
}
Server::~Server()
{
}
void Server::Run()
{
Message result = r.ReceiveMsg();
Message aMessage("Server", "I am alive");
f.SendMsg(result.m_strSender, msg);
}