最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++設計模式中的觀察者模式一起來看看

 更新時間:2022年03月13日 12:19:13   作者:貪心的葡萄  
這篇文章主要為大家詳細介紹了C++觀察者模式,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助

設計模式:觀察者模式

觀察者模式也稱發(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
  • 主函數測試
/************************************************************************
    > 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;
}

相關文章

  • C語言實現(xiàn)密碼本小項目

    C語言實現(xiàn)密碼本小項目

    這篇文章主要為大家詳細介紹了C語言實現(xiàn)密碼本小項目,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • C語言源碼實現(xiàn)停車場管理系統(tǒng)

    C語言源碼實現(xiàn)停車場管理系統(tǒng)

    這篇文章主要為大家詳細介紹了C語言源碼實現(xiàn)停車場管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • C++迭代器介紹(iterator、const_iterator、reverse_interator、const_reverse_interator)

    C++迭代器介紹(iterator、const_iterator、reverse_interator、const_rev

    這篇文章主要介紹了C++迭代器介紹(iterator、const_iterator、reverse_interator、const_reverse_interator),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-02-02
  • C語言實現(xiàn)將字符和數字串到一起

    C語言實現(xiàn)將字符和數字串到一起

    今天小編就為大家分享一篇C語言實現(xiàn)將字符和數字串到一起,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • 深入探討linux下進程的最大線程數、進程最大數、進程打開的文件數

    深入探討linux下進程的最大線程數、進程最大數、進程打開的文件數

    本篇文章是對linux下進程的最大線程數、進程最大數、進程打開的文件數進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05
  • C語言程序設計譚浩強第五版課后答案(第三章習題答案)

    C語言程序設計譚浩強第五版課后答案(第三章習題答案)

    這篇文章主要介紹了C語言程序設計譚浩強第五版課后答案(第三章習題答案),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2021-04-04
  • C++程序中添加.c.h的實現(xiàn)方法

    C++程序中添加.c.h的實現(xiàn)方法

    這篇文章主要介紹了C++程序中添加.c.h的實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • C語言實現(xiàn)循環(huán)隊列基本操作

    C語言實現(xiàn)循環(huán)隊列基本操作

    這篇文章主要為大家詳細介紹了C語言實現(xiàn)循環(huán)隊列基本操作,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • OpenCV圖像處理之常見的圖像灰度變換

    OpenCV圖像處理之常見的圖像灰度變換

    這篇文章主要介紹了OpenCV圖像處理之常見的圖像灰度變換,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-07-07
  • C++11中移動構造函數案例代碼

    C++11中移動構造函數案例代碼

    C++11 標準中為了滿足用戶使用左值初始化同類對象時也通過移動構造函數完成的需求,新引入了 std::move() 函數,它可以將左值強制轉換成對應的右值,由此便可以使用移動構造函數,對C++11移動構造函數相關知識感興趣的朋友一起看看吧
    2023-01-01

最新評論

金平| 库尔勒市| 静乐县| 游戏| 广东省| 平昌县| 保德县| 明星| 葫芦岛市| 长乐市| 勃利县| 积石山| 南和县| 永吉县| 南充市| 乐清市| 光泽县| 积石山| 陆良县| 当雄县| 乐至县| 漳浦县| 改则县| 上林县| 泾源县| 漳平市| 湘潭县| 息烽县| 开原市| 泰州市| 象州县| 客服| 东山县| 手机| 东丰县| 娄底市| 台东县| 永康市| 惠安县| 简阳市| 松潘县|