Chain Of Responsibility Pattern

Chain Of Responsibility Pattern

In chain of responsibility, sender sends a request to a chain of objects. The request can be handled by any object in the chain.
A Chain of Responsibility Pattern says that just "avoid coupling the sender of a request to its receiver by giving multiple objects a chance to handle the request". For example, an ATM uses the Chain of Responsibility design pattern in money giving process.
In other words, we can say that normally each receiver contains reference of another receiver. If one object cannot handle the request then it passes the same to the next receiver and so on.

Example. Servlet pipeline and Commerce pipeline of Oracle Commerce framework. In servlet pipeline request passes through multiple classes to make HTTP request to Dynamo Request. In Commerce pipeline, Order passes through many processors to process price, commerce item, inventory check, payment, shipping etc before placing order.

Advantage of Chain of Responsibility Pattern

  • It adds flexibility while assigning the responsibilities to objects.
  • It allows a set of classes to act as one; events produced in one class can be sent to other handler classes with the help of composition.
  • To reduce the coupling degree. Decoupling it will request the sender and receiver.
  • Simplified object. The object does not need to know the chain structure.
  • Enhance flexibility of object assigned duties. By changing the members within the chain or change their order, allow dynamic adding or deleting responsibility.
  • Increase the request processing new class of very convenient.

Usage of Chain of Responsibility Pattern:

It is used:

  • When more than one object can handle a request and the handler is unknown.
  • When the group of objects that can handle the request must be specified in dynamic way.
  • When you want to decouple a request’s sender and receiver
  • Multiple objects, determined at runtime, are candidates to handle a request
  • When you don’t want to specify handlers explicitly in your code
  • When you want to issue a request to one of several objects without specifying the receiver explicitly.

interface Chain 
public abstract void setNext(Chain nextInChain); 
public abstract void process(Number request); 

class Number 
private int number; 

public Number(int number) 
this.number = number; 

public int getNumber() 
return number; 


class NegativeProcessor implements Chain 
private Chain nextInChain; 

public void setNext(Chain c) 
nextInChain = c; 

public void process(Number request) 
if (request.getNumber() < 0) 
System.out.println("NegativeProcessor : " + request.getNumber()); 
else
nextInChain.process(request); 

class ZeroProcessor implements Chain 
private Chain nextInChain; 

public void setNext(Chain c) 
nextInChain = c; 

public void process(Number request) 
if (request.getNumber() == 0) 
System.out.println("ZeroProcessor : " + request.getNumber()); 
else 
nextInChain.process(request); 

class PositiveProcessor implements Chain 

private Chain nextInChain; 

public void setNext(Chain c) 
nextInChain = c; 

public void process(Number request) 
if (request.getNumber() > 0) 
System.out.println("PositiveProcessor : " + request.getNumber()); 
else
nextInChain.process(request); 

class TestChain 
public static void main(String[] args) { 
//configure Chain of Responsibility 
Chain c1 = new NegativeProcessor(); 
Chain c2 = new ZeroProcessor(); 
Chain c3 = new PositiveProcessor(); 
c1.setNext(c2); 
c2.setNext(c3); 

//calling chain of responsibility 
c1.process(new Number(90)); 
c1.process(new Number(-50)); 
c1.process(new Number(0)); 
c1.process(new Number(91)); 

Comments

Popular posts from this blog

gsutil Vs Storage Transfer Service Vs Transfer Appliance

SQL basic interview question