Description: This paper is the code. The main contents include the following three points.
First, the test function call
Second, the header file (.h) content
Third, the source code (.cpp) content
text
First, the test function call method
#include " observer.h " include headers // int main () { testObeserver (); // call the function is the main function test }
Test functions as follows:
void testObeserver () { the Sound * m = new new Music (); // add a song as the viewer The Listener * = zhoujielun new new Funs ( " zhoujielun " ); // add four fans, as an observer The Listener * = caiyilin new new Funs ( " caiyilin " ); The Listener * = dongxiaojie new new Funs ( " dongxiaojie " ); The Listener * = kunling new new Funs ( " kunling " ); m -> the addListener (zhoujielun); // Register the observer m ->the addListener (dongxiaojie); m -> the addListener (caiyilin); m -> the addListener (kunling); m -> makeNoise ( " Hanada Rose " ); // broadcast viewer is issued Delete m; m = nullptr a; }
Results of the:
Second, the header file (.h) content
. 1 #pragma Once 2 #include " util.h " . 3 #include <List> . 4 . 5 / * observation mode 6 define dependencies between the subject an-many, when a state of the object changes, 7 all it depends on the object to be notified and updated automatically. . 8 . 9 * / 10 . 11 12 is /////////////////////////////////////////// ///////////////////////////// // 13 // is observer (Sound), a viewer (listener) 14 15 16 . 17 class Listener; // declare in advance class 18 19 // the observed abstract class 20 classThe Sound 21 is { 22 is public : 23 is the Sound (); 24 Virtual ~ the Sound (); 25 Virtual void the addListener (The Listener listener *) = 0 ; // registered observers 26 is Virtual void removeListener (The Listener listener *) = 0 ; 27 Virtual void makeNoise ( const String & Noise) = 0 ; // the broadcast message viewer 28 29 }; 30 31 is // is observer particular class 32 class Music:public Sound 33 { 34 public: 35 Music(); 36 ~Music(); 37 void addListener(Listener* listener) override; 38 void removeListener(Listener* listener) override; 39 void makeNoise(const string& noise) override; 40 41 private: 42 list<shared_ptr<Listener>> _listenerlist; 43 }; 44 45 //Abstract viewer 46 is class The Listener 47 { 48 public : 49 The Listener (); 50 Virtual ~ The Listener (); 51 is Virtual void the listen ( const String & Noise) = 0 ; // receiving a broadcast message viewer 52 is Virtual void setListenerName ( const String & name) = 0 ; 53 is Virtual const String & name () = 0 ; 54 is 55 }; 56 is 57 is //观察者具体类 58 class Funs :public Listener 59 { 60 public: 61 Funs(); 62 Funs(const string& name); 63 void listen(const string& noise) override; 64 void setListenerName(const string& name) override; 65 const string& name() override; 66 67 private: 68 String the _name; 69 }; 70 71 is // test 72 void testObeserver ();
Third, the source code (.cpp) content
#include "pch.h" #include "observer.h" Sound::Sound(){} Sound::~Sound(){} Music::Music(){} Music::~Music() { _listenerlist.clear(); } void Music::addListener(Listener * listener) { shared_ptr<Listener> temp(listener); auto iter = find(_listenerlist.begin(), _listenerlist.end(), temp); if (iter == _listenerlist.end()) { _listenerlist.push_front(temp); } else { print("listener already exists"); } } void Music::removeListener(Listener * listener) { auto iter = _listenerlist.begin(); for (;iter!=_listenerlist.end();iter++) { if ((*iter).get()==listener) { _listenerlist.erase(iter); return; } } } void Music::makeNoise(const string& noise) { //notify listeners auto iter = _listenerlist.begin(); for (;_listenerlist.end()!=iter;iter++) { (*iter).get()->listen(noise); } } Listener::Listener(){} Listener::~Listener(){} Funs::Funs():_name("unknown"){} Funs::Funs(const string & name):_name(name){} void Funs::listen(const string & noise) { print(this->name().data()); print("listened "); print(noise.data()); } void Funs::setListenerName(const string & name) { _name = name; } const string & Funs::name() { return _name; } void testObeserver() { Sound* m = new Music(); Listener* zhoujielun = new Funs("zhoujielun"); Listener* caiyilin = new Funs("caiyilin"); Listener* dongxiaojie = new Funs("dongxiaojie"); Listener* kunling = new Funs("kunling"); m->addListener(zhoujielun); m->addListener(dongxiaojie); m->addListener(caiyilin); m->addListener(kunling); m->makeNoise("花田玫瑰"); delete m; m = nullptr; }