欢迎来到天天文库
浏览记录
ID:38527750
大小:47.00 KB
页数:4页
时间:2019-06-14
《设计模式-Observer》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、MVC结构便是一个经典的Observer的实例。Observer模式要解决的问题就是:建立一个Subject对多个Observer的依靠关系,并且做到当S变化的时候,依靠这个S的诸多O也能够同步改变;一个简单的例子就是,当一组数据变化的时候,所有统计数据的显示方法也需要同时改变。 Observer维护着一个Subject的指针,所以可以直接调用或者由Sub直接NOTIFY各个OBSERVERS.所以该模式也被称为发布-订阅模式,在JAVA中有其固定的MVC框架,并且提供了OBSERVER模式的实现接口。 #include#include2、g>#includeusingnamespacestd;typedefstringstates; classObserver;classSubjects{public: virtual~Subjects(); virtualvoidAttatch(Observer*obs); virtualvoidDetatch(Observer*obs); virtualvoidNotify(); virtualvoidSetState(conststates&)=0; virtualstatesGetState()=0;private: list*m_ob3、s;//SAVEALLTHEOBSERVERSprotected: Subjects();}; classConSubjuects:publicSubjects{public: ConSubjuects(){m_st=' ';} ~ConSubjuects(){} statesGetState(){returnm_st;} voidSetState(conststates&st){m_st=st;}private: statesm_st;}; classObserver{public: virtual~Observer(){}; virtualvoidUpdate(Sub4、jects*sub)=0; virtualvoidPrintInfo()=0;protected: Observer(){m_st=' ';}; statesm_st;}; classConAObserver:publicObserver{public: virtualSubjects*GetSubject() {returnm_sub;} ConAObserver(Subjects*sub) { m_sub=sub; m_sub->Attatch(this); } virtual~ConAObserver() { m_sub->Detatch(this); if(5、!m_sub)deletem_sub; } voidUpdate(Subjects*sub) { m_st=sub->GetState(); PrintInfo(); } voidPrintInfo() { cout<<"AOPER"<GetState()<6、_sub=sub; m_sub->Attatch(this); } virtual~ConBObserver() { m_sub->Detatch(this); if(!m_sub)deletem_sub; } voidUpdate(Subjects*sub) { m_st=sub->GetState(); PrintInfo(); } voidPrintInfo() { cout<<"BOPER"<GetState()<7、ewlist;}Subjects::~Subjects(){}voidSubjects::Attatch(Observer*obs){ m_obs->push_front(obs);}voidSubjects::Detatch(Observer*obs){ if(!obs)m_obs->remove(obs);}voidSubjects::Notify(){ list::iteratorit; it=m_obs->begin(); for(;it!=m_obs->end
2、g>#includeusingnamespacestd;typedefstringstates; classObserver;classSubjects{public: virtual~Subjects(); virtualvoidAttatch(Observer*obs); virtualvoidDetatch(Observer*obs); virtualvoidNotify(); virtualvoidSetState(conststates&)=0; virtualstatesGetState()=0;private: list*m_ob
3、s;//SAVEALLTHEOBSERVERSprotected: Subjects();}; classConSubjuects:publicSubjects{public: ConSubjuects(){m_st=' ';} ~ConSubjuects(){} statesGetState(){returnm_st;} voidSetState(conststates&st){m_st=st;}private: statesm_st;}; classObserver{public: virtual~Observer(){}; virtualvoidUpdate(Sub
4、jects*sub)=0; virtualvoidPrintInfo()=0;protected: Observer(){m_st=' ';}; statesm_st;}; classConAObserver:publicObserver{public: virtualSubjects*GetSubject() {returnm_sub;} ConAObserver(Subjects*sub) { m_sub=sub; m_sub->Attatch(this); } virtual~ConAObserver() { m_sub->Detatch(this); if(
5、!m_sub)deletem_sub; } voidUpdate(Subjects*sub) { m_st=sub->GetState(); PrintInfo(); } voidPrintInfo() { cout<<"AOPER"<GetState()<6、_sub=sub; m_sub->Attatch(this); } virtual~ConBObserver() { m_sub->Detatch(this); if(!m_sub)deletem_sub; } voidUpdate(Subjects*sub) { m_st=sub->GetState(); PrintInfo(); } voidPrintInfo() { cout<<"BOPER"<GetState()<7、ewlist;}Subjects::~Subjects(){}voidSubjects::Attatch(Observer*obs){ m_obs->push_front(obs);}voidSubjects::Detatch(Observer*obs){ if(!obs)m_obs->remove(obs);}voidSubjects::Notify(){ list::iteratorit; it=m_obs->begin(); for(;it!=m_obs->end
6、_sub=sub; m_sub->Attatch(this); } virtual~ConBObserver() { m_sub->Detatch(this); if(!m_sub)deletem_sub; } voidUpdate(Subjects*sub) { m_st=sub->GetState(); PrintInfo(); } voidPrintInfo() { cout<<"BOPER"<GetState()<7、ewlist;}Subjects::~Subjects(){}voidSubjects::Attatch(Observer*obs){ m_obs->push_front(obs);}voidSubjects::Detatch(Observer*obs){ if(!obs)m_obs->remove(obs);}voidSubjects::Notify(){ list::iteratorit; it=m_obs->begin(); for(;it!=m_obs->end
7、ewlist;}Subjects::~Subjects(){}voidSubjects::Attatch(Observer*obs){ m_obs->push_front(obs);}voidSubjects::Detatch(Observer*obs){ if(!obs)m_obs->remove(obs);}voidSubjects::Notify(){ list::iteratorit; it=m_obs->begin(); for(;it!=m_obs->end
此文档下载收益归作者所有