Design Mode C ++ implementation (15) -- Observer Mode

Source: Internet
Author: User

The design patterns in the software field provide developers with an effective way to use expert design experience. The design patterns use the important features of object-oriented programming languages: encapsulation, inheritance, and polymorphism. It may be a long process to truly comprehend the essence of the design patterns, which requires a lot of practical experience. I recently read a book on design patterns. I wrote a small example in C ++ for each pattern to help me better understand it. Refer to "big talk Design Patterns" and "design patterns: Reusable basics of object-oriented software. This article introduces the implementation of the observer mode.

Observer mode: defines a one-to-many dependency between objects. When the State of an object changes, all objects dependent on it are notified and updated automatically. It also has two aliases: dependents and publish-subsrcibe ). You can give an example of blog subscription. When a blogger posts a new article, that is, the status of the blogger changes, the readers who subscribe will receive a notification and then perform the corresponding action, for example, reading articles or adding them to favorites. There is a one-to-many dependency between the blogger and the reader. The corresponding UML diagram design is given below.


You can see that there is an observer linked list (I .e. subscriber) in the blog class. When the blog status changes, notify all the observers through the notify y member function and tell them that the blog status is updated. The observer obtains the status information of the blog through the update member function. It is not difficult to implement the Code. Below is an implementation of C ++.

// Observer class observer {public: Observer () {} virtual ~ Observer () {} virtual void Update () {}}; // blog class blog {public: blog () {} virtual ~ Blog () {} void attach (Observer * Observer) {m_observers.push_back (observer);} // Add the observer void remove (Observer * Observer) {m_observers.remove (observer );} // remove the observer void y () // notify the observer {list <Observer *>: iterator iter = m_observers.begin (); For (; iter! = M_observers.end (); ITER ++) (* ITER)-> Update ();} virtual void setstatus (string s) {m_status = s ;} // set the status virtual string getstatus () {return m_status;} // obtain the status private: List <Observer *> m_observers; // observer linked list protected: String m_status; // status };

The above is the base class of the observer and blog and defines a common interface. The blog class mainly completes adding, removing, and notifying the observer. Setting and obtaining the status is only a default implementation. The implementation of their corresponding subclass is given below.

// Specific blog class blogcsdn: Public blog {PRIVATE: String m_name; // The Name Of The blogger public: blogcsdn (string name): m_name (name ){}~ Blogcsdn () {} void setstatus (string s) {m_status = "csdn notification:" + m_name + S;} // set the status information string getstatus () {return m_status ;}}; // The actual observer class observerblog: public observer {PRIVATE: String m_name; // observer name blog * m_blog; // The observed blog, of course, it is better to use a linked list. You can observe Multiple blogs: public: observerblog (string name, blog * blog): m_name (name), m_blog (blog ){}~ Observerblog () {} void Update () // obtain the Update Status {string status = m_blog-> getstatus (); cout <m_name <"-------" <status <Endl ;}};

Customer usage:

// Test case int main () {blog * blog = new blogcsdn ("wuz1_ai1985"); Observer * observer1 = new observerblog ("tutupig", blog ); blog-> attach (observer1); blog-> setstatus ("Design Mode C ++ implementation (15) -- Observer mode"); blog-> Y (); Delete blog; delete observer1; return 0 ;}

I enjoy the copyright of blog articles, reprint please indicate the source http://blog.csdn.net/wuzhekai1985


Related Article

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.