37 lines
1.5 KiB
Markdown
37 lines
1.5 KiB
Markdown
---
|
|
layout: pattern
|
|
title: Chain of responsibility
|
|
folder: chain
|
|
permalink: /patterns/chain/
|
|
pumlid: 9SR13SCm20NGLTe1OkxTXX0KKzd4Wa-pVYlrdTxJN4OTMZ4U7LZv8Wg-ssdejLTgoELGHvDhaesw6HpqvWzlXwQTlYq6D3nfSlv2qjcS5F9VgvXjrHnV
|
|
categories: Behavioral
|
|
tags:
|
|
- Java
|
|
- Gang Of Four
|
|
- Difficulty-Intermediate
|
|
---
|
|
|
|
## 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.
|
|
|
|

|
|
|
|
## Applicability
|
|
Use Chain of Responsibility when
|
|
|
|
* more than one object may handle a request, and the handler isn't known a priori. The handler should be ascertained automatically
|
|
* you want to issue a request to one of several objects without specifying the receiver explicitly
|
|
* the set of objects that can handle a request should be specified dynamically
|
|
|
|
## Real world examples
|
|
|
|
* [java.util.logging.Logger#log()](http://docs.oracle.com/javase/8/docs/api/java/util/logging/Logger.html#log%28java.util.logging.Level,%20java.lang.String%29)
|
|
* [Apache Commons Chain](https://commons.apache.org/proper/commons-chain/index.html)
|
|
* [javax.servlet.Filter#doFilter()](http://docs.oracle.com/javaee/7/api/javax/servlet/Filter.html#doFilter-javax.servlet.ServletRequest-javax.servlet.ServletResponse-javax.servlet.FilterChain-)
|
|
|
|
## Credits
|
|
|
|
* [Design Patterns: Elements of Reusable Object-Oriented Software](http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612)
|