Observer模式要解决的问题为:建立一个一(Subject)对多(Observer)的依赖关系,并且做到当“一”变化的时候,依赖这个“一”的多也能够同步改变。最常见的一个例子就是:对同一组数据进行统计分析时候,我们希望
能够提供多种形式的表示 (例如以表格进行统计显示、 柱状图统计显示、 百分比统计显示等) 。这些表示都依赖于同一组数据,我们当然需要当数据改变的时候,所有的统计的显示都能够同时改变。Observer模式就是解决了这一个问题。
/////////////Subject.h/////////////////////////////////////////////////////////////
#pragma once
#include <string>
#include <list>
using namespace std;
class Observer ; typedef string State ;
class Subject
{
public:
virtual ~Subject();
virtual void Attach(Observer* obv);
virtual void Detach(Observer* obv);
virtual void Notify();
virtual void SetState(const State& st) = ;
virtual State GetState() = ;
protected:
Subject(); private:
list<Observer*>* _Obvs ;
}; class ConcreteSubject : public Subject
{
public:
~ConcreteSubject();
ConcreteSubject();
State GetState();
void SetState(const State& st) ;
protected:
private:
State _st ;
};
//////////////////Observer.h////////////////////////////////////////////////////////
#pragma once
#include <string>
using namespace std;
typedef string State ;
class Subject ; class Observer
{
public:
virtual ~Observer();
virtual void Update(Subject* sub) = ;
virtual void PrintInfo() = ;
State _st ;
protected:
Observer(); private:
}; class ConcreteObserverA : public Observer
{
public:
~ConcreteObserverA();
ConcreteObserverA(Subject* sub);
virtual Subject* GetSubject();
void Update(Subject* sub);
void PrintInfo();
protected:
private:
Subject* _sub ;
}; class ConcreteObserverB : public Observer
{
public:
~ConcreteObserverB();
ConcreteObserverB(Subject* sub);
virtual Subject* GetSubject();
void Update(Subject* sub);
void PrintInfo();
protected:
private:
Subject* _sub ;
};
////////////Subject.cpp//////////////////////////////////////////////////////////////
#include "Subject.h"
#include "Observer.h"
#include <list>
typedef string State ; Subject::Subject()
{
_Obvs = new list<Observer*>();//在模板的使用之前一定要new
}
Subject::~Subject()
{ } void Subject::Attach(Observer* obv)
{
_Obvs->push_front(obv);
}
void Subject::Detach(Observer* obv)
{
if (obv != NULL)
{
_Obvs->remove(obv);
}
}
void Subject::Notify()
{
list<Observer*>::iterator it = _Obvs->begin();
for (;it != _Obvs->end(); it++)
{
(*it)->Update(this);
}
} ConcreteSubject::~ConcreteSubject()
{ }
ConcreteSubject::ConcreteSubject()
{
_st = '\0';
}
State ConcreteSubject::GetState()
{
return _st ;
}
void ConcreteSubject::SetState(const State& st)
{
_st = st ;
}
////////////Observer.cpp//////////////////////////////////////////////////////////////
#include "Observer.h"
#include "Subject.h"
#include <iostream>
using namespace std ;
Observer::Observer()
{
_st = '\0';
}
Observer::~Observer()
{ } ConcreteObserverA::ConcreteObserverA(Subject* sub)
{
_sub = sub ;
_sub->Attach(this);
}
ConcreteObserverA::~ConcreteObserverA()
{
_sub->Detach(this);
if (_sub != NULL)
{
delete _sub ;
}
} Subject* ConcreteObserverA::GetSubject()
{
return _sub ;
}
void ConcreteObserverA::PrintInfo()
{
cout<<"观察者A的到的通知:"<<_sub->GetState()<<endl;
} void ConcreteObserverA::Update(Subject* sub)
{
_st = sub->GetState();
PrintInfo();
} ConcreteObserverB::ConcreteObserverB(Subject* sub)
{
_sub = sub ;
_sub->Attach(this);
}
ConcreteObserverB::~ConcreteObserverB()
{
_sub->Detach(this);
if (_sub != NULL)
{
delete _sub;
}
}
Subject* ConcreteObserverB::GetSubject()
{
return _sub ;
} void ConcreteObserverB::PrintInfo()
{
cout<<"观察者B的到的通知:"<<_sub->GetState()<<endl;
}
void ConcreteObserverB::Update(Subject* sub)
{
_st = sub->GetState();
PrintInfo();
}
/////////////main.cpp/////////////////////////////////////////////////////////////
#include "Subject.h"
#include "Observer.h"
#include <iostream>
using namespace std; int main()
{
Subject* sub = new ConcreteSubject();
Observer* o1 = new ConcreteObserverA(sub);
Observer* o2 = new ConcreteObserverB(sub); sub->SetState("你获得一封邮件!");
sub->Notify();
sub->SetState("你的评论有新的回复!");
sub->Notify(); getchar();
return ;
}