Design Pattern: Observer Pattern

Source: Internet
Author: User

Design Pattern: Observer Pattern

The Observer mode perfectly separates the Observer from the observed object.


In GOF's design patterns: the basis for reusable object-oriented software, the observer pattern is like this: defining a one-to-many dependency between objects, when the status of an object changes, all objects dependent on it are notified and updated automatically. When an object changes, the object that follows it will be notified. This interaction is also called publish-subscribe ). The target is the publisher of the notification. It does not need to know who is its observer when it sends the notification.

For example, sometimes when we are at school and the class leader wants to collect the forms filled out by our class members, the class leader will not be able to ask us to accept them one by one. It is estimated that no one will be the class leader, of course we gave it to him by ourselves, but how do we know that we want to give it to him? We can't keep asking 40 of us, so normal people will think that the class leader will send a message to each of us, let's just give it to him. In this mode, the shift leader belongs to the observer, and we belong to the observer, so we can use the observer mode to handle it.


The UML diagram is as follows:


Subject (observer)
-- The target knows its observer. Multiple observers can observe the same object;
-- Provides interfaces for registering and deleting observer objects.

Observer (Observer)
-- Defines an update interface for objects that need to be notified when the target changes.

ConcreteSubject (target)
-- Stores related statuses into various ConcreteObserver objects;
-- When its status changes, a notification is sent to all its observers.

Observer1, Observer2 (Specific observer)
-- Maintain a reference pointing to the ConcreteSubject object;
-- Storage-related statuses, which should be consistent with the target status;
-- Implements the Observer update interface so that its status is consistent with that of the target.


The Code is as follows:

/** @ Author: LiuCimin * web: http://blog.csdn.net/liucimin/ * mail: lcmjk@foxmail.com * 2014-9-24 */# include
 
  
# Include
  
   
# Include
   
    
Using namespace std; class Observer; // class Subject {public: virtual void Attach (Observer *) = 0; virtual void Detach (Observer *) = 0; virtual void required y () = 0; virtual string GetState () = 0 ;}; // Observer class Observer {public: virtual void Update () = 0 ;}; // Observer 1 class Observer1: public Observer {public: Observer1 (Subject * pSubject): m_pSubject (pSubject) {} void Update () {cout <"Observer1 get the update. new State: "<
    
     
GetState () <
     
      
GetState () <
      
        ObserverList; string m_iState;}; void ConcreteSubject: Attach (Observer * pObserver) {ObserverList. push_back (pObserver);} void ConcreteSubject: Detach (Observer * pObserver) {vector
       
         : Iterator p = ObserverList. begin (); while (* p! = PObserver) {++ p;} ObserverList. erase (p);} void ConcreteSubject: Y () {vector
        
          : Iterator it = ObserverList. begin (); while (it! = ObserverList. end () {(* it)-> Update (); ++ it ;}} int main () {// Create SubjectConcreteSubject * pSubject = new ConcreteSubject (); // Create ObserverObserver * pObserver1 = new Observer1 (pSubject); Observer * pObserver2 = new Observer2 (pSubject); // Change the statepSubject-> SetState (" "); // Register the observerpSubject-> Attach (pObserver1); pSubject-> Attach (pObserver1); pSubject-> destroy y (); // Unregister the observerpSubject-> Detach (pObserver1 ); pSubject-> SetState ("fill in the table"); pSubject-> sort y (); delete pObserver1; delete pObserver2; delete pSubject ;}
        
       
      
     
    
   
  
 




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.