Friday, 17 January 2014

How to ......

Strategy Pattern:
"Defines a family of algorithms, encapsulates each one,and makes them interchangeable.Strategy lets the algorithm vary independently from clients that use it."
OO principles used:
1) Encapsulate what varies.
2)Favor composition over inheritence.
3)Program to interfaces, not implementations.
- Head first Design patterns.
When you  hear the word "strategy" , always think of money or money making strategies. After you reading this blog I can assure you that you will never , never forget what is strategy pattern in OO designing.

 The above picture is from http://www.oodesign.com/strategy-pattern.html
Now let us consider a  topic that you all love. Let us consider cricket Players.Let " CricketPlayer" is an abstract class.  Bowler,Batsman,WicketKeeper ... are all concreate implementation of "CricketPlayer" Class. BowlingStrategy, BattingStrategy.... are interfaces.Now  GoodBattingStrategy, BadBattingStrategy,ModerateBattingStrategy are concreate implementations of BattingStrategies. Where as BadBowlingStrategy, GoodBowlingStrategy,ModerateBowlingStrategy are concreate implementations of BowlingStrategies.Oh ya, now what?  ah ha  "Now we can do some match fixing". (according to the money).We can fix the match even  on the spot.
public abstrct class CricketPlayer {
BattingStrategy battingstrategy;
BowlingStrategy bowlingstrategy;
public abstract void description();
public void setBowlingStrategy(BowlingStrategy bw){
     bowlingstrategy = bw;
}
public void setBattingStrategy(BattingStrategy bs){
      battingstrategy = bs;
}
public void doBatting(){
       battingstrategy.doBattingWithStrategy();
}
public void doBowling(){
        bowlingStrategy.doBowlingWithStrategy();
}
public void doFielding(){
System.out.println("everybody can do fielding");
}
// add some more methods if you want to.
  }
Now concreate implemetation of "CricketPlayer"
public class Batsman extends CricketPlayer {
public Batsman(){
      battingStrategy = new GoodBattingStategy();
      bowlingStategy = new BadBowlingStrategy();
}
puplic void description(){
System.out.println("normally I do good batting , but I can't do bowling");
}
// you can set wicket keeping strategy too
}
Now concreate implemetation of "CricketPlayer"
public class Bowler extends CricketPlayer {
public Bowler(){
      battingStrategy = new BadBattingStategy();
      bowlingStategy = new goodBowlingStrategy();
}
puplic void description(){
System.out.println("normally I do good bowling , but I can't do batting");
}
// you can set wicket keeping strategy too
}

these are our interfaces:
public interface BattingStrategy {
public void doBattingWithStrategy();

public interface BowlingStrategy {
public void doBowlingWithStrategy();

Concreate implemementations of strategies.
         public class GoodBattingStratergy implements BattingStratergy{
                          public GoodBattingStratergy(){
                             // set good parameters
                                 //set good minimum  money too
                              }
             public void doBattingWithStrategy(){
                            // use a good algorithem for batting
                        // then  do batting
                 }
}
similar way you can implement other classes too. Differnt algorithmes like lefthand ,righthand , offspin, spinners .... as long as you implement the strategies.
let us see two simulators ie match:
public class GenuineMatch {
   public static void main(String[] args) {
    CricketPlayer  rajeshkootrapalli = new Batsman();
     CricketPlayer sheldoncooper = new Bowler();
     rajeshkootrapalli.doBatting();
      rajeshkootrapalli.doBowling();
       sheldoncooper.doBatting();
        sheldoncooper.doBowling();
}
}
public class  FixedMatch {
   public static void main(String[] args) {
    CricketPlayer  rajeshkootrapalli = new Batsman();
     CricketPlayer sheldoncooper = new Bowler();
   rajeshkootrapalli.setBattingStrategy(new BadBattingStrategy());
     rajeshkootrapalli.doBatting();
      rajeshkootrapalli.doBowling();
      sheldoncooper.setBowlingStrategy(new BadBowlingStrategy());
       sheldoncooper.doBatting();
        sheldoncooper.doBowling();
}
}

This way if you encapsulate algorithems in different classes and we can fix the match on the spot without anybodies' attention or concern.  This is how online gaming works.  All winning strategies or loosing strategies are encapsulated in different classes. You can purchase them according to the money. Good algorithems always costs lots of money.

hence the Title:

  How to make money from Strategy patterns.      

No comments:

Post a Comment