Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Observer Pattern vs Reactor Pattern

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?

Thanks, Carlo

like image 854
Carlo Bertuccini Avatar asked Oct 19 '14 19:10

Carlo Bertuccini


1 Answers

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.

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.

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).

like image 50
Floegipoky Avatar answered Oct 23 '22 05:10

Floegipoky