[design mode]<9>. C + + and observer patterns (Observer pattern)

Source: Internet
Author: User

Eeer in silence

Original: http://www.cnblogs.com/hebaichuanyeah/p/6091694.html

Intent: Define a one-to-many dependency between objects so that an object is changed and other objects are updated.

The Java event mechanism is an observer pattern, and when an event occurs, all event receivers execute the event response function.

To implement the Observer pattern, you first need to define an "observer Class (Observer)" interface that defines a pure virtual event response function within the class.

and requires a "target class (subject)", which contains a set/map or list container. When an object that inherits from the Observer Class (Observer) needs to listen for an event, it is added to the container of the target class (subject) object, and when the event occurs, it traverses the container and invokes the event response function. One time before I needed to implement a serial port event triggering mechanism in QT, I didn't use the serial port event in my Qt4, so I blocked waiting for the serial port in a thread and tried to implement this custom event mechanism using the observer pattern. Later the discovery of the egg pain, in QT if the non-main thread inside the operation UI class will occasionally run the program to die ...

So, if you use a language/library with a perfect event mechanism, the observer pattern will not be available. For example, the delegate in C # is used with generics, equivalent to the effect of the observer pattern.

A chestnut

#include <iostream> #include <set>using namespace std;//Observer interface, including event response function class observer{public:virtual void U pdate (int n) =0;};/    /target class, responsible for triggering event class Subject{public:subject () {};    Add Event listener void Addobserver (Observer *observer) {Observerset.insert (Observer);    }//delete event listener void Removeobserver (Observer *observer) {observerset.erase (Observer);        }//Trigger event void notify (int n) {set<observer *>::iterator iter;           for (iter = Observerset.begin (); ITER! = Observerset.end (); ++iter) {(*iter)->update (n);   Iter.        ->update (); }}private:set<observer *> observerset;}; Class Observer1:p ublic observer{public:void Update (int n) {cout<< "Observer 1 event response function, accept parameter:" <<n<<    Endl }};class Observer2:p ublic observer{public:void Update (int n) {cout<< ' Observer 2 event response function, accept parameter: "<<n<&    Lt;endl;    }};main () {Subject Subject; Observer * OBSErver1 = new Observer1 ();    Observer * Observer2 = new Observer2 ();    Subject.addobserver (Observer1);    Subject.addobserver (OBSERVER2);    Subject.notify (4); Subject.notify (1);}

[design mode]<9>. C + + and observer patterns (Observer pattern)

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.