C++設(shè)計(jì)模式中的觀察者模式一起來看看
更新時(shí)間:2022年03月13日 12:19:13 作者:貪心的葡萄
這篇文章主要為大家詳細(xì)介紹了C++觀察者模式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
設(shè)計(jì)模式:觀察者模式
觀察者模式也稱發(fā)布訂閱模式,發(fā)布者發(fā)布消息,訂閱者接收消息。
- 發(fā)布者接口
#ifndef __observerPatterns_publish_hpp__ #define __observerPatterns_publish_hpp__ #include "observerPatterns_subscribe.hpp" class observerPatterns_publish { public: virtual void registerObjectSubscribe(observerPatterns_subscribe *ops) = 0; virtual void removeObjectSubscribe(observerPatterns_subscribe *ops) = 0; virtual void notifyObjectSubscribe() = 0; }; #endif
- 訂閱者接口
#ifndef __observerPatterns_subscribe_hpp__ #define __observerPatterns_subscribe_hpp__ #include "observerPatterns_common.hpp" class observerPatterns_subscribe { public: virtual void update(const observerPatterns_msgPackage &opmp) = 0; }; #endif
- 發(fā)布者類
#ifndef __observerPatterns_object_publish_hpp__ #define __observerPatterns_object_publish_hpp__ #include <unordered_set> #include "observerPatterns_publish.hpp" class observerPatterns_object_publish : public observerPatterns_publish { private: observerPatterns_msgPackage _opmp; std::unordered_set<observerPatterns_subscribe *> _subscribeObjectBucket; public: observerPatterns_object_publish(); ~observerPatterns_object_publish(); void registerObjectSubscribe(observerPatterns_subscribe *ops); void removeObjectSubscribe(observerPatterns_subscribe *ops); void notifyObjectSubscribe(); void setMsgPackage(const observerPatterns_msgPackage &opmp); }; observerPatterns_object_publish::observerPatterns_object_publish() { } observerPatterns_object_publish::~observerPatterns_object_publish() { } void observerPatterns_object_publish::registerObjectSubscribe(observerPatterns_subscribe *ops) { _subscribeObjectBucket.insert(ops); } void observerPatterns_object_publish::removeObjectSubscribe(observerPatterns_subscribe *ops) { _subscribeObjectBucket.erase(ops); } void observerPatterns_object_publish::notifyObjectSubscribe() { for (auto &&_sob : _subscribeObjectBucket) _sob->update(_opmp); } void observerPatterns_object_publish::setMsgPackage(const observerPatterns_msgPackage &opmp) { _opmp = opmp; notifyObjectSubscribe(); } #endif
- 訂閱者類
#ifndef __observerPatterns_object_subscribe_hpp__ #define __observerPatterns_object_subscribe_hpp__ #include "observerPatterns_publish.hpp" #include "observerPatterns_subscribe.hpp" class observerPatterns_object_subscribe : public observerPatterns_subscribe { private: observerPatterns_msgPackage _opmp; observerPatterns_publish *_opb; public: observerPatterns_object_subscribe(observerPatterns_publish *opb); ~observerPatterns_object_subscribe(); void update(const observerPatterns_msgPackage &opmp); }; observerPatterns_object_subscribe::observerPatterns_object_subscribe(observerPatterns_publish *opb) :_opb(opb) { _opb->registerObjectSubscribe(this); } observerPatterns_object_subscribe::~observerPatterns_object_subscribe() { _opb->removeObjectSubscribe(this); } void observerPatterns_object_subscribe::update(const observerPatterns_msgPackage &opmp) { _opmp = opmp; printf("url:%s, msg:%s\n", _opmp.url.c_str(), _opmp.msg.c_str()); } #endif
#ifndef __observerPatterns_object_subscribe2_hpp__ #define __observerPatterns_object_subscribe2_hpp__ #include "observerPatterns_publish.hpp" #include "observerPatterns_subscribe.hpp" class observerPatterns_object_subscribe2 : public observerPatterns_subscribe { private: observerPatterns_msgPackage _opmp; observerPatterns_publish *_opb; public: observerPatterns_object_subscribe2(observerPatterns_publish *opb); ~observerPatterns_object_subscribe2(); void update(const observerPatterns_msgPackage &opmp); }; observerPatterns_object_subscribe2::observerPatterns_object_subscribe2(observerPatterns_publish *opb) :_opb(opb) { _opb->registerObjectSubscribe(this); } observerPatterns_object_subscribe2::~observerPatterns_object_subscribe2() { _opb->removeObjectSubscribe(this); } void observerPatterns_object_subscribe2::update(const observerPatterns_msgPackage &opmp) { _opmp = opmp; printf("url:%s, msg:%s\n", _opmp.url.c_str(), _opmp.msg.c_str()); } #endif
- 公共頭文件
#ifndef __observerPatterns_common_hpp__ #define __observerPatterns_common_hpp__ #include <string> struct observerPatterns_msgPackage { std::string url; std::string msg; }; typedef struct observerPatterns_msgPackage observerPatterns_msgPackage; #endif
- 主函數(shù)測(cè)試
/************************************************************************ > File Name: observerPatterns_main.cpp > Author: > Mail: > Created Time: ************************************************************************/ #include "observerPatterns_common.hpp" #include "observerPatterns_object_publish.hpp" #include "observerPatterns_object_subscribe.hpp" #include "observerPatterns_object_subscribe2.hpp" using namespace std; int main(int argc, char *argv[]) { observerPatterns_object_publish *opop = new observerPatterns_object_publish; observerPatterns_msgPackage opmp1, opmp2; opmp1.url = "www.aaa.com"; opmp1.msg = "Hello!"; opmp2.url = "www.xyzxyz.com"; opmp2.msg = "Hello, observerPatterns!"; observerPatterns_object_subscribe *oposa = new observerPatterns_object_subscribe(opop); observerPatterns_object_subscribe *oposb = new observerPatterns_object_subscribe(opop); observerPatterns_object_subscribe2 *oposc = new observerPatterns_object_subscribe2(opop); observerPatterns_object_subscribe2 *oposd = new observerPatterns_object_subscribe2(opop); opop->setMsgPackage(opmp1); opop->setMsgPackage(opmp2); delete oposa; delete opop; return 0; }
相關(guān)文章
深入理解c++中char*與wchar_t*與string以及wstring之間的相互轉(zhuǎn)換
本篇文章是對(duì)c++中的char*與wchar_t*與string以及wstring之間的相互轉(zhuǎn)換進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05總結(jié)UNIX/LINUX下C++程序計(jì)時(shí)的方法
本文總結(jié)了下UNIX/LINUX下C++程序計(jì)時(shí)的一些函數(shù)和方法,對(duì)日常使用C++程序的朋友很有幫助,有需要的小伙伴們可以參考學(xué)習(xí),下面一起來看看吧。2016-08-08從匯編看c++函數(shù)的默認(rèn)參數(shù)的使用說明
本篇文章介紹了,在c++中函數(shù)的默認(rèn)參數(shù)的使用說明分析。需要的朋友參考下2013-05-05C++ 學(xué)習(xí)之旅 Windows程序內(nèi)部運(yùn)行原理
學(xué)習(xí)C++與.net不同的是,一定要搞清楚Windows程序內(nèi)部運(yùn)行原理,因?yàn)樗婕按蠖鄶?shù)是操作系統(tǒng)的調(diào)用,而.net畢竟是在.netFrameWork上唱戲2012-11-11