//10.觀察者模式 //ver1 class StockObserver; //與 Secretary 相互依賴; 因此提早聲明 class Secretary; //股票同事類 class StockObserver { private: string _name; Secretary * _ps; public: StockObserver(string name, Secretary *p) { _name = name; _ps = p; } void Update() { //獲得前臺通知,趕快採起行動。 //_ps->GetSecretaryAction(); //這裏必須屏蔽; 這裏無法編譯經過, StockObserver 與 Secretary 相互耦合。 //解決的辦法是,分別寫在2個.h和.cpp文件中。並在.h文件裏提早聲明要引用的類,並把對象定義爲指針。 } }; //前臺祕書類 class Secretary { private: list<StockObserver *> myList; string _action; public: //增長 void Attach(StockObserver * psob) { myList.push_back(psob); } //減小 void Detach(StockObserver * psob) { myList.remove(psob); } //通知 void Notify() { list<StockObserver *>::iterator it = myList.begin(); for ( ; it != myList.end(); ++it ) { (*it)->Update(); } } //前臺狀態 void SetSecretaryAction(string strAct) { _action = strAct; } string GetSecretaryAction() { return _action; } }; void main1() { Secretary *ps1 = new Secretary(); StockObserver *pso1 = new StockObserver("fun", ps1); StockObserver *pso2 = new StockObserver("test", ps1); //前臺記下兩位同事 ps1->Attach((pso1)); ps1->Attach((pso2)); //老闆回來了,發通知 ps1->SetSecretaryAction("老闆回來了"); ps1->Notify(); }
//10.觀察者模式 //ver2 //Observer.h //前置聲明 class Subject; class Boss; class Secretary; class Observer { protected: string _name; Subject * _ps; public: Observer(string name, Subject * ps); virtual ~Observer(void); public: virtual void Update(); }; //看股票同事 class StockObserver : public Observer { public: StockObserver(string name, Subject * ps); virtual ~StockObserver(); virtual void Update(); }; //看NBA同事 class NBAObserver : public Observer { public: NBAObserver(string name, Subject * ps); virtual ~NBAObserver(); virtual void Update(); }; //Observer.cpp #include "Observer.h" #include "Secretary.h" Observer::Observer(string name, Subject * ps) { _ps = ps; _name = name; } Observer::~Observer(void) { } void Observer::Update() { _ps->GetSecretaryAction(); } StockObserver::StockObserver(string name, Subject * ps) : Observer(name, ps) {} StockObserver::~StockObserver() {} void StockObserver::Update() { _ps->GetSecretaryAction(); } /////////////////////////////// NBAObserver::NBAObserver(string name, Subject * ps) : Observer(name, ps) {} NBAObserver::~NBAObserver() {} void NBAObserver::Update() { _ps->GetSecretaryAction(); } //----------------------------------- //Subject.h //前置聲明 class Observer; class StockObserver; class NBAObserver; //通知者接口 class Subject { public: virtual void Attach(Observer *pob); virtual void Detach(Observer *pob); virtual void Notify(); void SetSecretaryAction(string strAct); string GetSecretaryAction(); }; //老闆通知者 class Boss : public Subject { private: list<Observer*> myList; string _action; public: void Attach(Observer *pob); void Detach(Observer *pob); void Notify(); void SetSecretaryAction(string strAct); string GetSecretaryAction(); }; class Secretary { private: list<Observer*> myList; string _action; public: //增長 void Attach(Observer * pob); //減小 void Detach(Observer * pob); //通知 void Notify(); //前臺狀態 void SetSecretaryAction(string strAct); string GetSecretaryAction(); }; //Subject.cpp #include "Secretary.h" #include "Observer.h" void Subject::Attach(Observer *pob){} void Subject::Detach(Observer *pob){} void Subject::Notify(){} void Subject::SetSecretaryAction(string strAct){} string Subject::GetSecretaryAction(){return "";} //////////////// void Boss::Attach(Observer *pob) { myList.push_back(pob); } void Boss::Detach(Observer *pob) { myList.remove(pob); } void Boss::Notify() { list<Observer *>::iterator it = myList.begin(); for ( ; it != myList.end(); ++it ) { (*it)->Update(); } } void Boss::SetSecretaryAction(string strAct) { _action = strAct; } string Boss::GetSecretaryAction() { return _action; } /////////////// void Secretary::Attach(Observer * pob) { myList.push_back(pob); } //減小 void Secretary::Detach(Observer * pob) { myList.remove(pob); } //通知 void Secretary::Notify() { list<Observer *>::iterator it = myList.begin(); for ( ; it != myList.end(); ++it ) { (*it)->Update(); } } //前臺狀態 void Secretary::SetSecretaryAction(string strAct) { _action = strAct; } string Secretary::GetSecretaryAction() { return _action; } //----------------- //main.cpp #include "Secretary.h" #include "Observer.h" int main() { //老闆 Boss *pBoss = new Boss(); //看股票同事 StockObserver *pTongshi1 = new StockObserver("fun", pBoss); //看NBA同事 NBAObserver *pTongshi2 = new NBAObserver("Test", pBoss); pBoss->Attach(pTongshi1); pBoss->Attach(pTongshi2); pBoss->Detach(pTongshi1); //老闆回來 pBoss->SetSecretaryAction("我是老闆我回來了!"); //發出通知 pBoss->Notify(); return 0; }