The following is an example of the observer mode. The source code is from the design mode final solution-gof 23 design modes with C ++ implementation source code.
The source code has a bug in the structure of each observer object, which may lead to repeated deletion of the subject object.
// Subject. h # ifndef subject_h # define subject_h # include <list> # include <string> using namespace STD; typedef string state; Class observer; Class subject {public: Virtual ~ Subject (); Virtual void attach (Observer * OBV); // register virtual void detach (Observer * OBV); // cancel virtual void notify (); virtual void setstate (const State & St) = 0; virtual state getstate () = 0; protected: subject (); // constructor is protected? Subject is an abstract class private: List <Observer *> * _ obvs; // stores pointer, also known as publish-subscribe}; Class concretesubject: public subject {public: concretesubject ();~ Concretesubject (); State getstate (); void setstate (const State & St); protected: Private: State _ st ;};# endif //~ Subject_h
Subject. cpp
# Include "subject. H "# include" Observer. H "# include <iostream >#include <list> using namespace STD; typedef string state; Subject: subject () {// be sure to use new before using the template, create _ obvs = new list <Observer *>;} subject ::~ Subject () {// release the delete _ obvs list after use; _ obvs = NULL;} void subject: attach (Observer * OBV) {_ obvs-> push_front (OBV);} void subject: Detach (Observer * OBV) {If (OBV! = NULL) _ obvs-> remove (OBV);} void subject: Policy () {list <Observer * >:: iterator it; for (IT = _ obvs-> begin (); it! = _ Obvs-> end (); It ++) {// usage of the template and iterator (* It)-> Update (this) ;}} concretesubject :: concretesubject () {_ ST = '\ 0';} concretesubject ::~ Concretesubject () {} state concretesubject: getstate () {return _ st;} void concretesubject: setstate (const State & St) {_ ST = sT ;}
Observer. h
# Ifndef observer_h # define observer_h # include "subject. H" # include <string> using namespace STD; typedef string state; Class observer {public: Virtual ~ Observer (); Virtual void Update (subject * sub) = 0; virtual void printinfo () = 0; protected: Observer (); State _ st; private:}; Class concreteobservera: public observer {public: concreteobservera (subject * sub); Virtual ~ Concreteobservera (); Virtual subject * getsubject (); // input subject as the parameter, so that a view can belong to multiple subject. Void Update (subject * sub); void printinfo (); protected: Private: Subject * _ Sub;}; Class concreteobserverb: public observer {public: concreteobserverb (subject * sub ); virtual ~ Concreteobserverb (); Virtual subject * getsubject (); // input subject as the parameter, so that a view can belong to multiple subject. Void Update (subject * sub); void printinfo (); protected: Private: Subject * _ Sub;}; # endif //~ Observer_h
Observer. cpp
# Include "Observer. H "# include" subject. H "# include <iostream >#include <string> using namespace STD; Observer: Observer () {_ ST = '\ 0'; // The end of the string} observer: :~ Observer () {} concreteobservera: concreteobservera (subject * sub) {_ sub = sub; _ Sub-> attach (this); // register} concreteobservera ::~ Concreteobservera () {_ Sub-> detach (this); // logout // If (_ sub! = 0) // Delete _ Sub;} subject * concreteobservera: getsubject () {return _ Sub;} void concreteobservera: printinfo () {cout <"concreteobservera observer .... "<_ 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! = 0) // {// Delete _ Sub; //} subject * concreteobserverb: getsubject () {return _ Sub;} void concreteobserverb: printinfo () {cout <"concreteobserverb observer .... "<_ Sub-> getstate () <Endl;} void concreteobserverb: Update (subject * sub) {_ ST = sub-> getstate (); printinfo ();}
Main. cpp
// Main. CPP # include "subject. H "# include" Observer. H "# include <iostream> using namespace STD; int main (INT argc, char * argv []) {concretesubject * sub = new concretesubject (); observer * O1 = new concreteobservera (sub); Observer * O2 = new concreteobserverb (sub); sub-> setstate ("old"); sub-> Policy (); sub-> setstate ("new"); // you can also call sub-> Y (); Delete O2; O2 = NULL; Delete O1; O1 = NULL by the observer; delete sub; sub = NULL; System ("pause"); Return 0 ;}
Design Mode: Observer Mode