Chain of Responsibility Pattern

(from Design Patterns)

Synopsis

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.

Context

The particular object which can fulfill a request may vary with time or with the specifics of the request. For example, the window which handles a mouse event, the class which handles a method call, the scope which resolves a variable reference, the clerk which isn't busy, or the format used for reading an image.

Forces

Solution

Give each handler the ability to reject a request. Then order the potential handlers in a chain-like data structure. The first object in the chain receives the request and either handles it or rejects it. If it rejects, give the request to the next object, and repeat until an object handles it or you run out of objects.

Consequences

Implementation

See Design Patterns for sample code.

Known Uses

The Chain of Responsibility is not a good choice for finding the best receiver of a message. For example, C++ resolves overloaded methods by minimizing the number of argument conversions required. The different methods cannot be ordered in a single chain to simulate this behavior.


Thomas Minka
Last modified: Fri Sep 02 16:57:16 GMT 2005