C++设计模式中的观察者模式一起来看看

 更新时间:2022年03月13日 12:19:13   作者:贪心的葡萄  
这篇文章主要为大家详细介绍了C++观察者模式,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助

设计模式:观察者模式

观察者模式也称发布订阅模式,发布者发布消息,订阅者接收消息。

  • 发布者接口
#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
  • 发布者类
#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语言实现密码本小项目

    C语言实现密码本小项目

    这篇文章主要为大家详细介绍了C语言实现密码本小项目,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-02-02
  • C语言源码实现停车场管理系统

    C语言源码实现停车场管理系统

    这篇文章主要为大家详细介绍了C语言源码实现停车场管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    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语言实现将字符和数字串到一起

    C语言实现将字符和数字串到一起

    今天小编就为大家分享一篇C语言实现将字符和数字串到一起,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-12-12
  • 深入探讨linux下进程的最大线程数、进程最大数、进程打开的文件数

    深入探讨linux下进程的最大线程数、进程最大数、进程打开的文件数

    本篇文章是对linux下进程的最大线程数、进程最大数、进程打开的文件数进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • C语言程序设计谭浩强第五版课后答案(第三章习题答案)

    C语言程序设计谭浩强第五版课后答案(第三章习题答案)

    这篇文章主要介绍了C语言程序设计谭浩强第五版课后答案(第三章习题答案),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2021-04-04
  • C++程序中添加.c.h的实现方法

    C++程序中添加.c.h的实现方法

    这篇文章主要介绍了C++程序中添加.c.h的实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-08-08
  • C语言实现循环队列基本操作

    C语言实现循环队列基本操作

    这篇文章主要为大家详细介绍了C语言实现循环队列基本操作,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-09-09
  • OpenCV图像处理之常见的图像灰度变换

    OpenCV图像处理之常见的图像灰度变换

    这篇文章主要介绍了OpenCV图像处理之常见的图像灰度变换,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-07-07
  • C++11中移动构造函数案例代码

    C++11中移动构造函数案例代码

    C++11 标准中为了满足用户使用左值初始化同类对象时也通过移动构造函数完成的需求,新引入了 std::move() 函数,它可以将左值强制转换成对应的右值,由此便可以使用移动构造函数,对C++11移动构造函数相关知识感兴趣的朋友一起看看吧
    2023-01-01

最新评论