#ifndef __OBSERVER__H#define __OBSERVER__H//observer 接口,继承者需要实现Update方法class Observer{public:virtual ~Observer(){}virtual void Update(Subject* pChange) = 0;protected:Observer(){}};//Sky类class Sky : public Observer{private://具体的目标对象指针,是通过它的状态来改变观察者的行为//可以有多个具体的目标对象,不过这里我们提供一个Person* m_pPerson;public:Sky(Person *pPerson){m_pPerson = pPerson;m_pPerson->Attach(this);}virtual ~Sky(){m_pPerson->Detach(this);}void Update(Subject *pChange){//由于可能有多个,所以这里需要判断if ( pChange == m_pPerson ){if ( m_pPerson->GetSpirit() == true ){cout << "天空是蓝色的!" << endl;}else{cout << "乌云密布!" << endl;}}}};//Flower类class Flower : public Observer{private:Person* m_pPerson;public:Flower(Person *pPerson){m_pPerson = pPerson;m_pPerson->Attach(this);}virtual ~Flower(){m_pPerson->Detach(this);}void Update(Subject *pChange){if ( pChange == m_pPerson ){if ( m_pPerson->GetSpirit() == true ){cout << "百花灿烂时!" << endl;}else{cout << "凋零的玫瑰!" << endl;}}}};#endif
Subject.h文件的内容:#ifndef __SUBJECT_HEADER__H#define __SUBJECT_HEADER__H//由于类Observer和Subject需要嵌套调用,所以不能直接#include"observer.h"//所以需要先申明一下class Observer;//目标接口class Subject{public://添加一个观察者virtual void Attach(Observer *);//删除一个观察者virtual void Detach(Observer *);//通知观察者virtual void Notify();protected:Subject(){}private://观察者列表list m_ObserverList;};//具体的目标类class Person : public Subject{private:bool m_bGoodSpirit;public:Person():m_bGoodSpirit(true){}bool GetSpirit(){return m_bGoodSpirit;}void GoodSpirit(){m_bGoodSpirit = true;cout << "我的心情很好,所以我看到了:" << endl;Notify();}void BadSpirit(){m_bGoodSpirit = false;cout << "我的心情很不好,所以我看到了:" << endl;Notify();}};#endif
#include "Subject.h"#include "Observer.h"void Subject::Attach(Observer *pObserver){m_ObserverList.push_back(pObserver);}void Subject::Detach(Observer *pObserver){m_ObserverList.remove(pObserver);}void Subject::Notify(){list::iterator ite;//遍历所有的观察者就行通知for( ite = m_ObserverList.begin() ; ite != m_ObserverList.end() ;++ite)(*ite)->Update(this);}
#include "Observer.h"#include "Subject.h"int main(int argc, char* argv[]){Person p;Sky sky(&p);Flower flower(&p);p.GoodSpirit();p.BadSpirit();return 0;}
template<class Item>class Iterator{public:virtual bool hasNext() = 0;virtual Item Next() = 0;};
template<class Item>class ArrayIterator : public Iterator - {private://指向数组的指针Item *m_pItems;//当前迭代器的位置int m_iPosition;//数组的最大长度int m_iMaxSize;public://初始化函数ArrayIterator(Item *pItems,int iMaxSize):m_pItems(pItems),m_iPosition(0),m_iMaxSize(iMaxSize){}bool hasNext(){//判断当前的元素是否有效if ( m_iPosition < m_iMaxSize && m_pItems[m_iPosition] != NULL ){return true;}else{return false;}}Item Next(){//返回当前的元素,并将位置加一return m_pItems[m_iPosition++];}};
//目标接口class Subject{public://添加一个观察者virtual void Attach(Observer *);//删除一个观察者virtual void Detach(Observer *);//通知观察者virtual void Notify();//创建迭代器Iterator<Observer*> *CreateIterator(){return new ArrayIterator<Observer*>(m_ObserverList,10);}protected:Subject():m_iPosition(0){for(int i = 0; i < 10 ; i++ )m_ObserverList[i] = NULL;}private:int m_iPosition;//观察者列表Observer* m_ObserverList[10];};
//创建迭代器Iterator<Observer*> *CreateIterator(){return new ArrayIterator<Observer*>(m_ObserverList,10);}
void Subject::Notify(){//遍历所有的观察者就行通知Iterator<Observer*> *pObserver = CreateIterator();while( pObserver->hasNext() ){pObserver->Next()->Update(this);}delete pObserver;}
参考书目:1, 设计模式——可复用面向对象软件的基础(Design Patterns ——Elements of Reusable Object-Oriented Software) Erich Gamma 等著 李英军等译 机械工业出版社2, Head First Design Patterns(影印版)Freeman等著 东南大学出版社3, 道法自然——面向对象实践指南 王咏武 王咏刚著 电子工业出版社4, 原来诗经可以这么读 唐文著 河北教育出版社
联系客服