The 23 GoF design patterns and Dependency Injection – choose a pattern Abstract Factory design patternBuilder design patternFactory Method design patternPrototype design patternSingleton design patternAdapter design patternBridge design patternComposite design patternDecorator design patternFacade design patternFlyweight design patternProxy design patternChain of Responsibility design patternCommand design patternInterpreter design patternIterator design patternMediator design patternMemento design patternObserver design patternState design patternStrategy design patternTemplate Method design patternVisitor design patternDependency Injection design pattern

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 Handler objects:
    • Define an interface for handling a request (Handler | handleRequest()).
    • Objects that can handle a request implement the Handler interface by either hand­ling 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]
  • 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 of Handler objects can be specified dynamically at run-time (Handler objects 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 Handler interface 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 a Handler object on the chain.
  • Handler
    • Defines an interface for handling a request.
    • Maintains a reference (successor) to the next Handler object on the chain.
  • Receiver1,Receiver2,Receiver3,…
    • Implement the Handler interface
      by either handling a request directly or forwarding it to the next handler (if any) on the chain.

Dynamic Object Collaboration

  • In this sample scenario, a Sender object sends a request to a Handler object on the chain. The request gets forwarded along the chain until a handler (receiver3) handles it.
  • The interaction starts with the Sender that calls handleRequest() on the receiver1 object (of type Handler).
  • receiver1 forwards the request by calling handleRequest() on the receiver2 object.
  • receiver2 forwards the request by calling handleRequest() on the receiver3 object.
  • receiver3 handles the request and returns to receiver2 (which returns to receiver1, which in turn returns to the Sender).
  • 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.