原文链接:http://www.cppblog.com/eXile/archive/2007/09/16/32297.html
以前我曾经实现过观察者模式(signal / slot )。有位朋友不以为然,也把他的实现发给我。这是用纯OO的方式实现的,没有使用模板,不依赖于其它库。应该是仿Java或C#接口。设计得不错,具有以下特点:
1)当Subject或Observer 销毁时,连接自动断开(注册自动取消),当然,这也是实现该模式的重点。
2)考虑了以下因素:一个Subject是否允许多个Observer观察?一个Observer是否允许同时观察多个Subject? 由此可分为一对一,一对多,多对多,一般的GUI库中都是后两种情况,但自己写的程序中却第一种情况居多。所以他出于效率,设计了SimpleSubject和SimpleObserver。
不足之处如下:
1)Observer::update()只能带无类型的参数(好象也没有更好的办法)。
2)由于C++中没有匿名类,所以使用起来并没有Java中方便。于是我又添加了一个ObserverAdapter。
使用举例:
class A : public Subject
{
int value;
public:
void setValue(int v)
{
value = v;
notify(&value);
}
};
class B : public Observer
{
public:
void update(void* arg)
{
printf("A changed:%d", *(int*)arg);
}
};
void f(A* a, B* b)
{
a->connect(b);
a->setValue(1);
}
使用ObserverAdapter ,则变成了以下情况:
class B
{
ObserverAdapter<B> observerOfA;
public:
B(A* a) : observerOfA(this, &B::valueChanged)
{
a->connect(&observerOfA);
}
void valueChanged(void* arg)
{
printf("A changed:%d", *(int*)arg);
}
};
代码分为两个文件:IObserver.h 和 Observer.h,就贴在下面吧:
IObserver.h : 接口定义
#pragma once
class ISubject;
class IObserver
{
public:
IObserver() {}
virtual ~IObserver() {};
virtual void update(void*) = 0;
protected:
virtual void addSubject(ISubject* ) = 0;
virtual void removeSubject(ISubject* ) = 0;
friend class ISubject;
private:
IObserver(IObserver const&);
IObserver& operator= (IObserver const&);
};
class ISubject
{
public:
ISubject() {}
virtual ~ISubject() {};
virtual void connect(IObserver*) = 0;
virtual void disconnect(IObserver*) = 0;
virtual bool isConnected(IObserver*) const= 0;
virtual void notify(void*) = 0;
protected:
void addObserver(IObserver* observer);
void removeObserver(IObserver* observer);
private:
ISubject(ISubject const&);
ISubject& operator= (ISubject const&);
};
Observer.h : 具体实现
#pragma once
#include <cassert>
#include <set>
#include "IObserver.h"
//-------------------------------------------------------------------
inline void ISubject::addObserver(IObserver* observer)
{
observer->addSubject(this);
}
inline void ISubject::removeObserver(IObserver* observer)
{
observer->removeSubject(this);
}
//-------------------------------------------------------------------
class SimpleSubject : public ISubject
{
public:
SimpleSubject() : m_observer(0)
{
}
~SimpleSubject()
{
if (m_observer) removeObserver(m_observer);
}
virtual void connect(IObserver* observer)
{
assert(observer);
if (m_observer)
removeObserver(m_observer);
addObserver(observer);
m_observer = observer;
}
virtual void disconnect(IObserver* observer)
{
assert(observer && observer == m_observer);
removeObserver(m_observer);
m_observer = 0;
}
virtual bool isConnected(IObserver* observer) const
{
return observer == m_observer;
}
virtual void notify(void* arg)
{
if (m_observer) m_observer->update(arg);
}
private:
IObserver* m_observer;
};
//-------------------------------------------------------------------
class Subject : public ISubject
{
public:
Subject() : m_observers()
{
}
~Subject()
{
std::set<IObserver*>::iterator
it = m_observers.begin(),
e = m_observers.end();
for (; it != e; ++it)
{
removeObserver(*it);
}
}
virtual void connect(IObserver* observer)
{
assert(observer);
addObserver(observer);
m_observers.insert(observer);
}
virtual void disconnect(IObserver* observer)
{
assert(observer);
removeObserver(observer);
m_observers.erase(observer);
}
virtual bool isConnected(IObserver* observer) const
{
return m_observers.find(observer) != m_observers.end();
}
virtual void notify(void* arg)
{
std::set<IObserver*>::iterator
it = m_observers.begin(),
e = m_observers.end();
while (it != e)
{
(*it++)->update(arg); // observer can be disconnected in update()
}
}
private:
std::set<IObserver*> m_observers;
};
//-------------------------------------------------------------------
class SimpleObserver : public IObserver
{
public:
SimpleObserver() : m_subject(0)
{
}
~SimpleObserver()
{
if (m_subject) m_subject->disconnect(this);
}
ISubject* getSubject() const
{
return m_subject;
}
private:
virtual void addSubject(ISubject* subject)
{
if (m_subject) m_subject->disconnect(this);
m_subject = subject;
}
virtual void removeSubject(ISubject* subject)
{
assert(subject == m_subject);
m_subject = 0;
}
private:
ISubject* m_subject;
};
//-------------------------------------------------------------------
class Observer : public IObserver
{
public:
Observer() : m_subjects()
{
}
~Observer()
{
std::set<ISubject*>::iterator
it = m_subjects.begin(),
e = m_subjects.end();
while (it != e)
{
(*it++)->disconnect(this);
}
}
private:
virtual void addSubject(ISubject* subject)
{
assert(subject);
m_subjects.insert(subject);
}
virtual void removeSubject(ISubject* subject)
{
assert(subject);
m_subjects.erase(subject);
}
private:
std::set<ISubject*> m_subjects;
};
//-------------------------------------------------------------------
template <class T, class Base = SimpleObserver>
class ObserverAdapter : public Base
{
public:
ObserverAdapter(T* t, void (T::*f)(void*))
: m_obj(t), m_func(f)
{
}
virtual void update(void* arg)
{
(m_obj->*m_func)(arg);
}
private:
T* m_obj;
void (T::*m_func)(void*);
};
//-------------------------------------------------------------------