observer center有多个subject 事件,observer可以向center同时订阅多个subject事件,
observer center内部维护observer的订阅记录,当subject 事件发生时,通知到订阅了该subject的observers。
#include <iostream>
#include <map>
#include <boost/thread.hpp>
class subject
{
public:
static const int readsub;
static const int writesub;
static const int closesub;
subject(){ m_sub = 0;}
int m_sub;
bool operator < ( const subject &rsub) const
{
return this->m_sub < rsub.m_sub;
}
};
//bool operator < (const subject &lsub, const subject &rsub)
//{
// return lsub.m_sub < rsub.m_sub;
//}
const int subject::readsub = 0;
const int subject::writesub = 1;
const int subject::closesub = 2;
class observer
{
public:
virtual void update(subject sub)
{
std::cout<<"observer recv subject update: "<<sub.m_sub<<"\n";
}
};
class observerAAA:public observer
{
public:
virtual void update(subject sub)
{
std::cout<<"observerAAA recv subject update: "<<sub.m_sub<<"\n";
}
};
class observerBBB:public observer
{
public:
virtual void update(subject sub)
{
std::cout<<"observerBBB recv subject update: "<<sub.m_sub<<"\n";
}
};
typedef boost::shared_ptr<observer> SPOBSERVER;
class observerCenter
{
public:
void registersubject(subject sub, SPOBSERVER x)
{
boost::mutex::scoped_lock lock(m_observercentermutex);
std::map<subject, LISTobserver>::iterator it = m_observercenter.find(sub);
if (it != m_observercenter.end())
{
it->second.push_back(x);
}
else
{
LISTobserver lo;
lo.push_back(x);
m_observercenter.insert(std::make_pair(sub, lo));
}
}
void update(subject sub)
{
LISTobserver temp;
{
boost::mutex::scoped_lock lock(m_observercentermutex);
std::map<subject, LISTobserver>::iterator it = m_observercenter.find(sub);
if (it != m_observercenter.end())
{
temp = it->second;
}
}
LISTobserver::iterator it = temp.begin();
for (; it != temp.end(); ++it)
{
(*it)->update(sub);
}
}
void unregistersubject(subject sub, SPOBSERVER x)
{
boost::mutex::scoped_lock lock(m_observercentermutex);
std::map<subject, LISTobserver>::iterator it = m_observercenter.find(sub);
if (it != m_observercenter.end())
{
it->second.erase(x);
}
}
private:
typedef std::list<SPOBSERVER> LISTobserver;
boost::mutex m_observercentermutex;
std::map<subject, LISTobserver> m_observercenter;
};
observerCenter g_observerCenter;
void eventProduce()
{
static int i1 = 0;
static int i2 = 0;
static int i3 = 0;
while (1)
{
if ((i1++ %1) == 0)
{
subject a;
a.m_sub = subject::readsub;
g_observerCenter.update(a);
}
if ((i2++ %2) == 0)
{
subject a;
a.m_sub = subject::writesub;
g_observerCenter.update(a);
}
if ((i3++ %3) == 0)
{
subject a;
a.m_sub = subject::closesub;
g_observerCenter.update(a);
}
boost::this_thread::sleep(boost::posix_time::seconds(1));
}
}
void addObserver()
{
subject a;
a.m_sub = subject::readsub;
subject b;
b.m_sub = subject::writesub;
subject c;
c.m_sub = subject::closesub;
//一个observer可能注册多个subject
SPOBSERVER sp(new observerAAA());
g_observerCenter.registersubject(a, sp);
g_observerCenter.registersubject(b, sp);
g_observerCenter.registersubject(c, sp);
SPOBSERVER sp1(new observerBBB());
g_observerCenter.registersubject(a, sp1);
g_observerCenter.registersubject(b, sp1);
SPOBSERVER sp2(new observerAAA());
g_observerCenter.registersubject(a, sp2);
}
int main()
{
boost::thread producerthread(boost::bind(eventProduce));
addObserver();
boost::this_thread::sleep(boost::posix_time::seconds(100));
}
posted on 2014-07-21 10:21
pizzx 阅读(283)
评论(0) 编辑 收藏 引用 所属分类:
c++/boost