Observer mode in C + + design pattern programming using the example _c language

Source: Internet
Author: User

Overview:
China's stock market has recently fluctuated, of course, the ups and downs on the business opportunities, Xiaoming found the consequences of the market, bought the Chinese securities, he wanted to be on the computer client, on the web, on the phone, the ipad can be viewed on the real-time market, this case how should we design our software? We can do this: all clients of xiaoming subscribe to the stock of China securities, as long as the stock changes, all clients will be notified and automatically updated.
This is our observer model, which defines a one-to-many dependency between objects, and when an object's state changes, all objects that depend on it are notified and automatically updated.

Class Diagram:

As you can see, there are these roles in the implementation of this observer pattern:
Abstract Theme (Subject) role: The theme role keeps all references to the object of observation in a cluster, each subject can have any number of observers. Abstract topics provide an interface to add and remove observer objects, which are also called Abstract Observer (observable) roles, typically implemented with an abstract class or an interface.
Abstract Observer (Observer) role: Defines an interface for all specific observers and updates itself when a topic is notified. This interface is called the update interface. Abstract observer roles are typically implemented with an abstract class or an interface. In this schematic implementation, the update interface contains only one method (the update () method), which is called the Update method.
Specific topic (concretesubject) Role: To deposit the state of a specific object of concern, and to give notice to all registered observers when the internal state of the specific subject changes. The specific subject role is also called the specific observer role (concrete observable). Specific theme roles are usually implemented with a specific subclass.
The specific observer (CONCRETEOBSERVER) role: The State of the store and the state of the topic itself. The specific reviewer role implements the update interface required by the abstract observer role to reconcile its own state with the state of the subject. If necessary, the specific reviewer role can save a reference to a specific subject object. The specific observer role is usually implemented with a specific subclass.       
from a specific theme role to the abstract observer role of the composite relationship, on behalf of the specific Subject object can have any number of abstract observer object references. The use of abstract observers rather than specific observers means that subject objects do not need to know which concreteobserver types are referenced, but only abstract observer types. This makes it possible for a specific subject object to dynamically maintain a series of references to the observer object and to invoke the update () method shared by each observer when needed. This approach is called "for abstract programming".

Concept
The Observer model is to say there is a goal, many observers to "observe" the goal. The target is a derived class of the target abstract class, and the observer is a derived class of the observer abstract class. When the target class's data changes, all corresponding observers correspond to update their state
What can be used: for example, there is a world clock program, there are multiple graphics clocks to display such as Beijing time zone, Paris time zone, and so on. If you set up a Beijing time, all other clock graphics need to be updated (plus or minus the slack value). Typical graphical interface design can be seen everywhere, a temperature program, in the temperature and humidity conditions change, to update a variety of display graphics to render.

Instance
when used, first define a subject class object, and then define multiple observer class (derived class) objects, each observer object specifying which subject object they are registered to.

Example:

#include <vector> #include <iostream> #include <string> using namespace std;
 
Class Subject;
 
Class observer{//Observer abstract class public:virtual void update (Subject *base) =0; Protected:subject * _subject;};            Class subject{//Target abstract class public:string S1;               Data values, which can be used as private data, and then define an excuse to return the value, here for easy int i1;
   Data value void Regiobserver (Observer *obs) {_observer.push_back (OBS);
 cout<< "Registered" <<endl;
 } void Deleobserver (Observer *obs) {_observer.pop_back ();
  } void Notify () {//Update all observers vector<observer *>::iterator it;
 for (it = _observer.begin (), it!= _observer.end (); it++) (*it)->update (this);    } private:vector<observer *> _observer;
 
Observer container};
  Class Fsubject:public subject{public:void Set (string S,int i) {S1 = s;
  I1 = i;                  Notify (); Inform the Observer.
 
The order of execution of the main function has ensured that all observers have entered the container}; Class Fobserver:p ublic observer{//First observer derived class Public:fobserver (Subject *base): ObservER () {_subject = base;
 _subject->regiobserver (this);
  } void Update (Subject *base) {S1 = base->s1;
  I1 = base->i1;
 Display ();
  void display () {cout<< "update value, first \ n" <<s1<<endl;
 cout<<i1<<endl;
 } private:string S1;
int i1;
 
};
  Class Sobserver:public observer{//second observer derived class Public:sobserver (Subject * Base) {_subject = base;
 _subject->regiobserver (this);
  } void Update (Subject *base) {S1 = base->s1;
  I1 = base->i1;
 Display ();
  void display () {cout<< "update value, second \ \" <<s1<<endl;
 cout<<i1<<endl;
 } private:string S1;
int i1;
 
};
 int main () {Fsubject * sub = new Fsubject;
 Fobserver * one = new Fobserver (sub);
 Sobserver * two = new Sobserver (sub);
 Sub->set ("OK", 3);
 return 0;

}
The container object maintainer in the Subject class all references to the observer, in order to update all the observers in the Notify, that is, to invoke the Observer->update () through traversal.

The role of the update () in the observer is to accomplish what the observer needs to do, such as in the previous example, to update its own saved copy value and then display it.

The Observer class has a subject pointer that is very important in the observer's derived class constructor, which needs to be registered with its own this pass in the past.

Observer has the same data as subject, or it can be set as a local variable, just to accomplish what the observer needs to do without having to store it.

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.