View
220
Download
2
Embed Size (px)
Citation preview
2
IntentMotivationApplicabilityStructureParticipantsImplementationConsequencesExampleRelated patternsAttention
Table of Contents
3
IntentAvoid coupling sender of request to its receiver
by giving more than one object chance to handle request. Chain receiving objects and pass request along until an object handles it.
Chain of Responsibility Pattern
4
Consider a context sensitive help facility for a GUI
The user can obtain help information on any part of the interface just by clicking on it.
The help that is provided depend on the parts of the interface that’s selected and its context.
Sometimes we will encounter with help about a particular button, a particular dialog, or the application in general based on whether help is available on that subject
Motivation
5
the help information is organized according to its generality - from the most specific to the most general
The problem is that the object that ultimately provides the help is not known explicitly to the object that initiates the help request
We need a way to decouple the button that initiates the help request from the objects that provide help information
Motivation(Cont)
6
Chain of Responsibility Pattern
The first object in the chain receives the request and will either handle it or forward it to the next object on the chain and so on.
The important thing to remember, is that the object that made the request, has no knowledge of which object is handling the request. The request has an implicit receiver.
7
HelpHandler class diagram To forward request along the chain, and to ensure receivers
remain implicit, Each object in the chain can share a common interface for handling requests and accessing its successor on the chain.
8
When more than one object may handle a particular request and the handler isn’t known ahead of time
When you want to issue a request to one of several objects without specifying the receiver explicitly
When the set of objects to handle a request should be specified dynamically
Applicability
10
Handler defines an interface for handling the requests (optional) implements the successor link
ConcreteHandlerhandles requests it is responsible for can access its successor if the ConcreteHandler can handle the request, it
does so; otherwise it forwards the request to its successor
Client initiates the request to a ConcreteHandler object
on the chain
Participants
11
Implementing the successor chainDefine new links (in Handler or ConcreteHandler)Use existing links (such as the parent references from the Composite pattern)
Connecting successorsRepresenting requests
Implementation
16
+ Reduced Coupling• The sender doesn’t need to know the specific receiver
and the receiver doesn’t need to know the sender
+ Added flexibility in assigning responsibilities to objects• By changing the chain you can change responsibilities
for handling a request at run time
– Receipt Isn’t Guaranteed• A request may fall off the end of the chain
Consequences
Example Whenever you spend company's money, you need get approval from your boss, or
your boss's boss. Let's say, the leadership chain is: Manager-->Director-->Vice President-->President
abstract class Approver { protected final double base = 500;protected Approver successor;public void setSuccessor(Approver successor){ this.successor = successor;
} abstract public void processRequest(PurchaseRequest request);
}
class Manager extends Approver { private final double ALLOWABLE = 10 * base; public void processRequest(PurchaseRequest request ) { if( request.getAmount() < ALLOWABLE )
System.out.println("Manager will approve $"+ request.getAmount()); else if( successor != null) successor.processRequest(request); }
}
Example (Cont)Manager manager = new Manager();Director director = new Director();VicePresident vp = new VicePresident();President president = new President()manager.setSuccessor(director);director.setSuccessor(vp);vp.setSuccessor(president);
try{ while (true) { System.out.println("Enter the amount to check who should approve yourexpenditure."); System.out.print(">"); double d = Double.parseDouble(new BufferedReader(new
InputStreamReader(System.in)).readLine()); manager.processRequest(new PurchaseRequest(0, d, "General")); } }catch(Exception e){ System.exit(1); }
21
A Decorator usually wraps the decorated object: clients point to the decorator and not the object
A Decorator does not have to forward the same message
A decorated object does not have to know that it is wrapped
With a chain of responsibility, the client asks the first chain objects explicitly.
Differences with Decorator
22
In chain of responsibility: you can break the chain at any point
Decorators can be thought of as executing all at once without any interaction with the other decorators
Links in a chain can be thought of as executing one at a time, because they each depend on the previous link
Chain of resposibility vs Decorator