1.Observer模式要解决的问题为:建立一个一(Subject)对多(Observer)的依赖关系,并且做到当“一”变化的时候,依赖这个“一”的多也能够同步改变.
2.Observer Pattern 结构图
注:这里的目标Subject提供依赖于它的观察者Observer的注册(Attach)和注销(Detach)操作,并且提供了使得依赖于它的所有观察者同步的操作(Notify)。观察者Observer则提供一个Update操作,注意这里的Observer的Update操作并不在Observer改变了Subject目标状态的时候就对自己进行更新,这个更新操作要延迟到Subject对象发出Notify通知所有Observer进行修改(调用Update)。
3.实现
1 #ifndef _SUBJECT_H_ 2 #define _SUBJECT_H_ 3 4 #include <list> 5 #include <string> 6 using namespace std; 7 8 typedef string State; 9 class Observer; 10 11 class Subject 12 { 13 public: 14 virtual ~Subject(); 15 virtual void Attach(Observer* obv); 16 virtual void Detach(Observer* obv); 17 virtual void Notify(); 18 virtual void SetState(const State& st) = 0; 19 virtual State GetState() = 0; 20 protected: 21 Subject(); 22 private: 23 list<Observer* >* _obvs; 24 }; 25 26 class ConcreteSubject:public Subject 27 { 28 public: 29 ConcreteSubject(); 30 ~ConcreteSubject(); 31 State GetState(); 32 void SetState(const State& st); 33 protected: 34 private: 35 State _st; 36 }; 37 38 #endif
1 #include "Subject.h" 2 #include "Observer.h" 3 #include <iostream> 4 #include <list> 5 6 using namespace std; 7 typedef string state; 8 9 Subject::Subject() 10 { 11 //****在模板的使用之前一定要new,创建 12 _obvs = new list<Observer*>; 13 } 14 Subject::~Subject() 15 { 16 17 } 18 void Subject::Attach(Observer* obv) 19 { 20 _obvs->push_front(obv); 21 } 22 void Subject::Detach(Observer* obv) 23 { 24 if (obv != NULL) 25 _obvs->remove(obv); 26 } 27 void Subject::Notify() 28 { 29 list<Observer*>::iterator it; 30 it = _obvs->begin(); 31 for (;it != _obvs->end();it++) 32 { //关于模板和iterator的用法 33 (*it)->Update(this); 34 } 35 } 36 ConcreteSubject::ConcreteSubject() 37 { 38 _st = '\0'; 39 } 40 ConcreteSubject::~ConcreteSubject() 41 { 42 43 } 44 State ConcreteSubject::GetState() 45 { 46 return _st; 47 } 48 void ConcreteSubject::SetState(const State& st) 49 { 50 _st = st; 51 }
1 #ifndef _OBSERVER_H_ 2 #define _OBSERVER_H_ 3 4 #include "Subject.h" 5 #include <string> 6 7 using namespace std; 8 typedef string State; 9 10 class Observer 11 { 12 public: 13 virtual ~Observer(); 14 virtual void Update(Subject* sub) = 0; 15 virtual void PrintInfo() = 0; 16 protected: 17 Observer(); 18 State _st; 19 private: 20 }; 21 22 class ConcreteObserverA:public Observer 23 { 24 public: 25 virtual Subject* GetSubject(); 26 ConcreteObserverA(Subject* sub); 27 virtual ~ConcreteObserverA(); 28 29 //传入Subject作为参数,这样可以让一个View属于多个的Subject。 30 void Update(Subject* sub); 31 32 void PrintInfo(); 33 protected: 34 private: 35 Subject* _sub; 36 }; 37 38 class ConcreteObserverB:public Observer 39 { 40 public: 41 virtual Subject* GetSubject(); 42 ConcreteObserverB(Subject* sub); 43 virtual ~ConcreteObserverB(); 44 45 //传入Subject作为参数,这样可以让一个View属于多个的Subject。 46 void Update(Subject* sub); 47 48 void PrintInfo(); 49 protected: 50 private: 51 Subject* _sub; 52 }; 53 54 #endif
1 #include "Observer.h" 2 #include "Subject.h" 3 #include <iostream> 4 #include <string> 5 using namespace std; 6 7 Observer::Observer() 8 { 9 _st = '\0'; 10 } 11 Observer::~Observer() 12 { 13 14 } 15 ConcreteObserverA::ConcreteObserverA(Subject* sub) 16 { 17 _sub = sub; 18 _sub->Attach(this); 19 } 20 ConcreteObserverA::~ConcreteObserverA() 21 { 22 _sub->Detach(this); 23 if (_sub != 0) 24 { 25 delete _sub; 26 } 27 } 28 Subject* ConcreteObserverA::GetSubject() 29 { 30 return _sub; 31 } 32 void ConcreteObserverA::PrintInfo() 33 { 34 cout<<"ConcreteObserverA observer.... "<<_sub->GetState()<<endl; 35 } 36 void ConcreteObserverA::Update(Subject* sub) 37 { 38 _st = sub->GetState(); 39 PrintInfo(); 40 } 41 ConcreteObserverB::ConcreteObserverB(Subject* sub) 42 { 43 _sub = sub; 44 _sub->Attach(this); 45 } 46 ConcreteObserverB::~ConcreteObserverB() 47 { 48 _sub->Detach(this); 49 if (_sub != 0) 50 { 51 delete _sub; 52 } 53 } 54 Subject* ConcreteObserverB::GetSubject() 55 { 56 return _sub; 57 } 58 void ConcreteObserverB::PrintInfo() 59 { 60 cout<<"ConcreteObserverB observer.... "<<_sub->GetState()<<endl; 61 } 62 void ConcreteObserverB::Update(Subject* sub) 63 { 64 _st = sub->GetState(); 65 PrintInfo(); 66 }
1 #include "Subject.h" 2 #include "Observer.h" 3 #include <iostream> 4 using namespace std; 5 6 int main(int argc,char* argv[]) 7 { 8 ConcreteSubject* sub = new ConcreteSubject(); 9 Observer* o1 = new ConcreteObserverA(sub); 10 Observer* o2 = new ConcreteObserverB(sub); 11 sub->SetState("old"); 12 sub->Notify(); 13 sub->SetState("new"); //也可以由Observer调用 14 sub->Notify(); 15 return 0; 16 }