发布者订阅者模式的现代替代方案 [英] Modern alternative to publisher subscriber pattern

查看:133
本文介绍了发布者订阅者模式的现代替代方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C ++ Windows应用程序.我正在处理发布者-订阅者的情况,其中我的一个类(发布者)会定期生成数据,并将其交给另一个类(订阅者),该类一直在等待接收来自发布者的通知.我对设计模式不熟悉,我查找了发布者订户模型的常见实现,我发现它们通常很老,并且通常涉及维护指向对象的指针列表.我想知道是否有使用C ++ 11编码发布者订户模型的更好方法,或者完全使用完全不同的模型代替发布者-订户. 如果您可以命名一些有趣的功能或方法,我将阅读它们的文档,编写实现并将其添加到此处以供进一步检查.

I have a C++ Windows application. I am dealing with a publisher-subscriber situation, where one of my classes (publisher) generates data periodically and hands it off to another class (subscriber) which is constantly waiting to receive notification from the publisher. I am new to design patterns, and I looked up common implementations for publisher subscriber models, I noticed they are usually quite old, and they usually involve maintaining lists of pointers to objects. I was wondering if there is a better way of coding publisher subscriber model using C++ 11. Or using an entirely different model altogether in place of publisher - subscriber. If you can name some interesting features or approaches, I will read documentation for them, write an implementation and add it here for further review.

更新:我说过要发布示例代码.首先,Jens推荐的Boost Signals 2确实很棒.我的代码与 http: //www.boost.org/doc/libs/1_55_0/doc/html/signals2/tutorial.html

Update : I said I would post sample code. First, Boost Signals 2 as recommended by Jens really works great. My code is not quite different from the beginner sections on http://www.boost.org/doc/libs/1_55_0/doc/html/signals2/tutorial.html

推荐答案

看看信号库,例如 Boost.Signals2

Take a look at a signal library, e.g. Boost.Signals2 or libsigc++. They provide an abstraction to define a signal in your class to which clients can connect. All the logic to store connections etc is implemented there.

这篇关于发布者订阅者模式的现代替代方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