1
edit
Changes
no edit summary
----
==Applicability==
Here are a few situations when using the Chain of Responsibility is more effective:
* More than one object can handle a request
* The handler is not known in advance
* The handler should be determined automatically
* It’s wished that the request is addressed to a group of objects without explicitly specifying its receiver
* The group of objects that may handle the request must be specified in a dynamic way
----
== Drawbacks ==
* '''Unhandled requests'''
**Unfortunately, the Chain doesn't guarantee that every command is handled, which makes the problem worse, since unhandled commands propagate through the full length of the chain, slowing down the application. One way to solve this is by checking if, at the end of the chain, the request has been handled at least once, otherwise we will have to implement handlers for all the possible requests that may appear.
*'''Broken Chain'''
**Sometimes we could forget to include in the implementation of the handleRequest method the call to the successor, causing a break in the chain. The request isn’t sent forward from the broken link and so it ends up unhandled.
----
== UML Diagram ==
**If it can handle the request it does so, otherwise it sends the request to its successor
* '''Client''' - sends commands to the first object in the chain that may handle the command
----
== Code Examples ==
----
=== Java Example ===
'''Example 1''' -- This code can be found at [http://www.javacamp.org/designPattern/chains.html www.Javacamp.org]
<pre>
import java.io.*;
abstract class PurchasePower {
protected final double base = 500;
protected PurchasePower successor;
public void setSuccessor(PurchasePower successor){
this.successor = successor;
}
abstract public void processRequest(PurchaseRequest request);
}
class Manager extends PurchasePower {
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);
}
}
class Director extends PurchasePower {
private final double ALLOWABLE = 20 * base;
public void processRequest(PurchaseRequest request ) {
if( request.getAmount() < ALLOWABLE )
System.out.println("Director will approve $"+ request.getAmount());
else
if( successor != null)
successor.processRequest(request);
}
}
class VicePresident extends PurchasePower {
private final double ALLOWABLE = 40 * base;
public void processRequest(PurchaseRequest request) {
if( request.getAmount() < ALLOWABLE )
System.out.println("Vice President will approve $" + request.getAmount());
else
if( successor != null )
successor.processRequest(request);
}
}
class President extends PurchasePower {
private final double ALLOWABLE = 60 * base;
public void processRequest(PurchaseRequest request){
if( request.getAmount() < ALLOWABLE )
System.out.println("President will approve $" + request.getAmount());
else
System.out.println( "Your request for $" + request.getAmount() + " needs a board meeting!");
}
}
class PurchaseRequest {
private int number;
private double amount;
private String purpose;
public PurchaseRequest(int number, double amount, String purpose){
this.number = number;
this.amount = amount;
this.purpose = purpose;
}
public double getAmount() {
return amount;
}
public void setAmount(double amt){
amount = amt;
}
public String getPurpose() {
return purpose;
}
public void setPurpose(String reason) {
purpose = reason;
}
public int getNumber(){
return number;
}
public void setNumber(int num) {
number = num;
}
}
class CheckAuthority {
public static void main(String[] args) throws Exception{
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);
//enter ctrl+c to kill.
while (true) {
System.out.println("Enter the amount to check who should approve your expenditure.");
System.out.print(">");
double d = Double.parseDouble(new BufferedReader(new InputStreamReader(System.in)).readLine());
manager.processRequest(new PurchaseRequest(0, d, "General"));
}
}
}
</pre>
[[Image:javacodeSolution.GIF]]
----
* More than one object can handle a requestFather: Hey Mother! Pick up the phone!
* The handler is not known in advanceMother: Hey Son! Pick up the phone!
* The handler should be determined automaticallySon: Hey Daughter! Pick up the phone!
* It’s wished that the request is addressed to a group of objects without explicitly specifying its receiverDaughter: Hello?
* The group of objects that may handle the request must be specified in a dynamic wayHello. This is ACME Movie Rentals with an important message.
----
----
== References ==
# [http://www.javaworld.com/javaworld/jw-08-2003/jw-0829-designpatterns.html Java World]
# [http://www.developer.com/java/other/article.php/631261 Developer.com]
# [http://www.oodesign.com/oo_design_patterns/behavioral_patterns/chain_of_responsibility.html OODesign.com]
# [http://codebetter.com/blogs/jeremy.miller/archive/2005/11/06/134359.aspx Codebetter.com]
# [http://www.c-sharpcorner.com/UploadFile/rmcochran/chain_of_command01172007143425PM/chain_of_command.aspx C-Sharpcorner.com]
# [http://www.javacamp.org/designPattern/chains.html Javacamp.org]
----
--[[User:Djeyarat|Djeyarat]] 1521:4956, 2 April 2007 (EDT)