Chain of responsibility Design pattern Concept with Example

Chain Of Responsibility Pattern creates a chain of objects that examine a request. Each object in turn examines the request and handles it if it can, otherwise passes it on to the next object in the chain. So each object in the chain acts as a handler.

The object that ultimately handles the request is unknown explicitly to the sender object that sends the request. By this way we are using chain of responsibility pattern to decouple the sender of the request and its receivers.

Each object on the chain shares a common interface for handling requests and for accessing its successor on the chain.

Receipt is not guaranteed - a request could fall off the end of the chain without being handled.

The Java Servlet filter framework is an example of chain of resposibility design. Note that the chain.doFilter() is the method that should be called to make the chain roll. If the subclass missed it, the whole chain would be stopped or blocked.

Java exception handling is another example of chain of responsibility design. When an error occurs, the exception call will look for a handling class. If there is no handler, the super Exception class will be called to throw the exception. Otherwise, the handler class will handle it. Processing stops after an event is handled.

Chain of responsibility simplifies object interconnections.Instead of senders and receivers maintaining references to all candidate receivers,each sender keeps a single reference to the head of the chain,and each receiver keeps a single reference to the immediate successor in the chain.

Chain Of Responsibility uses Command Pattern to represent requests as objects
Chain of Responsibility is often applied in conjunction with CompositePattern.There,a component's parent act as its successor.

package cop;

abstract class Car {
 
 public double price = 5.5;
 public String color = "Silver";
 public String size = "Small";
 
 
 // The Next Element in the Chain of Responsibility
 protected Car next; 
 
 protected Car searchNext(Car car) { 
  next=car; 
  return this;
 } 
 
 public void trail(String performance) {
  if(price < 10 && performance.equalsIgnoreCase("Satisfactory")){   
   buyCar(performance);
  }
  else{
   if(next!=null)
    next.trail(performance);
  }
 }  
 
 abstract protected void buyCar(String performance);
}





package cop;

public class Innova extends Car{
 
 public Innova(double price,String color,String size) {
  this.price = price;
  this.color = color;
  this.size = size;
 }
 
 protected void buyCar(String performance) {
  System.out.println("Liked Innova ...  performance... "+performance);
 }
}





package cop;

public class Toyota extends Car{
 
 public Toyota(double price,String color,String size) {
  this.price = price;
  this.color = color;
  this.size = size;
 }
 
 protected void buyCar(String performance) {
  System.out.println(" Liked Toyota ... performance... "+performance);
 }
}





package cop;

public class SearchCars {

 public static void main(String[] args) {
  Car car=new Toyota(10.5,"black","big").searchNext(new Innova(8.5,"silver","medium"));
  car.trail("Satisfactory"); 
 }

}




When To Use:
1. Use Chain Of Responsibility Pattern when you want to give more than one object a chance to handle a request and the actual handler is not known in advance.
2. Commonly used in windows systems to handle events like mouse clicks and keyboard events.
3. When you want to avoid coupling the sender of a request to its receiver.

Advantages:
1. Decouples the sender of the request and its receivers.
2. Simplifies your object as it doesn't have to know about the chain structure and keep direct references to its members.
3. Allows you to add or remove responsibilities dynamically by changing the members or order of the chain.


Disadvantage:
1. Hard to observe the runtime characteristics and debug.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.