Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

strategy-pattern

Project of example of strategy pattern implement by Java.

What is strategy pattern

Define family of behaviors, encapsulate each of them, make the interchangeable. let the behviors can be changed from clients that use it.

strategy-pattern

  1. Strategy

    • interface that define the strategy method
  2. Concrete Strategy

    • class that implements strategy method
  3. Contxt

    • class that use the Strategy method for action

When to use strategy pattern

  • 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

Exapmle

Assume the context is the Pokemon trainer in Pokemon GO.

And there are 3 items can thrown by the trainer:

  1. regular Pokemon ball

regualr

  1. great ball

great

  1. ultra ball

ultra

And there have 2 type of Trainer:

  1. Beginner
  2. Expert

And the Beginner is desired to use different types of Poke balls when they meet different type of Pokemon:

  1. use regular Pokemon ball if it is normal Pokemon
  2. use great ball if it is special Pokemon
  3. 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:

  1. use great ball if it is normal Pokemon
  2. use great ball if it is special Pokemon
  3. use ultra ball if it is rare Pokemon

class diagram: strategy-pattern

  • 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";
    }
}