Thursday, November 10, 2011

Going Technical .......Chain Of Responsibility

Well it seems I get to write a bit more in this month :) , without much ado lets jump into the subject straight away. The pattern that we are going to talk about today is Chain Of Responsibility and it is one of those behavioral patterns introduced in GOF patterns.

What can you imagine when you think the name of the pattern itself , mmmmm.... something like a chain that we used to keep a dog not to run away...well kind of. But the difference is that this chain is going to be made of virtual objects in the memory of a computer which you can't see...but lets try to imagine it!

I hope you have seen automated car washing facilities , well if you have seen them this is quite easy to understand. Usually at those places when you drive in and leave the car at the right place , the automated washing facility would first spray water on the car , and then the car is slowly moved to a section where a cleansing form is applied to it , and then the car is slowly moved to a section where it will be washed , and then the car is slowly moved to a section where a dryer would start drying wet surface of the car. And at last the car would be slowly moved out to another section where you could just get in and drive again.

Well in the above analogy , you see that each section has its individual responsibility. Once section spray water on the car , the other section put cleansing form , the other section washes cleansing form and the last section dries the surface of the car. When the car is moved out of the facility , the final shape of the car still remains the same , but it would look more cleaner than it was. In the chain of responsibility pattern you would do the same. Means you would have different classes which executes a certain logic (analogues to washing , forming , drying etc) over a given object (analogues to car). And there would be a way to chain these classes one after the other as well.

OK lets see some code now....

public abstract class CarWashChain{
 private CarWashChain nextChain;

 public void setNextInChain(CarWashChain chain){
  this.nextChain = chain;
 }

 public CarWashChain getNextInChain(){
  return this.nextChain;
 }

 public abstract void doWork(Car car);

}

public class SprayWater extends CarWashChain{
 public void doWork(Car car){
  //spray the car with water first...

  //send to the next in the chain if any
  if(getNextInChain() != null){
   getNextInChain().doWork(car);
  }
 }
}

public class SprayForm extends CarWashChain{
 public void doWork(Car car){
  //spray cleansing form to the car for cleaning

  if(getNextInChain() != null){
   getNextInChain().doWork(car);
  }
 }
}

public class ClearForm extends CarWashChain{
 public void doWork(Car car){
  //put water and wash the car after applying cleansing form

  if(getNextInChain() != null){
   getNextInChain().doWork(car);
  }
 }
}

public class Dry extends CarWashChain{
 public void doWork(Car car){
  //dry the surface of the car now

  if(getNextInChain() != null){
   getNextInChain().doWork(car);
  }

 }
}

Now we have defined the chain elements , what is left is putting them in the chain

//Only main methods is shown here
public static void main(String[] args){

 SprayWater sprayWaterChainElement = new SprayWater();
 SprayForm sprayFormChainElement = new SprayForm();
 ClearForm clearFormChainElement = new ClearForm();
 Dry dryChainElement = new Dry();

 //Now that chain all these in one
 sprayWaterChainElement.setNextInChain(sprayFormChainElement);
 sprayFormChainElement.setNextInChain(sprayFormChainElement);
 sprayFormChainElement.setNextInChain(clearFormChainElement);
 clearFormChainElement.setNextInChain(dryChainElement);

 //invoke the chain , now this will execute all the chain elements with their respective logic
 Car car = new Car();
 sprayWaterChainElement.doWork(car);
}


well that is all for today , hope you have got some idea of this pattern too!!!. Do add a comment if you like this

1 comment:

Nalaka said...

amm good one. Thanks for posting. keep up the good work :)