设计模式学习——观察者模式(Observer Pattern)

Posted 曾经时光

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了设计模式学习——观察者模式(Observer Pattern)相关的知识,希望对你有一定的参考价值。

有个报社,在有新报纸的时候,会给所有的订阅者发送推送。

 

 1  ///
 2  /// @file    Observer.h
 3  /// @author  marrs(chenchengxi993@gmail.com)
 4  /// @date    2017-08-20 19:54:38
 5  ///
 6 
 7 #ifndef __OBSERVER_H__
 8 #define __OBSERVER_H__
 9 
10 #include <iostream>
11  
12 namespace marrs{
13  
14 using std::cout;
15 using std::endl;
16 using std::string;
17 
18 class Observer
19 {
20     public:
21         virtual ~Observer(){}
22     public:
23         virtual void Update(string & str_message) = 0;
24     public:
25         virtual string GetName() = 0;
26         virtual string GetMsg() = 0;
27     protected:
28         string _str_name;
29         string _str_message;
30 };
31 
32 }
33 
34 #endif // __OBSERVER_H__
 1  ///
 2  /// @file    Observerable.h
 3  /// @author  marrs(chenchengxi993@gmail.com)
 4  /// @date    2017-08-20 19:48:42
 5  ///
 6 
 7 #ifndef __OBSERVERABLE_H__
 8 #define __OBSERVERABLE_H__
 9 
10 #include <iostream>
11 #include <vector>
12 
13 namespace marrs{
14  
15 using std::cout;
16 using std::endl;
17 using std::string; 
18 using std::vector;
19 
20 class Observer;
21 typedef vector<Observer *> ObserverCache;
22 
23 class Observerable
24 {
25     public:
26         virtual ~Observerable(){}
27 
28     public:
29         virtual void AddObserver(Observer * observer) = 0;
30         virtual void DelObserver(Observer * observer) = 0;
31         virtual void NotifyObserver(string str_message) = 0;
32     protected:
33         ObserverCache _observer;
34 };
35  
36 }
37 
38 #endif // __OBSERVERABLE_H__

 

 

 1  ///
 2  /// @file    NewspaperOffice.h
 3  /// @author  marrs(chenchengxi993@gmail.com)
 4  /// @date    2017-08-20 20:07:02
 5  ///
 6 
 7 #ifndef __NEWSPAPEROFFICE_H__
 8 #define __NEWSPAPEROFFICE_H__
 9 
10 #include "Observerable.h"
11  
12 namespace marrs{
13  
14 class NewspaperOffice
15 : public Observerable
16 {
17     public:
18         void AddObserver(Observer * observer);
19         void DelObserver(Observer * observer);
20         void NotifyObserver(string str_message);
21 };
22  
23 }
24 
25 #endif // __NEWSPAPEROFFICE_H__
 1  ///
 2  /// @file    NewspaperOffice.cc
 3  /// @author  marrs(chenchengxi993@gmail.com)
 4  /// @date    2017-08-20 20:19:15
 5  ///
 6  
 7 #include "NewspaperOffice.h"
 8 #include "Observer.h"
 9 #include <algorithm>
10 
11 namespace marrs{
12 
13 void NewspaperOffice::AddObserver(Observer * observer)
14 {
15     cout << "add a new Reader" << endl;
16     cout << "name : " << observer->GetName() << endl;
17     _observer.push_back(observer);
18 }
19 
20 void NewspaperOffice::DelObserver(Observer * observer)
21 {
22     cout << "del a Reader" << endl;
23     cout << "name : " << observer->GetName() << endl;
24     _observer.erase(remove(_observer.begin(), _observer.end(), observer));
25 }
26 
27 void NewspaperOffice::NotifyObserver(string str_message)
28 {
29     ObserverCache::iterator it_Observer;
30     it_Observer = _observer.begin();
31     for(; it_Observer != _observer.end(); ++it_Observer)
32     {
33         (*it_Observer)->Update(str_message);
34     }
35 }
36  
37 }

 

 

 1  ///
 2  /// @file    Readers.h
 3  /// @author  marrs(chenchengxi993@gmail.com)
 4  /// @date    2017-08-20 20:43:46
 5  ///
 6 
 7 #ifndef __READERS_H__
 8 #define __READERS_H__
 9 
