观察者(Observer)模式,是常见的模式之一。比如一份报纸,有很多订户。订阅者并不知道报纸何时会送来,他只知道自己订了这份报纸。订阅者在这里担任着观察者的角色,而报社则是被观察者。 报纸一旦印刷完毕,就应该及时送到订户手中,如果将报社看作一个对象,则报纸便是观察者(订户)和被观察者(报社)之间的纽带。观察者需要维护一个与报纸相关的变量或函数,在这个具体问题中,这个变量就是订户是否收到报纸,可设置为一个布尔型,当收到时,订户需要更新这个变量。
Observer.h 文件如下:
<span style="color: rgb(75, 75, 75); font-family: georgia, verdana, Arial, helvetica, sans-seriff; font-size: 13px; line-height: 20.7999992370605px; background-color: rgb(255, 255, 0);"></span><pre name="code" class="cpp">#include <list>
//typedef int STATE;
using namespace std;
class Observer;
class Subject //Subject抽象基类,值需要知道Observer基类的声明就可以了
{
public:
Subject(): m_nSubjectState(-1){}
virtual ~Subject();
void Notify();//通知对象改变状态
void Attach(Observer *pObserver);//新增对象
void Detach(Observer *pObserver);//删除对象
//虚函数,提供默认的实现,派生类可以自己实现覆盖基类的实现
virtual void SetState(int nState);
virtual int GetState();//得到状态
protected:
int m_nSubjectState;//模拟保存Subject状态的变量
list<Observer*> m_ListObserver;//保存Observer指针链表
};
//Observer抽象基类
class Observer
{
public:
Observer():m_nObserverState(-1){}
virtual ~Observer(){};
//纯虚函数,各个派生类可能有不同的实现
//通知Observer状态发生了变化
virtual void Update(Subject* pSubject)=0;
protected:
int m_nObserverState;//模拟保存Observer状态的变量
};
//ConcreateSubject类,派生在Subject类
class ConcreateSubject: public Subject
{
public:
ConcreateSubject(): Subject(){}
virtual ~ConcreateSubject(){}
//派生类自己实现来覆盖基类的实现
virtual void SetState(int nState);//设置状态
virtual int GetState();//得到状态
};
//ConcreateObserver类派生自Observer
class ConcreateObserver: public Observer
{
public:
ConcreateObserver():Observer(){}
virtual ~ConcreateObserver(){}
//虚函数,实现基类提供的接口
virtual void Update(Subject* pSubject);
};
下面是Observer.cpp文件
#include "Observer.h"
#include <iostream>
#include <algorithm>
// | Subject类成员函数的实现 |
void Subject::Attach(Observer* pObserver)//订阅报纸
{
cout<<"Attach an Observer\n";
m_ListObserver.push_back(pObserver);
}
void Subject::Detach(Observer* pObserver)//取消订阅
{
list<Observer*>::iterator iter;
iter=find(m_ListObserver.begin(),m_ListObserver.end(),pObserver);
if(m_ListObserver.end()!=iter)
{
m_ListObserver.erase(iter);
}
cout<<"Detach an Observer\n";
}
void Subject::Notify()
{
cout<<"Notify Observers's State\n";
list<Observer*>::iterator iter1,iter2;
for(iter1 = m_ListObserver.begin(),iter2 = m_ListObserver.end();iter1!=iter2; ++iter1)
{
(*iter1)->Update(this);
}
}
void Subject::SetState(int nState)
{
cout<<"SetState By Subject\n";
m_nSubjectState=nState;
}
int Subject::GetState()
{
cout<<"GetState By Subject\n";
return m_nSubjectState;
}
Subject::~Subject()
{
list<Observer*>::iterator iter1,iter2,temp;
for(iter1=m_ListObserver.begin(),iter2=m_ListObserver.end();iter1!=iter2;)
{
temp=iter1;
++iter1;
delete(*temp);
}
m_ListObserver.clear();
}
/*--------------------------------------------
| ConcreateSubject类成员函数的实现|
----------------------------------------------*/
void ConcreateSubject::SetState(int nState)
{
cout<<"SetState By ConcreateSubject\n";
m_nSubjectState = nState;
}
int ConcreateSubject::GetState()
{
cout<<"GetState By ConcreateSubject\n";
return m_nSubjectState;
}
/*------------------------------------------
| ConcreateObserver类成员函数的实现|
-------------------------------------------*/
void ConcreateObserver::Update(Subject* pSubject)
{
if(NULL==pSubject)
return;
m_nObserverState= pSubject->GetState();
cout<<"The ObserverState is"<<m_nObserverState<<endl;
}
下面是main.cpp
#include "Observer.h"
#include <iostream>
int main()
{
Observer *p1=new ConcreateObserver;
Observer *p2=new ConcreateObserver;
Subject *p=new ConcreateSubject;
p->Attach(p1);
p->Attach(p2);
p->SetState(4);
p->Notify();
p->Detach(p1);
p->SetState(10);
p->Notify();
delete p;
return 0;
}
程序的运行结果:
/*
Attach an Observer
Attach an Observer
SetState By ConcreateSubject
Notify Observers's State
GetState By ConcreateSubject
The ObserverState is4
GetState By ConcreateSubject
The ObserverState is4
Detach an Observer
SetState By ConcreateSubject
Notify Observers's State
GetState By ConcreateSubject
The ObserverState is10
*/