This is my blog , it could contain various topics from my own thoughts to computer programming ....

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

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