Wednesday, November 30, 2011

Me...

Unfairly judged
An Individual
Wading through
Difficult times
Looking for
A resting ground
Where there is
Endless Peace...

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

Thursday, November 3, 2011

Going Technical .....Template Pattern

Again back to my blog , I have been busy with some other assignments for the past few months.Hence never got a chance to step back to this yard recently.However today I managed to grab some of my time to write something technical for all of the visitors of this blog (Not sure how many since I do not seem to get many comments :))

Ok what are we going to learn today , well the title says all of it!!, we gonna see what "Template Pattern" is. Template pattern is one of those Gang OF Four patterns and it falls under the category of behavioral patterns. well what ever lets see what it really means....

I hope all of you are familiar with cup cakes. well what is that got to do with this? one might ask.. Let me tell you , to make cup cake what we do first is buying or making a small cup like moulds made out of possibly some sort of a hard material. And then we would prepare necessary cake mixture and pour this mixture into those moulds. After that we would keep these moulds filled with cake mixture into an oven for backing. Not only that you could put some chocolate on top of one cup cake and some icing on top of another cup cake. Like that you could make different cup cakes with different appearance in color , different taste as well. Now that lets see how this is analogues to Template design pattern.

In simple terms I used moulds which are of the same geometrical shape for making cup cakes of different taste and color. But the fundamental shape of the cup cake was not changed. And I could also use the same set of moulds to make more cup cakes of different taste and color as well. So template pattern is also the same , you make a template class which is similar to the moulds and then define the stuff that need not be changed or must not be changed. It is as if the fundamental physical appearance of the cup cake moulds remain the same.Then what did we do ? we put cake mixture , this is as if you extend the original template class and write your own logic , but do not change the stuff that was defined in the template class. It is as if when you bake cup cakes you are sure that the shape of the cup cake will be determined by the shape of the moulds used.

Let's see some code in action,

public abstract class CupCakeMould{

  public void makeCupCake(){
   fillWithTheCakeMixture();
   addColor();
   addChocolate();
   addIcing();
  }

  //These abstract methods are expected to be implemented by the
  //sub class and define required logic
  public abstract void fillWithTheCakeMixture();
  public abstract void addColor();
  public abstract void addChocolate();
  public abstract void addIcing();

}

//extending classes

public class CupCakeWithIcing extends CupCakeMould{
  public abstract void fillWithTheCakeMixture(){
   //logic to fill the mould with cake mixture
  }
  public abstract void addColor(){
   //add logic if you need to add color to this cake
  }
  public abstract void addChocolate(){
   //add logic if you need to add chocolate to this cake
  }
  public abstract void addIcing(){
   //add logic if you need to add icing to this cake
  }
}



public class CupCakeWithChocolate extends CupCakeMould{
  public abstract void fillWithTheCakeMixture(){
   //logic to fill the mould with cake mixture
  }
  public abstract void addColor(){
   //add logic if you need to add color to this cake
  }
  public abstract void addChocolate(){
   //add logic if you need to add chocolate to this cake
  }
  public abstract void addIcing(){
   //add logic if you need to add icing to this cake
  }
}


//usage of this
//only main method is shown for clarity
  public static void main(String args[]){
   CupCakeMould cupCake = new CupCakeWithIcing();
   //This will make a cup cake which will have icing on it. See that you did not
   //change the original algorithm on how the cake is made , only what    the cake is made
   //from. You let the sub classes to decide it
   cupCake.makeCupCake();
   //if you want some other cake
   CupCakeMould cupCake2 = new CupCakeWithChocolate();
   cupCake2.makeCupCake();

}

Going Technical , Prototype pattern

Well this is the year of 2014 and I have not made a single post yet for this year. Frankly I did not have a good real life example for expla...