[Design pattern] _ [actual use example of Observer Pattern in project]

Source: Internet
Author: User


Scenario:

1. for example, in interface development, communication is required between multiple windows. A common method is that each window contains references to other windows, then, when necessary, call the corresponding function to obtain the corresponding value through reference in other windows;

However, this confirmation is obvious, that is, it will cause the dependencies and coupling between windows. It is okay to think about another window when testing or mutating a window, the compilation of another window depends on it, which leads to repeated dependencies.

In this case, the compilation fails or the modification of one of the two is required. Very troublesome and difficult to maintain.

2. another unrealistic solution is to send events between windows, such as QT signals or Win32 messages. However, this will cause message flooding and resource abuse, in addition, the main thread is used to process events. Why should we issue events?


Solution:

1. Use the observer mode to send notifications to the observer, so that the observer can respond in a timely manner and reduce coupling. Let's look at the actual example:


Main. cpp

/** File: Main. CPP */# include <list> # include <stdio. h> # include <time. h ># include <string >#include <iostream> using namespace STD; Enum policytype {kupdateoutputdir = 0, kupdatetitle}; Class observer;/*** 1. abstract parent class, which cannot be instantiated. Target. */class subject {public: Virtual ~ Subject () {} virtual void attach (Observer * Observer); Virtual void detach (Observer * Observer); Virtual void dety (void * userdata, int type); protected: subject () {} private: List <Observer *> observers _;};/*** 1. abstract parent class, which cannot be instantiated, observer. */class observer {public: Virtual ~ Observer () {} virtual void Update (void * userdata, int type) {} protected: Observer () {} private :}; // subjectvoid subject :: attach (Observer * Observer) {observers _. push_back (observer);} void subject: Detach (Observer * Observer) {observers _. remove (observer);} void subject: Policy (void * userdata, int type) {list <Observer * >:: iterator theiterator; For (theiterator = observers _. begin (); theiterator! = Observers _. end (); theiterator ++) {(* theiterator)-> Update (userdata, type) ;}} class dhdialog: public subject, public observer {public: dhdialog () {}~ Dhdialog () {} void Update (void * userdata, int type) {// 1. to add a judgment type, you only need to process the specified message switch (type) {Case kupdateoutputdir: {output_dir _ = * (string *) userdata); STD :: cout <"I am in dhdialog:" <output_dir _ <STD: Endl; break ;}} STD: String title _; STD :: string output_dir _;}; Class dhoutputwindow: public subject, public observer {public: dhoutputwindow (){}~ Dhoutputwindow () {} void Update (void * userdata, int type) {// 1. to add a judgment type, you only need to process the specified message switch (type) {Case kupdatetitle: {String title = * (string *) userdata); // modify the output directory STD :: cout <"I am dhoutputwindow:" <title <STD: Endl; break ;}}; int main (INT argc, char * argv []) {dhdialog dialog; dhoutputwindow output_window _; // 1. listen to each other. Note that adding a listener dynamically does not directly reference each other's objects. dialog. attach (& output_window _); output_window _. attach (& DIALOG); dialog. title _ = "I am InfoWorld"; dialog. notify (& dialog. title _, kupdatetitle); string output = "C: \"; output_window _. Y (& output, kupdateoutputdir );}

Output:

I am DhOutputWindow: I am infoworldI am in DhDialog: C:\


[Design pattern] _ [actual use example of Observer Pattern in project]

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.