Simple Explanation for the "Reactor Pattern" with its Applications

Design Patterns

Design Patterns Problem Overview


Reactor pattern is explained in wikipedia, and it's a bit too abstract. Can you describe this pattern in a more concrete way? Ideally with code snippets or high-level class diagrams describing some applications of reactor pattern.

Design Patterns Solutions


Solution 1 - Design Patterns

You might want to check the original paper describing it http://www.dre.vanderbilt.edu/~schmidt/PDF/reactor-siemens.pdf

> The Reactor design pattern handles service requests that are delivered concurrently to an application by one or more clients. Each service in an application may consist of serveral methods and is represented by a separate event handler that is responsible for dispatching service-specific requests. Dispatching of event handlers is performed by an initiation dispatcher, which manages the registered event handlers. Demultiplexing of service requests is performed by a synchronous event demultiplexer.

Solution 2 - Design Patterns

A reactor allows multiple tasks which block (say due to IO) to be processed efficiently using a single thread. The reactor manages a pool of handlers and runs an event loop. When it is called to perform a task it links it with a new or vacant handler making it active. The event loop (1) finds all the handlers that are active and unblocked (or delegates this to a dispatcher implementation) (2) executes each of these found handlers sequentially until they either complete or reach a point where they block. Completed handlers become inactive and vacant for reuse whereas blocked active handlers yield, allowing the event loop to continue. (3) Repeats from step (1)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionElecoView Question on Stackoverflow
Solution 1 - Design PatternsreeseView Answer on Stackoverflow
Solution 2 - Design Patternsandrew pateView Answer on Stackoverflow