Observer Mode (OBSERVER): Defines a one-to-many dependency that allows multiple observer objects to listen to a Subject object at the same time, which notifies all observer objects when the state changes, enabling them to automatically update themselves.
One of the bad side effects of splitting a system into a series of collaborative classes is the need to maintain consistency between related objects, and we don't want to be tightly coupled in order to maintain consistency, which can be inconvenient for maintenance, expansion, and reuse. So, we can use the observer pattern when one object changes and you need to change other objects and don't know how many objects to change. An abstract model has two facets, one of which is dependent on the other, and the observer pattern can encapsulate the two in separate objects to change and reuse independently.
In general, the work of the observer pattern is to decouple, so that both sides of the coupling are dependent on abstraction, rather than on specifics, so that their changes do not affect the change on the other side.
#ifndef observer_h#define observer_h#include<iostream> #include <string> #include <list>using Namespace Std;class observer{friend class subject;public:string name; Subject *sub;//observer (string n, Subject *s): Name (n), sub (s) {}virtual void updata () = 0;}; Class Subject{list<observer *> observers;public:string action;void Attach (Observer *obser); void Detach (Observer * obser); void Notify (); void Subjectstate (String st);//virtual void Subjectstate (string&) = 0;}; void Subject::attach (Observer *obser) {observers.push_back (obser);} void Subject::D etach (observer* obser) {observers.remove (obser);} void Subject::notify () {List<observer*>::iterator it = Observers.begin (); for (; It! = Observers.end (); ++it) {(*it) ->updata ();}} void Subject::subjectstate (String st) {action = st;} Class Boss:p ublic subject{//public://void subjectstate (string& st);}; Class Secretary:p ublic subject{//void Subjectstate (string& st);}; Class Stockobserver:p ublic observer{//subject sub;publiC:stockobserver (string n, Subject *s) {name = N; sub = s;} void Updata ();}; void Stockobserver::updata () {cout << sub->action << "," <<name<< "closes the stock and continues to work. \ n ";} Class Nbaobserver:p ublic observer{public:nbaobserver (string n, Subject *s) {name = N; sub = s;} void Updata ();}; void Nbaobserver::updata () {cout << sub->action << "," << name << "Close the NBA Live, continue to work. \ n ";} #endif
#include "Observer.h" int main () {Subject * hunansan=new Boss; stockobserver* tongshi1=new stockobserver ("Oppe", Hunansan); Nbaobserver *tongshi2=new nbaobserver ("mins", Hunansan); Hunansan->attach (TONGSHI1); Hunansan->attach ( TONGSHI2); Hunansan->subjectstate ("I am Coming"); Hunansan->notify (); Hunansan->detach (TONGSHI1); hunansan- >notify (); Subject * Xiaomimi = new Secretary; stockobserver* tongshi3 = new Stockobserver ("Oppe", Xiaomimi); Nbaobserver *tongshi4 = new Nbaobserver ("mins", Xiaomimi); Xiaomimi->attach (TONGSHI3); Xiaomimi->attach ( TONGSHI4); Xiaomimi->subjectstate ("Boss comming"); Xiaomimi->notify (); return 0;}
Design pattern C + + implementation 10: Observer mode