Chain of Responsibility Pattern: Implementation and Applications in Open Source Frameworks
The article explains the Chain of Responsibility design pattern, compares its two implementation styles—chain‑driven and processor‑self‑driven—and demonstrates how open‑source frameworks such as Spring Interceptor, Servlet Filter, Dubbo, and Sentinel employ the pattern to achieve low coupling, high extensibility, and flexible request processing.
The Chain of Responsibility pattern is a widely used design pattern that decouples the sender and receiver of a request, allowing multiple receiver objects to handle the request in sequence. This article introduces common implementation methods of the Chain of Responsibility pattern and extends the discussion by combining it with open source frameworks such as Dubbo, Sentinel, etc.
The Chain of Responsibility pattern effectively reduces coupling between sender and receiver, enhances system extensibility, and allows for customization, ordering adjustment, and addition/removal of processors in the chain.
The Chain of Responsibility pattern has multiple implementation methods, which can be classified into two categories based on the driving method of processors in the chain: Chain-Driven and Processor Self-Driven.
In the Processor Self-Driven approach, each processor object maintains the next processor object, and the entire chain is driven by each processor object itself. The processors are linked in a linked list form.
In the Chain-Driven approach, the chain object itself maintains the processor objects in an array form. The execution of processors is driven by the chain object iterating through and calling the processor objects.
The Chain of Responsibility pattern is widely adopted in many open source frameworks due to its low coupling and high extensibility. This article selects Spring Interceptor, Servlet Filter, Dubbo, and Sentinel from open source frameworks to introduce the implementation of the Chain of Responsibility pattern.
Spring Interceptor is used to intercept the execution of controller methods, allowing custom logic to be added before and after method execution. The responsibility chain is implemented by the HandlerExecutionChain class, which builds and executes the chain.
Servlet Filter is a typical Chain of Responsibility pattern that checks and modifies request and response objects in the Servlet container. The ApplicationFilterChain class is responsible for building and executing the chain.
Dubbo's Filter is used to intercept the call process for service providers and consumers. The ProtocolFilterWrapper class builds the chain by encapsulating each Filter into an Invoker object and linking them together.
Sentinel's Slot is used for traffic governance in distributed service architectures. The DefaultProcessorSlotChain class builds the chain by linking each Slot using a linked list form.
In practice, the Chain of Responsibility pattern can be applied in various business scenarios, such as content filtering in UGC applications or order processing in e-commerce services.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
vivo Internet Technology
Sharing practical vivo Internet technology insights and salon events, plus the latest industry news and hot conferences.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
