Design mode (10)--Viewer mode

Source: Internet
Author: User

10. Observer mode//ver1class Stockobserver; interdependence with Secretary; So in advance declare class secretary;//stock colleague Class stockobserver{private:string _name; Secretary * _ps;public:stockobserver (string name, Secretary *p) {_name = Name;_ps = P;} void Update () {//Get a notification from the front desk and take action quickly. _ps->getsecretaryaction (); must be shielded here; There is no way to compile the pass, Stockobserver and secretary are mutually coupled. The solution is to write in 2. h and. cpp files, respectively. In the. h file, declare the class to be referenced in advance and define the object as a pointer. }};//Front desk Secretary class Secretary{private:list<stockobserver *> mylist;string _action;public://add void Attach ( Stockobserver * psob) {mylist.push_back (PSOB);} reduce void Detach (Stockobserver * psob) {mylist.remove (PSOB);} notify void Notify () {list<stockobserver *>::iterator it = Mylist.begin (); for (; It! = Mylist.end (); ++it) {(*it)->u Pdate ();}} foreground state void Setsecretaryaction (string stract) {_action = stract;} String Getsecretaryaction () {return _action;}}; void Main1 () {secretary *ps1 = new Secretary (); Stockobserver *pso1 = new Stockobserver ("Fun", PS1); Stockobserver *PSO2 = new Stockobserver ("Test", PS1);//foreground note two colleagues Ps1-> Attach ((PSO1));p S1->attach ((PSO2));//The Boss came back, sent notice ps1->setsecretaryaction ("The Boss came Back");p s1->notify ();}

 

10. Observer mode//ver2//observer.h//pre-declaration class Subject; Class Boss;class Secretary;class observer{protected:string _name; Subject * _ps;public:observer (string name, Subject * PS), virtual ~observer (void);p ublic:virtual void Update ();};/ /See Stock colleague Class Stockobserver:public Observer{public:stockobserver (string name, Subject * PS); virtual ~stockobserver (); virtual void Update ();};/ /See NBA colleague Class Nbaobserver:public Observer{public:nbaobserver (string name, Subject * PS); virtual ~nbaobserver (); virtual void Update ();};/ /observer.cpp#include "Observer.h" #include "Secretary.h" Observer::observer (string name, Subject * PS) {_ps = Ps;_name = Name;} Observer::~observer (void) {}void observer::update () {_ps->getsecretaryaction ();} Stockobserver::stockobserver (string name, Subject * PS): Observer (name, PS) {}stockobserver::~stockobserver () {}void Stockobserver::update () {_ps->getsecretaryaction ();} Nbaobserver::nbaobserver (string name, Subject * PS): Observer (name, PS) {}nbaobserver: :~Nbaobserver () {}void nbaobserver::update () {_ps->getsecretaryaction ();} -----------------------------------//subject.h//Pre-declaration class Observer;class Stockobserver;class nbaobserver;// Notifier interface class subject{public:virtual void Attach (Observer *pob); virtual void Detach (Observer *pob); virtual void Notify (); void Setsecretaryaction (String stract); string getsecretaryaction ();};/ /Boss Notifier class Boss:public subject{private:list<observer*> mylist;string _action;public:void Attach (Observer *pob void Detach (Observer *pob); void Notify (); void Setsecretaryaction (String stract); string getsecretaryaction ();}; Class secretary{private:list<observer*> mylist;string _action;public://add void Attach (Observer * pob);//decrease void Detach (Observer * pob);//notification void Notify ();//foreground State void Setsecretaryaction (string stract); string getsecretaryaction ();};/ /subject.cpp#include "Secretary.h" #include "Observer.h" void Subject::attach (Observer *pob) {}void Subject::D Etach ( Observer *pob) {}void subject::notify () {}void subject::sEtsecretaryaction (String stract) {}string subject::getsecretaryaction () {return "";} void Boss::attach (Observer *pob) {mylist.push_back (POB);} void Boss::D etach (Observer *pob) {mylist.remove (POB);} void Boss::notify () {list<observer *>::iterator it = Mylist.begin (); for (; It! = Mylist.end (); ++it) {(*it)-&GT;UPDA Te ();}} void Boss::setsecretaryaction (String stract) {_action = stract;} String Boss::getsecretaryaction () {return _action;} void Secretary::attach (Observer * pob) {mylist.push_back (POB);} Reduced void Secretary::D Etach (Observer * pob) {mylist.remove (POB);} notify void Secretary::notify () {list<observer *>::iterator it = Mylist.begin (); for (; It! = Mylist.end (); ++it) {(*it) ->update ();}} foreground state void Secretary::setsecretaryaction (string stract) {_action = stract;} String Secretary::getsecretaryaction () {return _action;} -----------------//main.cpp#include "Secretary.h" #include "Observer.h" int main () {//boss bosses *pboss = new boss ();// See Stock colleague Stockobserver *ptongshi1= new Stockobserver ("Fun", pboss);//Watch NBA colleague Nbaobserver *ptongshi2 = new Nbaobserver ("Test", Pboss);p Boss->attach ( PTONGSHI1);p Boss->attach (pTongshi2);p Boss->detach (PTONGSHI1);//The Boss came back Pboss->setsecretaryaction (" I'm the boss and I'm back! "); /give notice pboss->notify (); return 0;}

 

Design mode (10)--Viewer mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.