Design Patterns

Chain Of Responsibility



Design Pattern Type: Behavioral

GoF Statement of Intent:


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.
Brief Overview:



Consists of a source of "command objects" and a series of "processing objects." Each processing object contains a set of logic that describes the types of command objects that it can handle, and how to pass off those that it cannot to the next processing object in the chain. A mechanism also exists for adding new processing objects to the end of this chain. In a variation some handlers may also act as "dispatchers" that can send commands out in a variety of directions thus creating a "tree of responsibility". An XML interpreter is one possible example. Chain of Responsibility promotes loose coupling.
UML Diagram:

Discussion and In-class Example:

Suppose you are writing an automated e-mail handler for a business. The business gets 4 types of e-mail (1) Fan mail with complements, (2) Complaints, (3) Requests for new features, and (4) Spam.


The business already has a way of detecting the type of an incoming e-mail. It needs an an application that uses the detectors to handle incoming e-mail. With Chain Of Responsibility you create a chain of objects each of which examines a request, event, etc. and either handles it or passes it on to the next object in the chain. This pattern is used in window systems to handle events like mouse clicks and keyboard events.

Advantages of using a Chain of Responsibility
  1. Decouples the sender of a request from its' receivers
  2. Simpler structure because the request object doesn't have to know anything about the chain's structure or the sequence of handlers.
  3. Allows you to add and remove handlers from the chain easily to add or remove responsibilities.

Possible problems of using a Chain of Responsibility
  1. Request may not be handled at all if it falls off the end of the request.
  2. Can be hard to observe the runtime characteristics and to debug.