C + + design mode-Viewer mode

Source: Internet
Author: User

Observer pattern: It defines a dependency on the elephant's leisure, so that when a likeness is changed, all of its dependent people are notified and updated on their own, too.

The graph of the Observer pattern:

code example: using the Weatherdata to obtain the figures, update the concreteobserver and show that the following is the code example:

1 #ifndef Subject_h2 #defineSubject_h3#include"Observer.h"4 classsubject5 {6  Public:7 subject ();8     Virtual~subject ();9     Virtual voidRegisterobserver (observer* o) =0;Ten     Virtual voidRemoveobserver (observer* o) =0; One     Virtual voidNotifyobservers () =0; A }; -  - #endif //Subject_h
Subject
1 #ifndef Observer_h2 #defineObserver_h3 4 classObserver5 {6  Public:7 Observer ();8     Virtual~Observer ();9     Virtual voidUpdatefloatTempfloatHumidity,floatpressure) =0;Ten  One }; A  - #endif //Observer_h
Observer
1#include"weather_data.h"2 3 Weatherdata::weatherdata ()4 {5 m_vecobserver.clear ();6 }7weatherdata::~Weatherdata ()8 {9 Ten } One  A voidWeatherdata::registerobserver (Observer *o) - { - m_vecobserver.append (o); the  - } -  - voidWeatherdata::removeobserver (observer*o) + { -   intindex =M_vecobserver.indexof (o); +   if(Index >=0) A   { at M_vecobserver.remove (index); -   } - } -  - voidweatherdata::notifyobservers () - { in      for(inti =0; i < m_vecobserver.size (); i++) -     { tom_vecobserver.at (i)->update ( This->temperature, This->humidity, This-pressure); +     } - } the  * voidWeatherdata::setmeasurements (floatTemperature,floatHumidity,floatpressure) $ {Panax Notoginseng      This->temperature =temperature; -      This->humidity =humidity; the      This->pressure =pressure; + measurementschanged (); A } the floatweatherdata::gettemperature () + { -     return  This-temperature; $ } $  - floatweatherdata::gethumidity () - { the     return  This-humidity; - }Wuyi  the floatweatherdata::getpressure () - { Wu     return  This-pressure; - } About  $ voidweatherdata::measurementschanged () - { - notifyobservers (); -}
Weather_data
1#include"Concreteobserver.h"2#include <QDebug>3#include <weather_data.h>4Concreteobserver::concreteobserver (Subject *weatherdata)5 {6      This->weatherdata =Weatherdata;7Weatherdata->registerobserver ( This);8 9 }Ten  One voidConcreteobserver::update (floatTempfloatHumidity,floatpressure) A { -      This->temperature =temp; -      This->humidity =humidity; the display (); - } -  - voidconcreteobserver::d isplay () + { -Qdebug () <<"when the temperature of the former:"<< This->temperature<<"Pre-elevation:"<< This-humidity; +}
Concreteobserver
1#include <QCoreApplication>2#include"Concreteobserver.h"3#include"weather_data.h"4 intMainintargcChar*argv[])5 {6 qcoreapplication A (argc, argv);7weatherdata* subject =NewWeatherdata ();8concreteobserver* Observer =NewConcreteobserver (subject);9Subject->setmeasurements ( -, -, +);TenSubject->setmeasurements ( +, -, -); One  A  -     returna.exec (); -}
Main

Reference information: Headfirst design mode

Source code:

C + + design mode-Viewer mode

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.