Eventbus 是中介者模式还是观察者模式?

发布于 2024-11-07 05:41:07 字数 162 浏览 0 评论 0原文

Eventbus 更像是中介者还是观察者?据谷歌称, “eventbus mediator”获得 2.430 次点击, “eventbus观察者”获得 3.850 次点击。

从描述来看,它们都符合我想要做的事情(调解器甚至更多)。 那么 eventbus 是否实现了特定的模式,还是由我来决定?

Is Eventbus more a Mediator or an Observer? According to Google,
"eventbus mediator" gets 2.430 hits and
"eventbus observer" gets 3.850 hits.

From the description, they would both match what I was trying to do (the mediator even a little more).
So does eventbus implement a specific pattern or is it up to me which I say it is?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

苦笑流年记忆 2024-11-14 05:41:07

通常,一段给定的代码本质上并不是一种模式或另一种模式的示例。这就是为什么它们被称为“模式”(而不是“实现技术”)。很多软件看起来有点像一种模式,但也类似于另一种模式——这很好。最好不要为了模式而遵循模式,而是将它们用作讨论架构的共享词汇。

EventBus 就是这样的工具之一。我在编写它时考虑了类似观察者的情况,但如果您适当地构建应用程序,它可以发挥类似中介者的作用。

Often, a given piece of code isn't intrinsically an example of one pattern or another. This is why they're called "patterns" (rather than, say, "implementation techniques"). A lot of software kinda looks like one pattern, but also resembles another -- this is good. It's best not to adhere to patterns for patterns' sake, but to use them as a shared vocabulary for discussing architecture.

EventBus is one such tool. I wrote it with Observer-like situations in mind, but if you structure your application appropriately it can play a Mediator-like role.

最好是你 2024-11-14 05:41:07

EventBus 的一般用法是触发事件。使用“观察者”这个词更适合这一点。观察者模式使用事件或消息来通知有关正在观察(更改)的对象的感兴趣对象的更改。
Mediator 还尝试将这两个实现解耦,但在某种意义上比 Observer 更具体,它可以了解这两个对象/接口的所有信息,并充当使这两个对象/接口工作的粘合剂。
观察者并不声称了解内部结构,甚至界面。它所知道或关心的是当事件发生时,它需要通知感兴趣的对象。

中介者可能是特定于场景的设置,而观察者可能更通用。

EventBus 在应用程序范围内几乎总是一个单例,我肯定会将 EventBus 归类为使用观察者,因为在大多数情况下,它的真正目的是促进运行时内的各个模块/对象之间的全局消息传递。

The general usage of EventBus is to fire events. Use of the word Observer is better fit for that. Observer pattern uses events or messages to notify of a change to objects of interest about the object being observed(changed).
Mediator also tries to de-couple the two implementations but is more concrete than Observer in the sense, it can know all about the two objects/interfaces and works as a glue to make those two work.
Observer doesn't claim to know about the internals or even the interface. All it knows or cares about is when the event occurs, it needs to notify the objects who are interested.

Mediator could be a scenario specific setup whereas the Observer could be more generic.

EventBus being almost always a singleton within the scope of the application, I would definitely categorise EventBus as using Observer as its real intent in most cases is to facilitate messaging globally amongst the various modules/objects within your runtime.

瑶笙 2024-11-14 05:41:07

我想说,典型的事件总线利用了这两种模式:

  • 事件总线本质上封装了其他对象的通信方式,因此它一个中介者
  • ,注册为事件处理程序/侦听器的对象是观察者(并且主体他们的观察结果是事件类型和/或产生这些事件的对象,不是总线本身),因此 维基百科说观察者模式主要用于实现分布式事件处理系统”< /em>(强调我的),但事件总线本身不是观察者。

I'd say that a typical event bus utilises both of these patterns:

  • event bus essentially encapsulates how other objects communicate, so it is a mediator
  • objects that register as event handlers/listeners are observers (and the subjects of their observations are event types and/or objects that produce these events, not the bus itself), so as wikipedia says observer pattern "is mainly used to implement distributed event handling systems" (emphasis mine), but an event bus is not an observer per se.
离旧人 2024-11-14 05:41:07

维基百科:Mediator 模式的本质是“定义一个对象来封装一组对象如何交互”,

EventBus 不这样做。

EventBus 也不是观察者模式,因为如果你有 N 个对象,并且想要在所有对象之间进行通信,那么如果你使用观察者模式,则需要 N*N 个观察者,但只有一个全局 EventBus 就足以完成相同的工作。

所以EventBus 就是EventBus 模式。

wikipedia: The essence of the Mediator Pattern is to "Define an object that encapsulates how a set of objects interact"

EventBus does not do this.

EventBus is also not observer pattern because if you have N objects and you want to communicate between all of them you need N*N observers if you use the observer pattern but only one global EventBus is enough to do the same job.

So EventBus is THE EventBus pattern.

鹿! 2024-11-14 05:41:07

由于前言说“一个 [...] 发布/订阅 API”,我会选择 Observer。

Since the foreword says "a [...] publish/subscribe API", I'd go for Observer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文