Saturday, November 13, 2010

..........Chain of Responsibility

The chain of responsibility pattern is a design pattern that defines a linked list of handlers, each of which is able to process requests. When a request is submitted to the chain, it is passed to the first handler in the list that is able to process it.

The responsibility of handling the request data is given to any of the members of the “chain”. If the first link of the chain cannot handle the responsibility, it passes the request data to the next level in the chain, i.e. to the next link. This is similar to what happens in an Exception Hierarchy. Suppose the code written throws an ArrayIndexOutOfBoundsException. Now, this exception is because of some bug in coding and so, it gets caught at the correct level. Suppose, we have an application specific exception in the catch block. This will not be caught by that. It will find for an Exception class and will be caught by that as both the application specific exceptions and the ArrayIndexOutOfBoundsException are sub-classes of the class Exception.

Once get caught by the exception, which is the base class, it will then not look for any other exception. This is precisely the reason why, we get an “Exception is unreachable” message when we try to add a catch block with the exception below the parent exception catch block.

So, in short, the request rises in hierarchy till some object takes responsibility to handle this request.

public abstract class CNumberHandler
{
protected Handler m_objSuccessor;

public void setSuccessor(Handler successor)
{
m_objSuccessor = successor;
}

public abstract void handleRequest(Request request);
}

public class NegativeHandler : CNumberHandler
{
public void handleRequest(int iNumber)
{
if (iNumber < 0) { //if request is eligible handle it Console.WriteLine("Negative values are handled by ConcreteHandlerOne:"); } // if not else m_objSuccessor.handleRequest(request); } } public class ZeroHandler : CNumberHandler { public void handleRequest(int iNumber { if (iNumber == 0) { //if request is eligible handle it Console.WriteLine("Zero values are handled by ConcreteHandlerThree:"); } // if not else m_objSuccessor.handleRequest(request); } } public class PositiveHandler : CNumberHandler { public void handleRequest(int iNumber) { if (iNumber > 0)
{ //if request is eligible handle it
Console.WriteLine("Positive values are handled by ConcreteHandlerTwo:");
}
// if not
else
m_objSuccessor.handleRequest(iNumber);
}
}
public class Main {

public static void main(String[] args)
{

// Setup Chain of Responsibility
CNumberHandler objCNegativeHandler = new NegaitveHandler();
CNumberHandler objCZeroHandler = new ZeroHandler();
CNumberHandler objCPositiveHandler = new PositiveHandler();

objCNegativeHandler .setSuccessor(objCZeroHandler );
h2.setSuccessor(objCPositiveHandler );

// Send requests to the chain
objCNegativeHandler .handleRequest(-1);
objCNegativeHandler .handleRequest(0);
objCNegativeHandler .handleRequest(1);
objCNegativeHandler .handleRequest(2);
objCNegativeHandler .handleRequest(-5);
}
}

No comments:

Followers

Link