Design Patterns - Gamma
Design Patterns - Gamma
Chain of Responsibility
Pattern
l 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.
l Motivation
é Consider a context-sensitive help system for a GUI
é The object that ultimately provides the help isn't known explicitly to the
object (e.g., a button) that initiates the help request
é So use a chain of objects to decouple the senders from the receivers. The
request gets passed along the chain until one of the objects handles it.
é Each object on the chain shares a common interface for handling requests
and for accessing its successor on the chain
1
The Chain of Responsibility Pattern
l Motivation
l Applicability
é Use Chain of Responsibility:
Ý When more than one object may handle a request and the actual handler is not
know in advance
Ý When requests follow a “handle or forward” model - that is, some requests can
be handled where they are generated while others must be forwarded to another
object to be handled
l Consequences
é Reduced coupling between the sender of a request and the receiver - the
sender and receiver have no explicit knowledge of each other
é Receipt is not guaranteed - a request could fall off the end of the chain
without being handled
é The chain of handlers can be modified dynamically
2
The Chain of Responsibility Pattern
l Structure
3
Chain of Responsibility Example 1 (Continued)
l In Java 1.1 the AWT event model was changed from the Chain of
Responsibility (CoR) pattern to the Observer pattern. Why?
é Efficiency: GUIs frequently generate many events, such as
MOUSE_MOVE events. In many cases, the application did not care about
these events. Yet, using the CoR pattern, the GUI framework would
propagate the event up the containment hierarchy until some component
handled it. This caused the GUI to slow noticeably.
é Flexibility: The CoR pattern assumes a common Handler superclass or
interface for all objects which can handle chained requests. In the case of
the Java 1.0 AWT, every object that could handle an event had to be a
subclass of the Component class. Thus, events could not be handled be
non-GUI objects, limiting the flexibility of the program.
4
Chain of Responsibility Example 1 (Continued)
l The Java 1.1 AWT event model uses the Observer pattern. Any
object that wants to handle an event registers as an event listener
with a component. When an event occurs, it is posted to the
component. The component then dispatches the event to any of
its listeners. If the component has no listeners, the event is
discarded. For this application, the Observer pattern is more
efficient and more flexible!
5
Chain of Responsibility Example 2 (Continued)
6
Handling Requests
l Notice in the basic structure for the CoR pattern that the Handler
class just has one method, handleRequest(). Here it is as a Java
interface:
public interface Handler {
public void handleRequest();
}
7
Handling Requests (Continued)
8
Handling Requests (Continued)
9
Handling Requests (Continued)
l Here's a concrete handler which deals with all three request types:
public class ConcreteHandler
implements HelpHandler, PrintHandler, FormatHandler {
10
Handling Requests (Continued)
11