Java高级程序设计

设计模式

Reactor

设计模式

在软件工程中,设计模式(design pattern)是对软件设计中普遍存在(反复出现)的各种问题,所提出的解决方案。

--Wikipedia

https://en.wikipedia.org/wiki/Software_design_pattern

设计模式分类

  • 创建型模式:创建对象的同时隐藏创建逻辑的方式
  • 结构型模式:关注类和对象的组合
    • 适配器模式(Adapter)、装饰器模式(Decorator)等
  • 行为型模式:关注对象之间的通信
    • 命令模式(Command)、策略模式(Strategy)、响应者模式(Reactor)等

工厂(方法)模式

width:700px

https://refactoringguru.cn/design-patterns/factory-method

Reactor pattern

The reactor design pattern is an event handling pattern for handling service requests delivered concurrently to a service handler by one or more inputs. The service handler then demultiplexes the incoming requests and dispatches them synchronously to the associated request handlers.

https://en.wikipedia.org/wiki/Reactor_pattern

源自

An Object Behavioral Pattern for
Demultiplexing and Dispatching Handles for Synchronous Events

by Douglas C. Schmidt

http://www.dre.vanderbilt.edu/~schmidt/PDF/reactor-siemens.pdf

实例

A reference implementation of the Reactor Pattern with Java NIO

A typical server application, such as a web server, needs to process thousands of request concurrently. Therefore, the modern web server needs to meet the following requirements

  • Handling of thousands of connections simultaneously (significant number of connections may be in idle state as well)
  • Handling high latency connections

https://github.com/kasun04/nio-reactor

Reactor Pattern

width:700px

Reactor pattern provides a way in which we can listen to the events (incoming connections/requests) with a synchronous demultiplexing strategy so that when an incoming event occurs, it is dispatched to a service provider (handler) that can handle this event.

如何模拟运行

height:150px
height:120px

扩展阅读

Scalable IO in Java

https://gee.cs.oswego.edu/dl/cpjslides/nio.pdf

Reactor 基础款

width:1100px

Reactor 时尚款

width:800px

Reactor 豪华款

width:800px