Design Pattern: Observer Pattern

Source: Internet
Author: User

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: [email protected] * 2014-9-24 */# include <iostream> # include <vector> # include <string> using namespace STD; Class observer; // class subject {public: virtual void attach (Observer *) = 0; virtual void detach (Observer *) = 0; virtual void dety () = 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: "<m_psubject-> getstate () <Endl;} private: Subject * m_psubject ;}; // observer 2 class observer2: public observer {public: observer2 (subject * psubject): m_psubject (psubject) {} void Update () {cout <"observer2 get the update. new state: "<m_psubject-> getstate () <en DL;} private: Subject * m_psubject;}; // The class concretesubject: public subject {public: void attach (Observer * pobserver); void detach (Observer * pobserver ); void every Y (); void setstate (string state) {m_istate = State;} string getstate () {return m_istate;} PRIVATE: vector <Observer *> observerlist; string m_istate ;}; void concretesubject: attach (Observer * pobserver) {observerlist. push_back (pobserver);} void con Cretesubject: Detach (Observer * pobserver) {vector <Observer * >:: iterator P = observerlist. Begin (); While (* P! = Pobserver) {++ P;} observerlist. erase (p);} void concretesubject: Notify () {vector <Observer * >:: 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 ;}




Design Pattern: 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.