Observer mode, also known as publish-subscribe, MVC pattern, etc. Popular point, for example, stocks, a lot of people pay attention to a stock, send a person to observe the situation of the stock, a change (observation), notify all the people who ordered the news.
While our common MVC pattern, v refers to the view-view, M refers to the model-model, a bit like the observer pattern, the data in the model, a change in the associated view of the notification. This makes it easy to separate the model from the view.
The example used here is a subscription to a blog. Blogs belong to the observed object, and subscribers belong to the Observer. Subscribers subscribe to the blog, and once the blog is updated, it iterates through the registry and pushes the updated blog to subscribers.
I draw UML diagram,,, myself, UML diagram is really helpful for writing programs. The corresponding code is given directly below:
Observer.h:
#ifndef observer_h#define observer_h#include<string> #include "subject.h" using namespace Std;class Blog;class Blogcsdn;class Observer{public:observer () {}virtual ~observer () {}virtual void Update () {}};class observerblog:public O Bserver{private:string M_name;//observer name blog *m_blog;//Observe the blog in the form of a list of better, can observe multiple blogs Public:observerblog (string name, blog * blog): M_name (name), M_blog (blog) {}~observerblog () {}void Update ();}; #endif
Observer.cpp
#include "observer.h" #include <iostream>using namespace Std;void observerblog::update () {String status = m_blog- >getstatus () cout << m_name << "-------" << status << Endl;}
Subject.h
#ifndef subject_h#define subject_h#include<list> #include "observer.h" using namespace Std;class Observer;class Blog{private:list<observer *> m_observers;//Observer list protected:string m_status;//Status Public:blog () {}virtual ~Blog ( ) {}void Attach (Observer *observer); Add observer void Remove (Observer *observer);//Remove observer void Notify ();//notify observer virtual void SetStatus (string s);//Set State virtual String GetStatus ();//Get Status};class blogcsdn:public blog{private:string m_name;//BO main name Public:blogcsdn (string name): m_ Name {}~blogcsdn () {}void SetStatus (string s); string GetStatus ();}; #endif
Subject.cpp
#include "subject.h" void Blog::attach (Observer *observer) {m_observers.push_back (Observer);} void Blog::remove (Observer *observer) {m_observers.remove (Observer);} void Blog::notify () {list<observer *>::iterator iter = M_observers.begin (); for (; ITER! = M_observers.end (); iter++ ) {(*iter)->update ();}} void Blog::setstatus (string s) {m_status = s;} String Blog::getstatus () {return m_status;} void Blogcsdn::setstatus (string s) {m_status = "CSDN notification:" +m_name + S;} String Blogcsdn::getstatus () {return m_status;}
Main.cpp
#include "observer.h" #include "subject.h" int main () {Blog *blog = new Blogcsdn ("zy416548283"); observer *observer = new Obs Erverblog ("Tutupig", blog), Blog->attach (Observer); Blog->setstatus ("Post Observer Pattern Implementation"); Blog->notify (); Delete blog;delete Observer;return 0;}
Compile run:
[Email protected]:~/code/cplus/designpattern/observer$./a.out tutupig-------csdn Notice: zy416548283 Post * * *
If you write a program that encounters cross-reference problems (header files refer to each other, and methods in the class are called), you can refer to the post: Click the open link or see my question: Click to open the link
Ps:
Reference Blog: Click to open link
Reference books: <23 Design Patterns-c++>,< easy to learn design patterns >
<c++ Implementing Design Patterns > Observer patterns