亦称: 职责链模式、命令链、CoR、Chain of Command、Chain of Responsibility

Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request.Chain the receiving objects and pass the request along the chain until an object handles it. 为避免请求发送者与接收者耦合在一起,让多个对象都有可能接收请求。将这些接收对象连接成一条链,并且沿着这条链传递请求,直到有对象处理它为止。

它将请求的发送者和接收者解耦了。客户端不需要知道请求处理者的明确信息和处理的具体逻辑,甚至不需要知道链的结构,它只需要将请求进行发送即可。

Untitled

Untitled

责任链模式结构

Untitled

实现

Netty中责任链模式的实现

Untitled

Lambda andThen 实现责任链

Untitled

适用场景

优缺点

<aside> ✅ 可以控制请求处理的顺序

</aside>