作用:
定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新.
UML结构图:
![](http://teddyma.cnblogs.com/images/cnblogs_com/teddyma/gof38.JPG)
解析:
Observer模式定义的是一种一对多的关系,这里的一就是图中的Subject类,而多则是Obesrver类,当Subject类的状态发生变化的时候通知与之对应的Obesrver类们也去相应的更新状态,同时支持动态的添加和删除Observer对象的功能.Obesrver模式的实现要点是,第一一般subject类都是采用链表等容器来存放Observer对象,第二抽取出Observer对象的一些公共的属性形成Observer基类,而Subject中保存的则是Observer类对象的指针,这样就使Subject和具体的Observer实现了解耦,也就是Subject不需要去关心到底是哪个Observer对放进了自己的容器中.生活中有很多例子可以看做是Observer模式的运用,比方说,一个班有一个班主任(Subject),他管理手下的一帮学生(Observer),当班里有一些事情发生需要通知学生的时候,班主任要做的不是逐个学生挨个的通知而是把学生召集起来一起通知,实现了班主任和具体学生的关系解耦.
实现:
1)Observer.h
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//********************************************************************
created: 2006/07/20
filename: Observer.h
author: 李创
http://www.cppblog.com/converse/
![](/Images/OutliningIndicators/InBlock.gif)
purpose: Observer模式的演示代码
*********************************************************************/
![](/Images/OutliningIndicators/None.gif)
#ifndef OBSERVER_H
#define OBSERVER_H
![](/Images/OutliningIndicators/None.gif)
#include <list>
![](/Images/OutliningIndicators/None.gif)
typedef int STATE;
![](/Images/OutliningIndicators/None.gif)
class Observer;
![](/Images/OutliningIndicators/None.gif)
// Subject抽象基类,只需要知道Observer基类的声明就可以了
class Subject
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
public:
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Subject() : m_nSubjectState(-1)
{}
virtual ~Subject();
![](/Images/OutliningIndicators/InBlock.gif)
void Notify(); // 通知对象改变状态
void Attach(Observer *pObserver); // 新增对象
void Detach(Observer *pObserver); // 删除对象
![](/Images/OutliningIndicators/InBlock.gif)
// 虚函数,提供默认的实现,派生类可以自己实现来覆盖基类的实现
virtual void SetState(STATE nState); // 设置状态
virtual STATE GetState(); // 得到状态
![](/Images/OutliningIndicators/InBlock.gif)
protected:
STATE m_nSubjectState; // 模拟保存Subject状态的变量
std::list<Observer*> m_ListObserver; // 保存Observer指针的链表
};
![](/Images/OutliningIndicators/None.gif)
// Observer抽象基类
class Observer
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
public:
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Observer() : m_nObserverState(-1)
{}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
virtual ~Observer()
{}
![](/Images/OutliningIndicators/InBlock.gif)
// 纯虚函数,各个派生类可能有不同的实现
// 通知Observer状态发生了变化
virtual void Update(Subject* pSubject) = 0;
![](/Images/OutliningIndicators/InBlock.gif)
protected:
STATE m_nObserverState; // 模拟保存Observer状态的变量
};
![](/Images/OutliningIndicators/None.gif)
// ConcreateSubject类,派生在Subject类
class ConcreateSubject
: public Subject
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
public:
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
ConcreateSubject() : Subject()
{}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
virtual ~ConcreateSubject()
{}
![](/Images/OutliningIndicators/InBlock.gif)
// 派生类自己实现来覆盖基类的实现
virtual void SetState(STATE nState); // 设置状态
virtual STATE GetState(); // 得到状态
![](/Images/OutliningIndicators/InBlock.gif)
};
![](/Images/OutliningIndicators/None.gif)
// ConcreateObserver类派生自Observer
class ConcreateObserver
: public Observer
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
public:
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
ConcreateObserver() : Observer()
{}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
virtual ~ConcreateObserver()
{}
![](/Images/OutliningIndicators/InBlock.gif)
// 虚函数,实现基类提供的接口
virtual void Update(Subject* pSubject);
};
![](/Images/OutliningIndicators/None.gif)
#endif
![](/Images/OutliningIndicators/None.gif)
2)Observer.cpp
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//********************************************************************
created: 2006/07/20
filename: Observer.cpp
author: 李创
http://www.cppblog.com/converse/
![](/Images/OutliningIndicators/InBlock.gif)
purpose: Observer模式的演示代码
*********************************************************************/
![](/Images/OutliningIndicators/None.gif)
#include "Observer.h"
#include <iostream>
#include <algorithm>
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//* --------------------------------------------------------------------
| Subject类成员函数的实现
|
----------------------------------------------------------------------*/
![](/Images/OutliningIndicators/None.gif)
void Subject::Attach(Observer *pObserver)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
std::cout << "Attach an Observer\n";
![](/Images/OutliningIndicators/InBlock.gif)
m_ListObserver.push_back(pObserver);
}
![](/Images/OutliningIndicators/None.gif)
void Subject::Detach(Observer *pObserver)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
std::list<Observer*>::iterator iter;
iter = std::find(m_ListObserver.begin(), m_ListObserver.end(), pObserver);
![](/Images/OutliningIndicators/InBlock.gif)
if (m_ListObserver.end() != iter)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
m_ListObserver.erase(iter);
}
![](/Images/OutliningIndicators/InBlock.gif)
std::cout << "Detach an Observer\n";
}
![](/Images/OutliningIndicators/None.gif)
void Subject::Notify()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
std::cout << "Notify Observers's State\n";
![](/Images/OutliningIndicators/InBlock.gif)
std::list<Observer*>::iterator iter1, iter2;
![](/Images/OutliningIndicators/InBlock.gif)
for (iter1 = m_ListObserver.begin(), iter2 = m_ListObserver.end();
iter1 != iter2;
++iter1)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
(*iter1)->Update(this);
}
}
![](/Images/OutliningIndicators/None.gif)
void Subject::SetState(STATE nState)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
std::cout << "SetState By Subject\n";
m_nSubjectState = nState;
}
![](/Images/OutliningIndicators/None.gif)
STATE Subject::GetState()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
std::cout << "GetState By Subject\n";
return m_nSubjectState;
}
![](/Images/OutliningIndicators/None.gif)
Subject::~Subject()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
std::list<Observer*>::iterator iter1, iter2, temp;
![](/Images/OutliningIndicators/InBlock.gif)
for (iter1 = m_ListObserver.begin(), iter2 = m_ListObserver.end();
iter1 != iter2;
)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
temp = iter1;
++iter1;
delete (*temp);
}
![](/Images/OutliningIndicators/InBlock.gif)
m_ListObserver.clear();
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//* --------------------------------------------------------------------
| ConcreateSubject类成员函数的实现
|
----------------------------------------------------------------------*/
void ConcreateSubject::SetState(STATE nState)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
std::cout << "SetState By ConcreateSubject\n";
m_nSubjectState = nState;
}
![](/Images/OutliningIndicators/None.gif)
STATE ConcreateSubject::GetState()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
std::cout << "GetState By ConcreateSubject\n";
return m_nSubjectState;
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//* --------------------------------------------------------------------
| ConcreateObserver类成员函数的实现
|
----------------------------------------------------------------------*/
void ConcreateObserver::Update(Subject* pSubject)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
if (NULL == pSubject)
return;
![](/Images/OutliningIndicators/InBlock.gif)
m_nObserverState = pSubject->GetState();
![](/Images/OutliningIndicators/InBlock.gif)
std::cout << "The ObeserverState is " << m_nObserverState << std::endl;
}
![](/Images/OutliningIndicators/None.gif)
3)Main.cpp
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//********************************************************************
created: 2006/07/21
filename: Main.cpp
author: 李创
http://www.cppblog.com/converse/
![](/Images/OutliningIndicators/InBlock.gif)
purpose: Observer模式的测试代码
*********************************************************************/
![](/Images/OutliningIndicators/None.gif)
#include "Observer.h"
#include <iostream>
![](/Images/OutliningIndicators/None.gif)
int main()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
Observer *p1 = new ConcreateObserver;
Observer *p2 = new ConcreateObserver;
![](/Images/OutliningIndicators/InBlock.gif)
Subject* p = new ConcreateSubject;
p->Attach(p1);
p->Attach(p2);
p->SetState(4);
p->Notify();
![](/Images/OutliningIndicators/InBlock.gif)
p->Detach(p1);
p->SetState(10);
p->Notify();
![](/Images/OutliningIndicators/InBlock.gif)
delete p;
![](/Images/OutliningIndicators/InBlock.gif)
system("pause");
![](/Images/OutliningIndicators/InBlock.gif)
return 0;
}
![](/Images/OutliningIndicators/None.gif)