How to Implement the Observer (Observer) mode in C ++

Source: Internet
Author: User

How to Implement the Observer (Observer) mode in C ++
The Observer (Observer) mode is one of the common modes. For example, a newspaper has many subscribers. The subscriber does not know when the newspaper will be delivered. He only knows that he has subscribed to the newspaper. Here, the subscriber acts as the observer, while the newspaper is the observer. Once printed, newspapers should be delivered to subscribers in a timely manner. If a newspaper is regarded as an object, the newspaper is the bond between the observer and the observer. The observer needs to maintain a newspaper-related variable or function. In this case, the variable is whether the user receives the newspaper. It can be set to a Boolean value, the consumer needs to update this variable.

The following is the source code:

// Observer. H # ifndef _ OBSERVER_H _ # define _ OBSERVER_H _ # include
 
  
Using namespace std; class Observer;/* Observer interface */class Subject {public: Subject (): m_Issue (0) {} void Attach (Observer * oo ); // subscribe to this object void Detach (Observer * oo); // unsubscribe void Detach y (); // Notify the subscriber to update void virtual Issue () = 0; // issuance function bool GetIssue () {return m_Issue ;}~ Subject () {list
  
   
: Iterator iter1, iter2, temp; for (iter1 = m_observerList.begin (), iter2 = m_observerList.end (); iter1! = Iter2;) {temp = iter1; ++ iter1; m_observerList.erase (temp);} m_observerList.clear ();} protected: bool m_Issue; // release flag list
   
    
M_observerList; // subscriber queue};/* Observer interface */class Observer {public: Observer (): m_receive (0) {}; void virtual Update () // update function {} void virtual ShowMessage () = 0; // simulate the function protected: bool m_receive related to the observer; // receives the newspaper LOGO }; /* Specific Observer */class ConcreteSubject: public Subject {public: ConcreteSubject () {} void Issue () ;};/* Specific Observer */class Subscriber1: public Observer {public: void virtual Update (); void ShowMessage () ;}; class Subscriber2: public Observer {public: void virtual Update (); void ShowMessage ();}; # endif
   
  
 

Description of the observer interface:
1. The subscribe object, UNSUBSCRIBE, and notification update function is a common action of the observer, so it is abstracted as the interface of the observer.
2. Release functions simulate changing state functions. Because different observer methods are different, they are declared as pure virtual functions and implemented in the derived class.
3. The observer uses the List template in STD for implementation, so the Destructor is required to release it.
Observer Interface Description:
1. Since all observers have the Update Status operation, declare it as an interface.
2. The Update () function has different behaviors for different specific objects. Therefore, it is implemented in a derived class and declared as a pure virtual function.
3. The ShowMessage () function is used to simulate operations related to the observer. If the status of the observer changes, the observer needs to perform some operation, which is implemented here. This example is used to display the information about the received newspaper.

The following is the implementation of the class: Observer. cpp

 

# Include Observer. H # include
 
  
/* Subscribe to newspapers */void Subject: Attach (Observer * oo) {m_observerList.push_back (oo);}/* unsubscribe */void Subject: Detach (Observer * oo) {list
  
   
: Iterator iter; iter = std: find (m_observerList.begin (), m_observerList.end (), oo); while (iter! = M_observerList.end () {m_observerList.erase (iter) ;}}/* notification update */void Subject: Policy () {list
   
    
: Iterator iter = m_observerList.begin (); for (; iter! = M_observerList.end (); iter ++) {(* iter)-> Update () ;}}/* change status */void ConcreteSubject: Issue () {this-> m_Issue =! This-> m_Issue; if (this-> m_Issue) {cout <Magazine is Issued !; This-> Policy (); this-> m_Issue = false;}/* subscriber Update */void Subscriber1: Update () {this-> m_receive = true; this-> ShowMessage (); this-> m_receive = false;} void Subscriber1: ShowMessage () {cout <Subscriber1 has received ed the newspapers !;} /* Subscriberupdate */void Subscriber2: Update () {this-> m_receive = true; this-> ShowMessage (); this-> m_receive = false;} void Subscriber2 :: showMessage () {cout <Subscriber2 has received ed the newspapers !;}
   
  
 


The main program section is as follows:
# Include Observer. H # include
 
  
Int main () {Subject * sub = new ConcreteSubject (); // Observer * customer1 = new Subscriber1 (); // Observer 1 Observer * customer2 = new Subscriber2 (); // observer 2 sub-> Attach (customer1); // observer 1 Subscribe to newspaper sub-> Attach (customer2 ); // observer 2 subscribe to the newspaper if (sub-> GetIssue () {cout <Newspapers has issued !; } Else {cout <Newspapers has not issued !; } Sub-> Issue (); // publish a newspaper cout <
  
   
GetIssue () {cout <Newspapers has issued !; } Else {cout <Newspapers has not issued !; } Sub-> Detach (customer1); // observer 1 unsubscribe sub-> Issue (); // publish a newspaper return 0 ;}
  
 




 

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.