Design Pattern note-Observer Pattern

Source: Internet
Author: User

Observer mode (Observer): defines a one-to-many dependency, allowing multiple Observer objects to listen to a topic object at the same time. When the status of this topic object changes, it notifies all observer objects so that they can automatically update themselves.

Implementation process:

1. The Observer (Observer) registers itself to the observed object (Observer). The observed object stores the Observer in a List.

2. When the observed object (Subject) changes, it will automatically traverse all registered observers from the container and notify all registered observers in sequence.

3. The Observer (Observer) tells the Observer (Observer) to unobserve and remove the Observer from the list.

One observer can correspond to multiple observers. When the observer changes, he can notify all the observers one by one. When being watched to send a notification, you do not need to know who is its observer. The observer and the observer are loosely coupled. The observer does not know the details of the observer, as long as the observer implements the same interface.

UML class diagram:

The Basic Code of the observer mode is as follows:

/*************************************** * **************************** Filename: observer. hcreated: 2012-09-25 author: firehood purpose: design Pattern of firehood-Observer Pattern ******************************** * **********************************/# pragma once # include "windows. h "# include <assert. h> # include <iostream> # include <string> # include <list> # include <algorithm> using namespace std; // abstract Observer class Observer {public: Observer (void) {} virtual ~ Observer (void) {} virtual void Update () = 0 ;}; // Abstract The Observer class Subject {public: Subject (void) {m_ObserverList.clear ();} virtual ~ Subject (void) {} virtual BOOL Attach (Observer * pObserver) {if (pObserver = NULL) {return FALSE;} if (std: find (m_ObserverList.begin (), m_ObserverList.end (), pObserver )! = M_ObserverList.end () {return FALSE;} m_ObserverList.push_back (pObserver); return TRUE;} virtual BOOL Detach (Observer * pObserver) {if (pObserver = NULL) {return FALSE ;} m_ObserverList.remove (pObserver); return TRUE;} virtual void y (void) {list <Observer * >:: iterator iter; for (iter = m_ObserverList.begin (); iter! = M_ObserverList.end (); iter ++) {(* iter)-> Update () ;}} private: list <Observer *> m_ObserverList ;}; typedef string STATE; // The class ConcreteSubject: public Subject {public: ConcreteSubject () {}; virtual ~ ConcreteSubject () {}; BOOL SetState (STATE SubjectState) {if (m_SubjectState! = SubjectState) {m_SubjectState = SubjectState;} cout <"sets the observed STATE:" <m_SubjectState <endl; return TRUE;} STATE GetState () {return m_SubjectState ;} private: STATE m_SubjectState;}; // observe the class ConcreteObserver: public Observer {public: // when constructing a ConcreteObserver, specify the specific ConcreteObserver (ConcreteSubject * pSubject ): m_pSubject (NULL) {assert (pSubject); m_pSubject = pSubject;}; virtual ~ ConcreteObserver () {} virtual void Update () {m_ObserverState = m_pSubject-> GetState (); cout <"Observer" <this <": changed by observer status: "<m_ObserverState <endl;} private: ConcreteSubject * m_pSubject; STATE m_ObserverState ;};

The client call code is as follows:

# Include "Observer. H "# include <iostream> using namespace STD; int main (INT argc, char * argv []) {cout <"*********************************** ** "<Endl; cout <"firehood learning design mode-Observer mode" <Endl; cout <"************************************ * "<Endl; // declare concretesubject subject; // create observer A. The observed object is subjectconcreteobserver * pobservera = new concreteobserver (& subject); Subject. attach (pobservera); // create observer B. The observed object is subjectconcreteobserver * pobserverb = new concreteobserver (& subject); Subject. attach (pobserverb); Subject. setstate ("status 1"); Subject. required y (); Subject. setstate ("state 2"); Subject. Y (); Delete pobservera; Delete pobserverb; System ("pause"); Return 0 ;}

Running result:

Firehood-Observer Mode
Set the status of the observer: Status 1
Observer 003a6530: changed by the observer status: Status 1
Observer 003a65e0: changed by the observer status: Status 1
Set the status of the observer: status 2
Observer 003A6530: changed by the observer status: status 2
Observer 003A65E0: changed by the observer status: status 2
Press any key to continue...

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