Chain of Responsibility Design Pattern
Behavioral pattern. Behavioral design patterns are concerned with algorithms and the assignment of responsibilities between objects.
The Chain of Responsibility design pattern solves problems like: How can coupling the sender of a request to its receiver be avoided?
Open in the interactive referenceDownload the PDF reference
Problem
The Chain of Responsibility design pattern solves problems
like:
How can coupling the sender of a request
to its receiver be avoided?
How can more than one object handle a request?
See Applicability section for all problems Chain of Responsibility can solve.
See Solution section for how Chain of Responsibility solves the problems.
-
An inflexible way is to implement (hard-wire) a request
(
receiver1.operation1()) directly within the class (Sender) that sends the request. - This commits (couples) the sender of a request to a particular receiver at compile-time and makes it impossible to specify more than one receiver.
- That's the kind of approach to avoid if we want to specify multiple objects that can handle a request.
-
For example,
providing context-sensitive help in a GUI/Web application.
In a context-sensitive help system, a user can click anywhere to get help information. That is, multiple objects exist that can handle a help request by providing a specific help information. Which object provides the help isn't known at compile-time and should be determined at run-time (depending on run-time conditions).
"The problem here is that the object that ultimately provides the help isn't known explicitly to the object [sender] (e.g., the button) that initiates the help request." [GoF, p223]
Solution
The Chain of Responsibility design pattern provides a solution:
Define a chain of Handler
objects
having the responsibility
to either handle a request
or forward it to the next handler.
A class sends a request
to a chain of handlers
and doesn't know (is independent of) which handler
handles the request.
Describing Chain of Responsibility in more
detail is the theme of the following sections.
See Applicability section for all problems Chain of Responsibility can solve.
- "The idea of this pattern is to decouple senders and receivers by giving multiple objects a chance to handle a request. The request gets passed along a chain of objects until one of them handles it." [GoF, p223]
-
Define and chain
Handlerobjects:-
Define an interface for handling a request
(
Handler | handleRequest()). -
Objects that can handle a request
implement the
Handlerinterface by either handling the request directly or forwarding it to the next handler (if any) on the chain. - "Chain of Responsibility is a good way to decouple the sender and the receiver if the chain is already part of the system's structure, and one of several objects may be in a position to handle the request." [GoF, p348]
-
Define an interface for handling a request
(
-
A class (
Sender) sends a request to a chain of handlers (handler.handleRequest()). The request gets passed along the chain until a handler handles it. - This enables loose coupling between the sender of a request and its
receiver(s).
The object that sends a request has no explicit knowledge of the object (receiver) that ultimately will handle the request.
The chain ofHandlerobjects can be specified dynamically at run-time (Handlerobjects can be added to and removed from the chain).
Applicability
Design Problems
-
Avoiding Hard-Wired Requests
- How can coupling the sender of a request to its receiver be avoided?
-
Specifying Multiple Receivers
- How can more than one object handle a request?
- How can the set of objects that can handle a request be specified dynamically?
Structure
Static Class Structure
-
Sender-
Refers to the
Handlerinterface to handle a request (handler.handleRequest())
and is independent of how the request is handled (which handler handles the request). -
Maintains a reference
(
handler) to aHandlerobject on the chain.
-
Refers to the
-
Handler- Defines an interface for handling a request.
-
Maintains a reference (
successor) to the nextHandlerobject on the chain.
-
Receiver1,Receiver2,Receiver3,…-
Implement the
Handlerinterface
by either handling a request directly or forwarding it to the next handler (if any) on the chain.
-
Implement the
Dynamic Object Collaboration
-
In this sample scenario,
a
Senderobject sends a request to aHandlerobject on the chain. The request gets forwarded along the chain until a handler (receiver3) handles it. -
The interaction starts with the
Senderthat callshandleRequest()on thereceiver1object (of typeHandler). -
receiver1forwards the request by callinghandleRequest()on thereceiver2object. -
receiver2forwards the request by callinghandleRequest()on thereceiver3object. -
receiver3handles the request and returns toreceiver2(which returns toreceiver1, which in turn returns to theSender). -
See also Sample Code / Example 1.
Not included on this page: Intent, Motivation, Consequences, Implementation, Related Patterns. See the GoF Design Patterns Reference (PDF) for the complete description.