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);
}
}
Saturday, November 13, 2010
Subscribe to:
Post Comments (Atom)
Followers
Link
Labels
Agille
(3)
Angular 2
(96)
Architecture
(7)
ASP .Net
(30)
C#/VB .Net
(161)
Entityframework
(11)
General
(33)
Git
(2)
Hosting
(2)
HTML
(4)
Interview
(12)
Interview-Agile
(1)
Interview-Angular
(25)
Interview-ASP .Net
(3)
Interview-C#
(16)
Interview-JavaScript
(1)
Interview-MVC
(30)
Interview-SQL Server
(4)
Interview-WCF
(2)
Islam
(6)
Java Script
(37)
JQuery
(1)
MVC
(53)
NodeJS
(1)
Puzzles
(1)
React-Native
(3)
Security
(1)
Shayri
(10)
SQL Server
(41)
VC++
(5)
WCF
(21)
Web API
(27)
WPF
(21)
No comments:
Post a Comment