Project of example of strategy pattern implement by Java.
Define family of behaviors, encapsulate each of them, make the interchangeable. let the behviors can be changed from clients that use it.
-
Strategy
- interface that define the strategy method
-
Concrete Strategy
- class that implements strategy method
-
Contxt
- class that use the Strategy method for action
- define a class will have one behavior that is similar to other behaviors in a list
- use the defined behaviors dynamically
- avoid duplicate code
- encapuslate the behavior
Assume the context is the Pokemon trainer in Pokemon GO.
And there are 3 items can thrown by the trainer:
- regular Pokemon ball
- great ball
- ultra ball
And there have 2 type of Trainer:
- Beginner
- Expert
And the Beginner is desired to use different types of Poke balls when they meet different type of Pokemon:
- use regular Pokemon ball if it is normal Pokemon
- use great ball if it is special Pokemon
- use ultra ball if it is rare Pokemon
And the Expert is desired to use different types of Poke balls when they meet different type of Pokemon:
- use great ball if it is normal Pokemon
- use great ball if it is special Pokemon
- use ultra ball if it is rare Pokemon
- Pokemon Trainer is Context
- ThrowStrategy is encapuslate action and interchangeable
ThrowStrategy interface
public interface ThrowStrategy {
public String throwItem();
}ThrowRegualr, ThrowGreat, ThrowUltra are concrete strategy
ThrowRegular
public class ThrowRegualr implements ThrowStrategy {
public String throwItem() {
return "throw regualr!";
}
}ThrowGreat
public class ThrowGreat implements ThrowStrategy {
public String throwItem() {
return "throw great!";
}
}ThrowUltra
public class ThrowUltra implements ThrowStrategy {
public String throwItem() {
return "throw ultra!";
}
}testing code
beginner = new Beginner();
expert = new Expert();
String beginnerStr, expertStr;
// normal pokemon
System.out.println("see normal Pokemon");
beginnerStr = beginner.throwItem();
expertStr = expert.throwItem();
System.out.println("beginner " + beginnerStr);
System.out.println("expert " + expertStr);
// special pokemon
System.out.println("see special Pokemon");
beginner.setThrowStrategy(new ThrowGreat());
expert.setThrowStrategy(new ThrowUltra());
beginnerStr = beginner.throwItem();
expertStr = expert.throwItem();
System.out.println("beginner " + beginnerStr);
System.out.println("expert " + expertStr);
// rare pokemon
beginner.setThrowStrategy(new ThrowUltra());
expert.setThrowStrategy(new ThrowUltra());
beginnerStr = beginner.throwItem();
expertStr = expert.throwItem();
System.out.println("beginner " + beginnerStr);
System.out.println("expert " + expertStr);output
see normal Pokemon
beginner throw regualr!
expert throw great!
see special Pokemon
beginner throw great!
expert throw ultra!
see rare Pokemon
beginner throw ultra!
expert throw ultra!
avoid coding like this:
public class Beginner extends PokemonTrainer {
// ...
public String throwItem() {
return "throw regular";
}
}public class Expert extends PokemonTrainer {
// ...
public String throwItem() {
return "throw great";
}
}



