Design pattern--observer pattern observer (object behavior type)

Source: Internet
Author: User

First, the Observer pattern

The Observer pattern is to define a one-to-many dependency between objects, so that when an object changes state, the objects that depend on it are notified and updated automatically. The observer pattern is also called: Subject-Viewer mode, publish-subscribe mode, the former is one, the latter is more.

Second, UML class diagram

Iii. examples

//observersclassObserver { Public: Observer () {}Virtual~Observer () {}Virtual voidUpdate () {}};//BlogclassBlog { Public: Blog () {}Virtual~Blog () {}voidAttach (Observer *observer) {m_observers.push_back (Observer);}//Add Observer    voidRemove (Observer *observer) {m_observers.remove (Observer);}//removing observers    voidNotify ()//notify the viewer{List<observer*>::iterator iter =M_observers.begin ();  for(; ITER! = M_observers.end (); iter++)            (*iter)Update (); }    Virtual voidSetStatus (strings) {m_status = s;}//Set Status    Virtual stringGetStatus () {returnM_status; }//Get StatusPrivate: List<observer* > M_observers;//Observer linked listprotected:    stringM_status;//Status};//Specific Blog CategoriesclassBLOGCSDN: Publicblog{Private:    stringM_name;//Bo Master Name Public: Blogcsdn (stringname): M_name (name) {}~blogcsdn () {}voidSetStatus (strings) {m_status ="CSDN Notice:"+ M_name + S; }//setting state information specifically    stringGetStatus () {returnm_status;}};//specific observersclassObserverblog: PublicObserver {Private:    stringM_name;//Viewer NameBlog *m_blog;//Watch the blog, of course, in a list form better, you can observe multiple blogs Public: Observerblog (stringName,blog *blog): M_name (name), M_blog (blog) {}~Observerblog () {}voidUpdate ()//Get update Status    {         stringStatus = M_blog->GetStatus (); cout<<m_name<<"-------"<<status<<Endl; }};//Test CaseintMain () {Blog*blog =NewBLOGCSDN ("wuzhekai1985"); Observer*observer1 =NewObserverblog ("Tutupig", blog); Blog-Attach (Observer1); Blog->setstatus ("Publish design Pattern C + + implementation (15)--Observer mode"); Blog-Notify (); DeleteBlogDeleteObserver1; return 0;}

Design pattern--observer pattern observer (object behavior type)

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.