10 #include "Observer.h"
11  
12 namespace marrs{
13  
14 class Readers
15 : public Observer
16 {
17     public:
18         Readers(string str_name);
19     public:
20         void Update(string & str_message);
21         string GetName();
22         string GetMsg();
23 };
24 
25 }
26 
27 #endif // __READERS_H__
 1  ///
 2  /// @file    Readers.cc
 3  /// @author  marrs(chenchengxi993@gmail.com)
 4  /// @date    2017-08-20 20:46:08
 5  ///
 6  
 7 #include "Readers.h"
 8 
 9 namespace marrs{
10 
11 Readers::Readers(string str_name)
12 {
13     _str_name = str_name;
14     _str_message = "None";
15 }
16 
17 void Readers::Update(string & str_message)
18 {
19     cout << "my name : " << _str_name << endl;
20     cout << "recv msg: " << str_message << endl;
21     _str_message = str_message;
22 }
23 
24 string Readers::GetName()
25 {
26     return _str_name;
27 }
28 
29 string Readers::GetMsg()
30 {
31     return _str_message;
32 }
33  
34 }

 

 

 1  ///
 2  /// @file    Main.cc
 3  /// @author  marrs(chenchengxi993@gmail.com)
 4  /// @date    2017-08-20 20:50:17
 5  ///
 6  
 7 #include "Readers.h"
 8 #include "NewspaperOffice.h"
 9 
10 using namespace marrs;
11 
12 int main()
13 {
14     NewspaperOffice * newspaperoffice = new NewspaperOffice;
15     Readers * reader1 = new Readers("zhang san");
16     Readers * reader2 = new Readers("li si");
17     Readers * reader3 = new Readers("wang wu");
18     Readers * reader4 = new Readers("zhao liu");
19 
20     newspaperoffice->AddObserver(reader1);
21     newspaperoffice->AddObserver(reader2);
22     newspaperoffice->NotifyObserver("first msg");
23     cout << "zhang san   msg: " << reader1->GetMsg() << endl;
24     cout << "li si       msg: " << reader2->GetMsg() << endl;
25     cout << "wang wu     msg: " << reader3->GetMsg() << endl;
26     cout << "zhao liu    msg: " << reader4->GetMsg() << endl;
27 
28 
29     newspaperoffice->AddObserver(reader3);
30     newspaperoffice->DelObserver(reader1);
31     newspaperoffice->NotifyObserver("second msg");
32     cout << "zhang san   msg: " << reader1->GetMsg() << endl;
33     cout << "li si       msg: " << reader2->GetMsg() << endl;
34     cout << "wang wu     msg: " << reader3->GetMsg() << endl;
35     cout << "zhao liu    msg: " << reader4->GetMsg() << endl;
36 
37 
38 
39     delete newspaperoffice;
40     delete reader1;
41     delete reader2;
42     delete reader3;
43     delete reader4;
44 
45     return 0;
46 }

 

 

[ccx@ubuntu ~/object-oriented/Observer_Pattern]$>g++ * -o main.exe
[ccx@ubuntu ~/object-oriented/Observer_Pattern]$>./main.exe 
add a new Reader
name : zhang san
add a new Reader
name : li si
my name : zhang san
recv msg: first msg
my name : li si
recv msg: first msg
zhang san   msg: first msg
li si       msg: first msg
wang wu     msg: None
zhao liu    msg: None
add a new Reader
name : wang wu
del a Reader
name : zhang san
my name : li si
recv msg: second msg
my name : wang wu
recv msg: second msg
zhang san   msg: first msg
li si       msg: second msg
wang wu     msg: second msg
zhao liu    msg: None

 

以上是关于设计模式学习——观察者模式(Observer Pattern)的主要内容,如果未能解决你的问题,请参考以下文章

C++设计模式学习笔记:观察者模式 Observer

尚硅谷设计模式学习(18)---[观察者模式( observer pattern)]

设计模式:观察者(Observer)模式

Carson带你学设计模式:观察者模式(Observer)

Carson带你学设计模式:观察者模式(Observer)

观察者模式(Observer)和发布(Publish/订阅模式(Subscribe)的区别