第一版,耦合比較嚴重
//.h
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <list>
using namespace std;
class StockObserver;
class Secretary {
private:
list<StockObserver *> observers ;
string action;
public:
void Attach(StockObserver * observer){
observers.push_back(observer);
}
void Notify() ;
string getSecretaryAction(){
return action;
}
void SecretaryAction(string value) {
action = value;
}
};
class StockObserver {
private:
string name;
Secretary * sub;
public:
StockObserver(string name,Secretary * sub):name(name),sub(sub){};
void update(){
cout<<"close the website ,back work"<<endl;
};
};
//.m
void Secretary::Notify() {
list<StockObserver *>::iterator itor;
itor = observers.begin();
while (itor != observers.end()) {
(*(*itor)).update();
itor++;
}
}
第二版桑寨,觀察者抽象
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <list>
using namespace std;
class Secretary;
class Observer {
protected:
string name;
Secretary * sub;
public:
Observer(string name,Secretary * sub):name(name),sub(sub){};
~Observer(){};
virtual void Update(){};
};
class Secretary {
private:
list<Observer *> observers ;
string action;
public:
void Attach(Observer * observer){
observers.push_back(observer);
}
void Detach(Observer * observer) {
observers.remove(observer);
}
void Notify() ;
string getSecretaryAction(){
return action;
}
void SecretaryAction(string value) {
action = value;
}
};
class StockObserver:public Observer {
public:
StockObserver (string name,Secretary * sub):Observer(name,sub){};
void Update(){
cout<<sub->getSecretaryAction()<<name<<"close stock website,back work"<<endl;
}
};
class NBAObserver:public Observer {
public:
NBAObserver(string name,Secretary * sub):Observer(name,sub){};
void Update(){
cout<<sub->getSecretaryAction()<<name<<"close nba website,back work"<<endl;
}
};
//.m
void Secretary::Notify() {
list<Observer *>::iterator itor;
itor = observers.begin();
while (itor != observers.end()) {
(*(*itor)).Update();
itor++;
}
}
第三次解耦 觀察者和通知者都分離了
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <list>
using namespace std;
class Subject;
class Observer {
protected:
string name;
Subject * sub;
public:
Observer(string name,Subject * sub):name(name),sub(sub){};
~Observer(){};
virtual void Update(){};
};
class Subject {
public:
virtual void Attach(Observer * observer){};
virtual void Detach(Observer * observer){};
virtual void Notify(){};
virtual string getSubjectState(){ return nullptr;};
virtual void SubjectState(string substate){};
};
class Boss:public Subject {
private:
list<Observer *> observers;
string action;
public:
void Attach(Observer * observer){
observers.push_back(observer);
}
void Detach(Observer * observer) {
observers.remove(observer);
}
void Notify() {
list<Observer *>::iterator itor;
itor = observers.begin();
while (itor != observers.end()) {
(*(*itor)).Update();
itor++;
}
}
string getSubjectState(){ return action;}
void SubjectState(string substate){action = substate;};
};
class StockObserver:public Observer {
public:
StockObserver (string name,Subject * sub):Observer(name,sub){};
void Update(){
cout<<sub->getSubjectState()<<name<<"close stock website,back work"<<endl;
}
};
class NBAObserver:public Observer {
public:
NBAObserver(string name,Subject * sub):Observer(name,sub){};
void Update(){
cout<<sub->getSubjectState()<<name<<"close nba website,back work"<<endl;
}
};
重要概念
將一個系統(tǒng)分割成一系列相互協(xié)作的類有一個很不好的副作用揍庄,就是需要維護相關對象間的一致性救鲤,我們不希望為了維持一致性而使各類緊密耦合久窟。
當一個對象不知道具體有多少對象會受到改變時,考慮使用觀察者模式
觀察者模式主要的工作就是解除耦合蜒简,讓耦合的雙方都依賴于抽象瘸羡,而不是依賴于具體,從而使各自的變化都不會影響另一邊的變化