C++程式設計思想 第2卷 第10章 設計模式 觀察者模式
阿新 • • 發佈:2018-12-09
觀察者 Observer 模式用於解決一個常見的問題 當其他物件改變狀態時 如果一組物件需要進行相應的更新 那麼應該如何處理呢?
使用兩種物件的型別以實現觀察者模式
//: C10:Observer.h // From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison. // (c) 1995-2004 MindView, Inc. All Rights Reserved. // See source code use permissions stated in the file 'License.txt', // distributed with the code package available at www.MindView.net. // The Observer interface. #ifndef OBSERVER_H #define OBSERVER_H class Observable; class Argument {}; class Observer { public: // Called by the observed object, whenever // the observed object is changed: virtual void update(Observable* o, Argument* arg) = 0; virtual ~Observer() {} }; #endif // OBSERVER_H ///:~
這種方法中Observer與Observable互動作用 必須首先宣告Observable 另外 類Argument是空的 在更新過程中它只擔任一個基類角色
被觀察物件的型別是Observable
//: C10:Observable.h // From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison. // (c) 1995-2004 MindView, Inc. All Rights Reserved. // See source code use permissions stated in the file 'License.txt', // distributed with the code package available at www.MindView.net. // The Observable class. #ifndef OBSERVABLE_H #define OBSERVABLE_H #include <set> #include "Observer.h" class Observable { bool changed; std::set<Observer*> observers; protected: virtual void setChanged() { changed = true; } virtual void clearChanged() { changed = false; } public: virtual void addObserver(Observer& o) { observers.insert(&o); } virtual void deleteObserver(Observer& o) { observers.erase(&o); } virtual void deleteObservers() { observers.clear(); } virtual int countObservers() { return observers.size(); } virtual bool hasChanged() { return changed; } // If this object has changed, notify all // of its observers: virtual void notifyObservers(Argument* arg = 0) { if(!hasChanged()) return; clearChanged(); // Not "changed" anymore std::set<Observer*>::iterator it; for(it = observers.begin();it != observers.end(); it++) (*it)->update(this, arg); } virtual ~Observable() {} }; #endif // OBSERVABLE_H ///:~
只要有方法用Observable註冊 一個 Observer並有方法為Observable 更新其Observer 不必在意成員函式的設定
無輸出 只是標頭檔案