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
----
----
----
'''Example 2''' - This code can be found at [http://www.dofactory.com/Patterns/PatternChain.aspx#_self1 www.dofactory.com]<pre>using System; namespace DoFactory.GangOfFour.Chain.RealWorld { // MainApp test application class MainApp { static void Main() { // Setup Chain of Responsibility Director Larry = new Director(); VicePresident Sam =new VicePresident(); President Tammy =new President(); Larry.SetSuccessor(Sam); Sam.SetSuccessor(Tammy); // Generate and process purchase requests Purchase p =new Purchase(2034, 350.00, "Supplies"); Larry.ProcessRequest(p); p = Cnew Purchase(2035, 32590.10, "Project X"); Larry.ProcessRequest(p); p = new Purchase(2036, 122100.00, "Project Y"); Larry.ProcessRequest(p); // Wait for user Console.Read(); } } // "Handler" abstract class Approver { protected Approver successor; public void SetSuccessor(Approver successor) { this.successor = successor; } public abstract void ProcessRequest(Purchase purchase); } // "ConcreteHandler" class Director : Approver { public override void ProcessRequest(Purchase purchase) { if (purchase.Amount < 10000.0) { Console.WriteLine("{0} approved request# Example {1}", this.GetType().Name, purchase.Number); } else if (successor != null) { successor.ProcessRequest(purchase); } } } // "ConcreteHandler" class VicePresident : Approver { public override void ProcessRequest(Purchase purchase) { if (purchase.Amount < 25000.0) { Console.WriteLine("{0} approved request# {1}", this.GetType().Name, purchase.Number); } else if (successor != null) { successor.ProcessRequest(purchase); } } } // "ConcreteHandler" class President : Approver { public override void ProcessRequest(Purchase purchase) { if (purchase.Amount < 100000.0) { Console.WriteLine("{0} approved request# {1}", this.GetType().Name, purchase.Number); } else { Console.WriteLine( "Request# {0} requires an executive meeting!", purchase.Number); } } } // Request details class Purchase { private int number; private double amount; private string purpose; // Constructor public Purchase(int number, double amount, string purpose) { this.number = number; this.amount = amount; this.purpose =purpose; } // Properties public double Amount { get{ return amount; } set{ amount =value; } } public string Purpose { get{ return purpose; } set{ purpose =value; } }
public int Number
{
get{ return number; }
set{ number = value; }
}
}
}
</pre>
----
== External Links References ==*External Links# [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)