标签:c style class blog code java
Observer模式要解决的问题为:建立一个一(Subject)对多(Observer)的依赖关系,并且做到当“一”变化的时候,依赖这个“一”的多也能够同步改变。最常见的一个例子就是:对同一组数据进行统计分析时候,我们希望
能够提供多种形式的表示 (例如以表格进行统计显示、 柱状图统计显示、 百分比统计显示等) 。这些表示都依赖于同一组数据,我们当然需要当数据改变的时候,所有的统计的显示都能够同时改变。Observer模式就是解决了这一个问题。
1 /////////////Subject.h///////////////////////////////////////////////////////////// 2 #pragma once 3 #include <string> 4 #include <list> 5 using namespace std; 6 class Observer ; 7 8 typedef string State ; 9 class Subject 10 { 11 public: 12 virtual ~Subject(); 13 virtual void Attach(Observer* obv); 14 virtual void Detach(Observer* obv); 15 virtual void Notify(); 16 virtual void SetState(const State& st) = 0 ; 17 virtual State GetState() = 0 ; 18 protected: 19 Subject(); 20 21 private: 22 list<Observer*>* _Obvs ; 23 }; 24 25 class ConcreteSubject : public Subject 26 { 27 public: 28 ~ConcreteSubject(); 29 ConcreteSubject(); 30 State GetState(); 31 void SetState(const State& st) ; 32 protected: 33 private: 34 State _st ; 35 };
1 //////////////////Observer.h//////////////////////////////////////////////////////// 2 #pragma once 3 #include <string> 4 using namespace std; 5 typedef string State ; 6 class Subject ; 7 8 class Observer 9 { 10 public: 11 virtual ~Observer(); 12 virtual void Update(Subject* sub) = 0 ; 13 virtual void PrintInfo() = 0; 14 State _st ; 15 protected: 16 Observer(); 17 18 private: 19 }; 20 21 22 class ConcreteObserverA : public Observer 23 { 24 public: 25 ~ConcreteObserverA(); 26 ConcreteObserverA(Subject* sub); 27 virtual Subject* GetSubject(); 28 void Update(Subject* sub); 29 void PrintInfo(); 30 protected: 31 private: 32 Subject* _sub ; 33 }; 34 35 class ConcreteObserverB : public Observer 36 { 37 public: 38 ~ConcreteObserverB(); 39 ConcreteObserverB(Subject* sub); 40 virtual Subject* GetSubject(); 41 void Update(Subject* sub); 42 void PrintInfo(); 43 protected: 44 private: 45 Subject* _sub ; 46 };
1 ////////////Subject.cpp////////////////////////////////////////////////////////////// 2 #include "Subject.h" 3 #include "Observer.h" 4 #include <list> 5 typedef string State ; 6 7 8 Subject::Subject() 9 { 10 _Obvs = new list<Observer*>();//在模板的使用之前一定要new 11 } 12 Subject::~Subject() 13 { 14 15 } 16 17 void Subject::Attach(Observer* obv) 18 { 19 _Obvs->push_front(obv); 20 } 21 void Subject::Detach(Observer* obv) 22 { 23 if (obv != NULL) 24 { 25 _Obvs->remove(obv); 26 } 27 } 28 void Subject::Notify() 29 { 30 list<Observer*>::iterator it = _Obvs->begin(); 31 for (;it != _Obvs->end(); it++) 32 { 33 (*it)->Update(this); 34 } 35 } 36 37 ConcreteSubject::~ConcreteSubject() 38 { 39 40 } 41 ConcreteSubject::ConcreteSubject() 42 { 43 _st = ‘\0‘; 44 } 45 State ConcreteSubject::GetState() 46 { 47 return _st ; 48 } 49 void ConcreteSubject::SetState(const State& st) 50 { 51 _st = st ; 52 }
1 ////////////Observer.cpp////////////////////////////////////////////////////////////// 2 #include "Observer.h" 3 #include "Subject.h" 4 #include <iostream> 5 using namespace std ; 6 Observer::Observer() 7 { 8 _st = ‘\0‘; 9 } 10 Observer::~Observer() 11 { 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 != NULL) 24 { 25 delete _sub ; 26 } 27 } 28 29 Subject* ConcreteObserverA::GetSubject() 30 { 31 return _sub ; 32 } 33 void ConcreteObserverA::PrintInfo() 34 { 35 cout<<"观察者A的到的通知:"<<_sub->GetState()<<endl; 36 } 37 38 void ConcreteObserverA::Update(Subject* sub) 39 { 40 _st = sub->GetState(); 41 PrintInfo(); 42 } 43 44 45 ConcreteObserverB::ConcreteObserverB(Subject* sub) 46 { 47 _sub = sub ; 48 _sub->Attach(this); 49 } 50 ConcreteObserverB::~ConcreteObserverB() 51 { 52 _sub->Detach(this); 53 if (_sub != NULL) 54 { 55 delete _sub; 56 } 57 } 58 Subject* ConcreteObserverB::GetSubject() 59 { 60 return _sub ; 61 } 62 63 void ConcreteObserverB::PrintInfo() 64 { 65 cout<<"观察者B的到的通知:"<<_sub->GetState()<<endl; 66 } 67 void ConcreteObserverB::Update(Subject* sub) 68 { 69 _st = sub->GetState(); 70 PrintInfo(); 71 }
1 /////////////main.cpp///////////////////////////////////////////////////////////// 2 #include "Subject.h" 3 #include "Observer.h" 4 #include <iostream> 5 using namespace std; 6 7 int main() 8 { 9 Subject* sub = new ConcreteSubject(); 10 Observer* o1 = new ConcreteObserverA(sub); 11 Observer* o2 = new ConcreteObserverB(sub); 12 13 sub->SetState("你获得一封邮件!"); 14 sub->Notify(); 15 sub->SetState("你的评论有新的回复!"); 16 sub->Notify(); 17 18 19 getchar(); 20 return 0 ; 21 }
标签:c style class blog code java
原文地址:http://www.cnblogs.com/csxcode/p/3755935.html