Chain Of Responsibilities Pattern is a Behavioral Pattern mainly used to decouple the requests. The request from the client is passed through a set of objects that decide whether to send the request to the next object or not.
Each object contains the reference of another object. If a object cannot process the request, then it is transferred to the next object and forms a chain structure. An object oriented link is formed in the pattern.
Advantages
Loose coupling can be done easily.
Chain of responsibilities pattern can be used as command.
The handler object is not revealed.
The request is handled automatically.
Each object in the chain will have its own implementation.
Conceptual
figure
Handler: Handler is used to define an interface for handling the requests.
ConcreteHandle: Concretehandle is used for requesting its responsibilities.
Client: Client sends the command to the first object in the chain, which will handle the command.
Examples
Following is an example for Chain Of Responsibility Pattern, Create an interface Chain
[java] public interface Chain
{
public abstract void setNext(Chain nextInChain);
public abstract void process(Number request);
} [/java]
Creating a class Value which will request the object.
[java]
public class Value
{
private int value;
public Number(int value)//reads the value
{
this.value = value;
}
public int getValue()//returns the value
{
return value;
}
}
[/java]
Chain Of Responsibility Pattern - In chain series, this class is a link. Creating a class NegativeValue to implement the interface Chain
[java]
public class NegativeValue implements Chain
{
private Chain nextInChain;
public void setNext(Chain c) // overrides the interface method
{
nextInChain = c;
}
public void process(Value request)//overrides the interface method
{
if (request.getValues() < 0)
{
System.out.println("NegativeValue : " + request.getValue());
}
else
{
nextInChain.process(request);
}
}
}[/java]
Chain Of Responsibility Pattern - Creating a class ZeroValue to implement the interface Chain and this class acts as another link in the chain.
[java]public class ZeroValue implements Chain
{
private Chain nextInChain;
public void setNext(Chain c)//overrides the interface method
{
nextInChain = c;
}
public void process(Value request)//overrides the interface method
{
if (request.getValue() == 0) {
System.out.println("ZeroValue : " + request.getValue());
}
else
{
nextInChain.process(request);
}
}
}
[/java]
Creating a class PositiveProcessor to implement the interface Chain and this class acts as another link to the chain.
[java]
public class PositiveProcessor implements Chain
{
private Chain nextInChain;
public void setNext(Chain c)//overrides the interface method
{
nextInChain = c;
}
public void process(Number request)//overrides the interface method
{
if (request.getNumber() > 0)
{
System.out.println("PositiveProcessor : " + request.getNumber());
}
else {
nextInChain.process(request);
}
}
}[/java]
Chain Of Responsibility Pattern - Creating a main class Chain and configuration of chain is done.
[java]
public class ChainTest
{
public static void main(String[] args)
{
//configure Chain of Responsibility
Chain c1 = new NegativeValue();
Chain c2 = new ZeroValue();
Chain c3 = new PositiveValue();
c1.setNext(c2);
c2.setNext(c3);
//calling chain of responsibility
c1.process(new Value(90));
c1.process(new Value(-40));
c1.process(new Value(0));
c1.process(new Value(1000));
}
}[/java]
Output
The result will be as follows.
[java]PositiveValue : 90
NegativeValue : -40
ZeroValue : 0
PositiveValue : 1000
[/java]
Summary
Key Points
Decoupling of requests can be done easily.
Using Chain of responsibilities, request can be passed between the objects easily.
User will not know which process is processing the request.