Chain-of-responsibility pattern
Encyclopedia
In Object Oriented Design
, the chain-of-responsibility pattern is a design pattern
consisting 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 handle 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 of the standard chain-of-responsibility model, some handlers may act as dispatcher
s, capable of sending commands out in a variety of directions, forming a tree of responsibility. In some cases, this can occur recursively, with processing objects calling higher-up processing objects with commands that attempt to solve some smaller part of the problem; in this case recursion continues until the command is processed, or the entire tree has been explored. An XML interpreter (parsed, but not yet executed) might be a fitting example.
This pattern promotes the idea of loose coupling
, which is considered a programming best practice
.
Writing to stdout: Entering function y.
Writing to stdout: Step1 completed.
Sending via e-mail: Step1 completed.
Writing to stdout: An error has occurred.
Sending via e-mail: An error has occurred.
Writing to stderr: An error has occurred.
Note that this example should not be seen as a recommendation on how to write logging classes.
Also, note that in a 'pure' implementation of the chain of responsibility pattern, a logger would not pass responsibility further down the chain after handling a message. In this example, a message will be passed down the chain whether it is handled or not.
Below is another example of this pattern in Java.
In this example we have different roles, each having a fix purchase power limit and a successor. Every time a user in a role receives a purchase request, when it's over his limit, he just passes that request to his successor.
The PurchasePower abstract class with the abstract method processRequest.
Four implementations of the abstract class above: Manager, Director, Vice President, President
The PurchaseRequest class with its Getter methods which keeps the request data in this example.
And here a usage example, the successors are set like this: Manager -> Director -> Vice President -> President
Object oriented design
Object-oriented design is the process of planning a system of interacting objects for the purpose of solving a software problem. It is one approach to software design.-Overview:...
, the chain-of-responsibility pattern is a design pattern
Design pattern (computer science)
In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that...
consisting of a source of command objects
Command pattern
In object-oriented programming, the command pattern is a design pattern in which an object is used to represent and encapsulate all the information needed to call a method at a later time...
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 handle 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 of the standard chain-of-responsibility model, some handlers may act as dispatcher
Dynamic dispatch
In computer science, dynamic dispatch is the process of mapping a message to a specific sequence of code at runtime. This is done to support the cases where the appropriate method can't be determined at compile-time...
s, capable of sending commands out in a variety of directions, forming a tree of responsibility. In some cases, this can occur recursively, with processing objects calling higher-up processing objects with commands that attempt to solve some smaller part of the problem; in this case recursion continues until the command is processed, or the entire tree has been explored. An XML interpreter (parsed, but not yet executed) might be a fitting example.
This pattern promotes the idea of loose coupling
Loose coupling
In computing and systems design a loosely coupled system is one where each of its components has, or makes use of, little or no knowledge of the definitions of other separate components. The notion was introduced into organizational studies by Karl Weick...
, which is considered a programming best practice
Best practice
A best practice is a method or technique that has consistently shown results superior to those achieved with other means, and that is used as a benchmark...
.
Example
The following Java code illustrates the pattern with the example of a logging class. Each logging handler decides if any action is to be taken at this log level and then passes the message on to the next logging handler. The output is:Writing to stdout: Entering function y.
Writing to stdout: Step1 completed.
Sending via e-mail: Step1 completed.
Writing to stdout: An error has occurred.
Sending via e-mail: An error has occurred.
Writing to stderr: An error has occurred.
Note that this example should not be seen as a recommendation on how to write logging classes.
Also, note that in a 'pure' implementation of the chain of responsibility pattern, a logger would not pass responsibility further down the chain after handling a message. In this example, a message will be passed down the chain whether it is handled or not.
Below is another example of this pattern in Java.
In this example we have different roles, each having a fix purchase power limit and a successor. Every time a user in a role receives a purchase request, when it's over his limit, he just passes that request to his successor.
The PurchasePower abstract class with the abstract method processRequest.
Four implementations of the abstract class above: Manager, Director, Vice President, President
The PurchaseRequest class with its Getter methods which keeps the request data in this example.
And here a usage example, the successors are set like this: Manager -> Director -> Vice President -> President
External links
- Article "The Chain of Responsibility pattern's pitfalls and improvements" by Michael Xinsheng Huang
- Article "Follow the Chain of Responsibility" by David GearyDavid GearyDavid Geary is an award winning playwright from New Zealand. He also writes for television.Geary studied acting at Toi Whakaari New Zealand Drama School in Wellington. He graduated in 1987. His play Lovelocks Dream Run was published by Victoria University Press in 1993. The play is studied at...
- Article "Pattern Summaries: Chain of Responsibility" by Mark Grand
- Behavioral Patterns - Chain of Responsibility Pattern
- Descriptions from Portland Pattern Repository
- Apache Jakarta Commons Chain
- PerfectJPattern Open Source Project, Provides a context-free and type-safe implementation of the Chain of Responsibility Pattern in Java
- Chain.NET(NChain) - Ready-to-use, generic and lightweight implementation of the Chain of Responsibility pattern for .NET and Mono
- Chain of Responsibility Design Pattern - An Example
- Sourcemaking Tutorial