the Reactor Pattern from An Object Behavioral Pattern for Demultiplexing and dispatching Handles for Synchronous Events
intent:
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.
Reactor Pattern 为了处理一个或者多个客户端并发分发到应用程序的请求.
应用程序由许多不同的方法组成来处理客户端端每一个特殊的请求.通过初始化一个dispatcher来管理注册的事件,同时负责分发请求.
多路复用服务器请求是通过一个同步的事件分发器.
Benefits
The Reactor pattern offers the following benefits:
Separation of concerns:
The Reactor pattern decouples application-independent demultiplexing and dispatching mechanisms from application-specific hook method functionality.
The application-independent mechanisms become reusable components that kNow how to demultiplex events and dispatch the appropriate hook methods defined by Event Handlers.
In contrast,the application-specific functionality in a hook method kNows how to perform a particular type of service.
分离关注点:
Reactor Pattern 通过在应用程序中指定函数把多路复用和分发器分离机制来解耦合.
应用程序独立机制变成了可以用组件因为事件被分发到不同的处理函数.
总之,应用程序指定函数来处理不同的服务器请求.
Improve modularity,reusability,and configurability of event-driven applications:
The pattern decouples application functionality into separate classes.
For instance,there are two separate classes in the logging server: one for establishing connections and another for receiving and processing logging records.
This decoupling enables the reuse of the connection establishment class for different types of connection-oriented services
(such as file transfer,remote login,and video-on-demand).
Therefore,modifying or extending the functionality of the logging server on only affects the implementation of the logging handler class.
增强模块型,重用行和灵活配置型的事件驱动程序:
Reactor Pattern 把应用程序分离成不同的部分:
例如,在LoggingServer 中把程序分离成2个部分,一部分处理链接的建立(Socket的建立),另外一部分来处理日志的记录.(Socket I/O 输入输出部分)
对于不同类型的面向链接的服务,分离可以重用创建链接的部分.例如:文件传输,远程登录,多媒体播放.
所有,修改和拓展LoggingServer的功能仅仅影响Logging handler 实现部分.
Improves application portability:
The Initiation dispatcher’s interface can be reused independently of the OS system calls that perform event demultiplexing.
These system calls detect and report the occurrence of one or more events that may occur simultaneously on multiple sources of events.
Common sources of events may in-clude I/O handles,timers,and synchronization objects.
On UNIX platforms,the event demultiplexing system calls are called select and poll .
In the Win32 API,the WaitForMultipleObjects system call performs event demultiplexing.
提高应用程序的可移植性:
在执行事件的多路复用部分,可以根据不同的操作系统调用不同的函数来初始化dispatcher.
在Unix 操作系统上,事件的多路复用可以使用select() 和Poll()函数.
在win32 API中,可以使用WaitForMultipleObjects()来调用事件的多路复用.
Provides coarse-grained concurrency control:
The Reactor pattern serializes the invocation of event handlers at the level of event demultiplexing and dispatching within a process or thread.
Serialization at the Initiation dispatcher level often eliminates the need for more complicated synchronization or locking within an application process.
提供粗粒度的并发控制: Reactor Pattern 模型在一个线程或者一个进程中在多路复用和分发器的级别上序列化事件处理(event handler)的激活. 在应用程序进程中在初始化dispatcher的级别上序列化将消除对于复杂的同步和锁的使用.