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();

}

4 comments:

testerMe said...

happy baking... :) content was useful

Ranil said...

Thank you "testerMe"

Pinky said...

u have explained it well aiya...keep it up.. :)

Ranil said...

Thank you Pinky...

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...