https://blog.csdn.net/u012611878/article/details/72859047
观察者模式包括一下角色:
Subject: 目标
ConcreteSubject: 具体目标
Observer: 观察者
ConcreteObserver: 具体观察者
框架
#include <iostream> #include <string> #include <vector> using namespace std; class Observer; class Subject { public: Subject(){} virtual ~Subject(){} virtual void attach(Observer *observer) =0; virtual void detach(Observer *observer) =0; virtual void notify() =0; virtual string get() =0; virtual void set(string value) =0; }; class Boss : public Subject { public: void attach(Observer *observer) override { observers.push_back(observer); } void detach(Observer *observer) override { //删除特定的observer auto it = observers.begin(); for(;it!=observers.end();++it) { if (*it == observer) break; } if(it!=observers.end()) observers.erase(it); } void notify() override; string get() override { return action; } void set(string value) override { action = value; } private: vector<Observer*>observers; string action; }; class Secretary : public Subject { public: void attach(Observer *observer) override { observers.push_back(observer); } void detach(Observer *observer) override { //删除特定的observer auto it = observers.begin(); for(;it!=observers.end();++it) { if (*it == observer) break; } if(it!=observers.end()) observers.erase(it); } void notify() override; string get() override { return action; } void set(string value) override { action = value; } private: vector<Observer *>observers; string action; }; class Observer { public: Observer(){} virtual ~Observer(){} Observer(string name, Subject *sub) { this->name = name; this->sub = sub; } virtual void update() = 0; protected: string name; Subject *sub; }; class StockObserver : public Observer { public: StockObserver(string name, Subject *sub) { this->name = name; this->sub = sub; } void update() override; }; class NBAObserver: public Observer { public: NBAObserver(string name,Subject *sub) { this->name = name; this->sub = sub; } void update() override; }; void StockObserver::update() { cout << sub->get() << " " << name << " " << "关闭股票行情,继续工作!" << endl; } void NBAObserver::update() { cout << sub->get() << " " << name << " " << "关闭NBA直播,继续工作!" << endl; } void Boss::notify() { for(auto it =observers.begin(); it != observers.end();++it) { (*it)->update(); } } void Secretary::notify() { for(auto it =observers.begin(); it != observers.end();++it) { (*it)->update(); } } int main() { Subject *chaochao = new Boss(); Observer *tongshi1 = new StockObserver("liu", chaochao); Observer *tongshi2 = new NBAObserver("chao", chaochao); chaochao->attach(tongshi1); chaochao->attach(tongshi2); //chaochao->detach(tongshi1); chaochao->set("我超超回来了!"); chaochao->notify(); delete chaochao; delete tongshi1; delete tongshi2; return 0; }
观察者模式特点:
https://blog.csdn.net/ThomasKUI/article/details/80033521
1、将一个系统分割成一系列相互协作的类有一个很不好的副作用,那就是需要维护相关相关对象间的一致性。我们不希望为了维持一致性而使各类紧密耦合,这样会给维护、扩展和重用都带来不便。
2、当一个对象的改变需要同时改变其他对象,而且它不知道具体有多少对象有待改变时,应该考虑使用观察者模式。一个抽象模型有两个方面,其中一方面依赖于另一方面,这时用观察者模式可以将这两者封装在独立的对象中使它们各自独立
3、观察者模式所做的工作其实就是在解除耦合。让耦合的双方都依赖于抽象,而不是依赖于具体。从而使得各自的变化都不会影响另一边的变化。