#include <iostream>
#include <list>
using namespace std;
//
struct IObserver
{
public:
virtual ~IObserver() { }
virtual void Update() = 0;
};
class CObserver : public IObserver
{
public:
CObserver(string name) : m_name(name) { }
virtual ~CObserver() { }
virtual void Update() { cout<<m_name.c_str()<<" observer update."<<endl; }
private:
string m_name;
};
class CBlog
{
public:
void AddObserver(IObserver *pObserver) { m_pObserverList.push_back(pObserver); }
void RemoveObserver(IObserver *pObserver) { m_pObserverList.remove(pObserver); }
void Notify()
{
list<IObserver*>::iterator ite = m_pObserverList.begin();
for(; m_pObserverList.end() != ite; ite++)
{
(*ite)->Update();
}
}
private:
list<IObserver*> m_pObserverList;
};
//
void main()
{
CBlog blog;
CObserver observerA("AAA");
CObserver observerB("BBB");
blog.AddObserver(&observerA);
blog.AddObserver(&observerB);
blog.Notify();
}