为什么人们在他们的代码中使用消息/事件总线? [英] Why people use message/event buses in their code?

查看:1770
本文介绍了为什么人们在他们的代码中使用消息/事件总线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为你听说过消息/事件总线,这是系统中所有事件流程的单一位置。在计算机主板和局域网中也有相似的架构。这对于主板和网络来说是一个很好的方法,因为它减少了电线的数量,但它对软件开发有好处吗?我们没有电子产品的限制。

I think that you have heard of message/event buses, it's the single place when all events in the system flow. Similar architectures are found in computer's motherboards and LAN networks. It's a good approach for motherboards and networks as it reduces the number of wires, but is it good for software development? We don't have such restrictions as electronics does.

消息总线/事件总线的最简单的实现可以是:

The simplest implementation of message bus/event bus can be like:

class EventBus {
    void addListener(EventBusListener l}{...}
    void fireEvent(Event e) {...}
}

使用bus.fireEvent(event)完成发布事件,接收消息由bus.addListener启用(听众),这样的架构有时用于软件开发,例如MVP4G为GWT实现类似的消息总线。

Posting events is done with bus.fireEvent(event), receiving messages is enabled by bus.addListener(listener). Such architectures are sometimes used for software development, for example MVP4G implements similar message bus for GWT.

活动项目:

  • Google Guava EventBus
  • MBassador by Benjamin Diedrichsen
  • Mycila PubSub by Mathieu Carbou
  • mvp4g Event Bus
  • Simple Java Event Bus

休眠/死亡项目:

  • Sun/Oracle JavaBeans InfoBus
  • https://eventbus.dev.java.net/ [Broken link]

只是流行的观察者(监听者)模式全局 - 每个对象在系统中可以听每个消息,我认为它是坏的,它打破了封装原则(每个对象都知道一切)和单一责任原则(例如,当一些对象需要一种新的消息类型时,事件总线通常需要改为例如在侦听器类中添加一个新的侦听器类或新方法。)

It's just the popular Observer (Listener) pattern made 'globally' - each object in the system can listen to each message, and I think it's bad, it breaks the Encapsulation principle (each object knows about everything) and Single Responsibility principle (eg when some object needs to a new type of message, event bus often needs to be changed for example to add a new Listener class or a new method in the Listener class).

由于这些原因,我认为,对于大多数软件,Observer模式优于事件总线。您对事件总线的看法如何?对于典型的应用程序来说,它有什么好的意义吗?

For these reasons I think, that for most software, Observer pattern is better than event bus. What do you think about event bus, does it make any good sense for typical applications?

编辑:我不是在谈论ESB这样的大型企业解决方案 - 他们可以是有用的(ESB提供的更多,不仅仅是一个事件总线)。我在询问在常规Java代码中使用消息总线的对象到对象连接的有用性 - 有些人做到这一点,检查上面的链接。事件总线可能是电话到电话通信或计算机到计算机通信的最佳解决方案,因为网络中的每个电话(或计算机)通常可以相互通信,并且总线减少了电线数量。但对象很少相互对话 - 一个对象可以拥有多少个协作者 - 3,5?

I'm not talking about 'big' enterprise solutions like ESB - they can be useful (what's more ESB offers much, much more than just an event bus). I'm asking about usefulness of using message bus in 'regular' Java code for object-to-object connection - some people do it, check the links above. Event bus is probably best solution for telephone-to-telephone communication or computer-to-computer communication because each telefone (or computer) in a network can typically talk to each other, and bus reduces the number of wires. But objects rarely talk to each other - how many collaborators one object can have - 3, 5?

推荐答案

有些人喜欢它,因为这是门面图案。org / wiki / Mediator_pattern =noreferrer>它集中了诸如记录,警报,监控,安全性等交叉活动。

Some people like it because it is the embodiment of the Facade pattern or Mediator pattern. It centralizes cross-cutting activities like logging, alerting, monitoring, security, etc.

有些人不喜欢它,因为它通常是一个单身故障点。每个人都必须了解它。

Some people don't like it because it is often a Singleton point of failure. Everyone has to know about it.

这篇关于为什么人们在他们的代码中使用消息/事件总线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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