观察者模式vs反应器模式 [英] Observer Pattern vs Reactor Pattern

查看:148
本文介绍了观察者模式vs反应器模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在学习这两种方法,但没有发现真正的区别,我错过了什么吗?
在网络上有人说观察员只能处理一个事件,而反应堆却可以处理多个事件,但我认为这并不是真正的区别,因为可以像许多观察员一样看到反应堆。有什么区别还是新名称Reactor比Observer更酷?

I've been studying both but could not find any real difference, am I missing something? On the web some people say the Observer should handle only one event while the Reactor many -- but I don't consider this a real difference because a Reactor could be seen just like many Observer. Is there any difference or the newer name Reactor is just more cool than Observer?

谢谢,
Carlo

Thanks, Carlo

推荐答案

反应器模式利用中介服务处理程序,该处理程序对请求进行多路分解并分派到正确的处理程序。

The Reactor Pattern utilizes an intermediary service handler which demultiplexes requests and dispatches to the correct handler.

观察者模式要求观察者向该主题注册,然后在事件发生时向所有注册的观察者推送通知。

The Observer Pattern requires that "Observers" register with the subject, which then pushes notifications to all registered observers when an event occurs.

重要的区别在于,Reactor模式使用中央请求处理程序处理调度,而观察者模式则允许消费者直接与生产者对话。

The important difference is that the Reactor Pattern handles dispatching with a central request handler, while the Observer Pattern lets the consumers talk to the producers directly.

实际上,观察者模式可以在动态注册和注销使用者时提供更大的灵活性。在非常高的并发应用程序中,它还提供了一个优点,即没有单个中间调度程序会阻塞吞吐量。 监听器问题也很重要,当消费者忘记注销主题时会发生当他们不再希望收到通知时。在垃圾回收语言中,这通常会阻止GC收集这些事件处理程序,从而泄漏它们以及它们所保存的所有引用。使用Reactor模式通常是不可能的,因为它通常不使用动态注册(尽管理论上可以使用)。

In practice, the Observer Pattern can provide more flexibility in dynamically registering and deregistering consumers. In very high concurrency applications it also offers the benefit of not having a single intermediate dispatcher bottlenecking the throughput. The Lapsed Listener Problem is also relevant, which occurs when consumers forget to de-register with the subject when they no longer want to be notified. In garbage collected languages, this often prevents the GC from being able to collect these event handlers and thus leak them and any references that they hold. This is not typically possible with the Reactor Pattern, which usually doesn't utilize dynamic registration (though it could theoretically).

这篇关于观察者模式vs反应器模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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