观察者模式也称发布订阅模式,发布者发布消息,订阅者接收消息。
发布者接口#ifndef __obrverpatterns_publish_hpp__#define __obrverpatterns_publish_hpp__#include "obrverpatterns_subscribe.hpp"class obrverpatterns_publish{public: virtual void registerobjectsubscribe(obrverpatterns_subscribe *ops) = 0; virtual void removeobjectsubscribe(obrverpatterns_subscribe *ops) = 0; virtual void notifyobjectsubscribe() = 0;};#endif订阅者接口
#ifndef __obrverpatterns_subscribe_hpp__#define __obrverpatterns_subscribe_hpp__#include "obrverpatterns_common.hpp"class obrverpatterns_subscribe{public: virtual void update(const obrverpatterns_msgpackage &opmp) = 0;};#endif发布者类
#ifndef __obrverpatterns_object_publish_hpp__#define __obrverpatterns_object_publish_hpp__#include <unordered_t>#include "obrverpatterns_publish.hpp"class obrverpatterns_object_publish : public obrverpatterns_publish{private: obrverpatterns_msgpackage _opmp; std::unordered_t<obrverpatterns_subscribe *> _subscribeobjectbucket;public: obrverpatterns_object_publish(); ~obrverpatterns_object_publish(); void registerobjectsubscribe(obrverpatterns_subscribe *ops); void removeobjectsubscribe(obrverpatterns_subscribe *ops); void notifyobjectsubscribe(); void tmsgpackage(const obrverpatterns_msgpackage &opmp);};obrverpatterns_object_publish::obrverpatterns_object_publish(){}obrverpatterns_object_publish::~obrverpatterns_object_publish(){}void obrverpatterns_object_publish::registerobjectsubscribe(obrverpatterns_subscribe *ops){ _subscribeobjectbucket.inrt(ops);}void obrverpatt一只绣花鞋erns_object_publish::removeobjectsubscribe(obrverpatterns_subscribe *ops){ _subscribeobjectbucket.era(ops);}void obrverpatterns_object_publish::notifyobjectsubscribe(){ for (auto &&_sob : _subscribeobjectbucket) _sob->update(_opmp);}void obrverpatterns_object_publish::tmsgpackage(const obrverpatterns_msgpackage &opmp){ _opmp = opmp; notifyobjectsubscribe();}#endif订阅者类
#ifndef __obrverpatterns_object_subscribe_hpp__#define __obrverpatterns_object_subscribe_hpp__#include "obrverpatterns_publish.hpp"#include "obrverpatterns_subscribe.hpp"class obrverpatterns_object_subscribe : public obrverpatterns_subscribe{private: obrverpatterns_msgpackage _opmp; obrverpatterns_publish *_opb;public: obrverpatterns_object_subscribe(obrverpatterns_publish *opb); ~obrverpatterns_object_subscribe(); void update(c被子植物生殖过程onst obrverpatterns_msgpackage &opmp);};obrverpatterns_object_subscribe::obrverpatterns_object_subscribe(obrverpatterns_publish *opb) :_opb(opb){ _opb->registerobjectsubscribe(this);}obrverpatterns_object_subscribe::~obrverpatterns_object_subscribe(){ _怎么破解空间相册密码opb->removeobjectsubscribe(this);}void obrverpatterns_object_subscribe::update(const obrverpatterns_msgpackage &opmp){ _opmp = opmp; printf("url:%s, msg:%s\n", _opmp.url.c_str(), _opmp.msg.c_str());}#endif
#ifndef __obrverpatterns_object_subscribe2_hpp__#define __obrverpatterns_object_subscribe2_hpp__#include "obrverpatterns_publish.hpp"#include "obrverpatterns_subscribe.hpp"class obrverpatterns_object_subscribe2 : public obrverpatterns_subscribe{private: obrverpatterns_msgpackage _opmp; obrverpatterns_publish *_opb;public: obrverpatterns_object_subscribe2(obrverpatterns_publish *opb); ~obrverpatterns_object_subscribe2(); void update(const obrverpatterns_msgpackage &opmp);};obrverpatterns_object_subscribe2::obrverpatterns_object_subscribe2(obrverpatterns_publish *opb) :_opb(opb){ _opb->registerobjectsubscribe(this);}obrverpatterns_object_subscribe2::~obrverpatterns_object_subscribe2(){ _opb->removeobjectsubscribe(this);}void obrverpatterns_object_subscribe2::update(const obrverpatterns_msgpackage &opmp){ _opmp = opmp; printf("url:%s, msg:%s\n", _opmp.url.c_str(), _opmp.msg.c_str());}#endif公共头文件
#ifndef __obrverpatterns_common_hpp__#define __obrverpatterns_common_hpp__#include <string>struct obrverpatterns_msgpackage{ std::string url; std::string 等待绽放msg;};typedef struct obrverpatterns_msgpackage obrverpatterns_msgpackage;#endif主函数测试
/************************************************************************ > file name: obrverpatterns_main.cpp > author: > mail: > created time: ************************************************************************/#include "obrverpatterns_common.hpp"#include "obrverpatterns_object_publish.hpp"#include "obrverpatterns_object_subscribe.hpp"#include "obrverpatterns_object_subscribe2.hpp"using namespace std;int main(int argc, char *argv[]){ obrverpatterns_object_publish *opop = new obrverpatterns_object_publish; obrverpatterns_msgpackage opmp1, opmp2; opmp1.url = "www.aaa.com"; opmp1.msg = "hello!"; opmp2.url = "www.xyzxyz.com"; opmp2.msg = "hello, obrverpatterns!"; obrverpatterns_object_subscribe *oposa = new obs如烟往事俱忘却erverpatterns_object_subscribe(opop); obrverpatterns_object_subscribe *oposb = new obrverpatterns_object_subscribe(opop); obrverpatterns_object_subscribe2 *oposc = new obrverpatterns_object_subscribe2(opop); obrverpatterns_object_subscribe2 *oposd = new obrverpatterns_object_subscribe2(opop); opop->tmsgpackage(opmp1); opop->tmsgpackage(opmp2); delete oposa; delete opop; return 0;}
本文发布于:2023-04-06 03:36:22,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/a3b4fbeceac9de23acfa52b61fd9d4d1.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:C++设计模式中的观察者模式一起来看看.doc
本文 PDF 下载地址:C++设计模式中的观察者模式一起来看看.pdf
留言与评论(共有 0 条评论) |