Wednesday, May 23, 2012

Going Technical ... Strategy Pattern

After sometime , here I am back again on my blog. Right now I am travelling in a train from one end to another end in Singapore. While I am travelling , I just now saw someone who was pulling all the credit cards that he got and inspected one by one for some reason. As soon as I saw this , I got a marvelous idea to blog something. The pattern is Strategy and I see a potential example in this instance to explain you.

He got three credit cards it seems , Visa , MasterCard and one more , I guess that was an American Express card. Surely he must be using all these three at different occasion. Lets say this person is going to a shop and buys some stuff. Of course , he got to pay for all the stuff he buys, now that lets say he is going to use any of those cards randomly at the counter where he is going to pay. Now the cashier probably uses the card given to him and swipes that card against a card reader at the counter. Most of the time we see that the cashier has got one card reader device , but it is used for many types of cards.

This means , the person who pays have got few strategies to pay , use Visa , MasterCard or American Express. But the cashier would only use one card reader device irrespective of the type of the credit card being used. This scenario is a fairly good example of how we could use Strategy pattern.

I hope you got the idea , simply and clearly. Let's look at some code then....

public interface CreditCard{
  public abstract void pay();
}


public class VisaCard implements CreditCard{
  public void pay(){
    //paying the amount using VisaCard
  }
}


public class MasterCard implements CreditCard{
  public void pay(){
    //paying the amount using MasterCard
  }
}


public class AmericanExpressCard implements CreditCard{
  public void pay(){
    //paying the amount using American Express Card
  }
}


now that lets see how the card reader is,

pubic class CreditCardReader{
  public void readCard(CreditCard creditCard){
    creditCard.pay();
  }
}


Lets put all these into a main method...

public static void main(String args[]){
  CreditCardReader cardReader = new CreditCardReader();

  //first strategy pay by Visa
  CreditCard creditCard = new VisaCard();
  cardReader.readCard(creditCard);

  //second strategy pay by MasterCard
  creditCard = new MasterCard();
  cardReader.readCard(creditCard);

  //Third strategy pay by American Express
  creditCard = new AmericanExpressCard();
  cardReader.readCard(creditCard);

}


That's it. Depending on the situation you could use any card , but the card reader stays the same. This approach is ideal for a situation where you have different algorithms for doing the same work. So depending on the scenario you could use any algorithms.

Note: the JAVA code above is not tested , since I just wrote this while travelling

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