观察者模式(Observer Pattern)
Posted s-lisheng
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了观察者模式(Observer Pattern)相关的知识,希望对你有一定的参考价值。
观察者模式(Observer Pattern) —— 定义对象间的一种一对多的依赖关系,以便当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并自动刷新。
观察者模式可以理解为发布-订阅模式,即多个订阅者(观察者)向发布者(被观察者)订阅状态信息,当发布者更新状态时会将状态信息向它的订阅者发布信息。发布者需要自己维护订阅者列表,可以注册或者注销对状态信息感兴趣或不感兴趣的订阅者。
// subject.h
#include<vector>
#include<string>
using namespace std;
typedef int State;
class Observer;
// 被观察者(发布者)抽象
class Subject
public:
Subject()
virtual ~Subject()
virtual void Attach(Observer* obv); // 注册观察者
virtual void Detach(Observer* obv); // 注销观察者
virtual void Notify(); // 通知观察者
virtual void SetState(const State& st) = 0;
virtual State GetState() = 0;
private:
//观察者列表
vector<Observer*> m_pObs;
;
// 被观察者(实际发布者)
class ConcreteSubject: public Subject
public:
ConcreteSubject()
~ConcreteSubject()
State GetState();
void SetState(const State& st);
private:
State m_state;
;
// subject.cpp
#include "subject.h"
#include "observer.h"
void Subject::Attach(Observer* pObs)
m_pObs.push_back(pObs);
void Subject::Detach(Observer* pObs)
vector<Observer*>::iterator it = m_pObs.begin();
for ( ;it!=m_pObs.end();++it)
if (*it == pObs)
m_pObs.erase(it);
return;
void Subject::Notify()
vector<Observer*>::iterator it = m_pObs.begin();
for ( ; it!=m_pObs.end(); ++it)
(*it)->update(this);
void ConcreteSubject::SetState(const State& st)
m_state = st;
State ConcreteSubject::GetState()
return m_state;
// observer.h
#include<iostream>
#include<string>
#include<vector>
using namespace std;
typedef int State;
class Subject;
// 观察者抽象
class Observer
public:
Observer()
virtual ~Observer()
virtual void update(Subject* sub) = 0;
virtual void outputState();
protected:
string m_name;
State m_observerState;
;
// 实际观察者A
class ConcreteObserverA: public Observer
public:
ConcreteObserverA(string name, State init_state)
m_name = name;
m_observerState = init_state;
~ConcreteObserverA()
void update(Subject* sub);
private:
// Subject* m_sub;
;
// 实际观察者B
class ConcreteObserverB: public Observer
public:
ConcreteObserverB(string name, State init_state)
m_name = name;
m_observerState = init_state;
~ConcreteObserverB()
void update(Subject* sub);
private:
// Subject* m_sub;
;
// observer.cpp
#include "observer.h"
#include "subject.h"
void Observer::outputState()
cout<<m_name<<" update state :"<<m_observerState<<endl;
void ConcreteObserverA::update(Subject* sub)
m_observerState = sub->GetState();
outputState();
void ConcreteObserverB::update(Subject* sub)
m_observerState = sub->GetState();
outputState();
// main.cpp
#include "observer.h"
#include "subject.h"
int main()
ConcreteSubject* subject = new ConcreteSubject();
Observer* obA = new ConcreteObserverA("observerA", -1);
Observer* obB = new ConcreteObserverB("observerB", -1);
subject->Attach(obA);
subject->Attach(obB);
subject->SetState(0);
subject->Notify();
subject->Detach(obA);
subject->SetState(1);
subject->Notify();
delete obA;
delete obB;
delete subject;
return 0;
// 输出结果:
//observerA update state :0
//observerB update state :0
//observerB update state :1
以上是关于观察者模式(Observer Pattern)的主要内容,如果未能解决你的问题,请参考以下文章