Java Design Pattern: Strategy

Strategy pattern is also called policy pattern.

Here is a story about Strategy pattern. Suppose Mike sometimes speeds when driving, but he doesn't always do that. He may be stopped by a police officer. It's possible that the police is very nice, who would let him go without any ticket or with a simple warning. (Let call this kind of police "NicePolice".) Also it's possible that he may be caught by a hard police and gets a ticket. (Let's call this kind of police "HardPolice".) He doesn't know what kind of police would stop him, until he actually gets caught, that is, run-time. This is the whole point of Strategy pattern.

Strategy Pattern Class Diagram

strategy-pattern-class-diagram

Strategy Pattern Java Code

Define a interface Strategy, which has one method processSpeeding()

public interface Strategy {
	//defind a method for police to process speeding case.
	public void processSpeeding(int speed);
}

Now we have two kinds of police officers.

public class NicePolice implements Strategy{
	@Override
	public void processSpeeding(int speed) {
		System.out.println("This is your first time, be sure don't do it again!");		
	}
}
public class HardPolice implements Strategy{
	@Override
	public void processSpeeding(int speed) {
		System.out.println("Your speed is "+ speed+ ", and should get a ticket!");
	}
}

Define a situation in which a police officer will be involved to process speeding.

public class Situation {
	private Strategy strategy;
 
	public Situation(Strategy strategy){
		this.strategy = strategy;
	}
 
	public void handleByPolice(int speed){
		this.strategy.processSpeeding(speed);
	}
}

Finally, try the result.

public class Main {
	public static void main(String args[]){
		HardPolice hp = new HardPolice();
		NicePolice ep = new NicePolice();
 
		// In situation 1, a hard officer is met
                // In situation 2, a nice officer is met
		Situation s1 = new Situation(hp);
		Situation s2 = new Situation(ep);
 
		//the result based on the kind of police officer.
		s1.handleByPolice(10);
		s2.handleByPolice(10);        
	}
}

Output is:

Your speed is 10, and should get a ticket!
This is your first time, be sure don't do it again!

You can compare this pattern with State pattern which is very similar. The major difference is that State pattern involves changing the behavior of an object when the state of the object changes while Strategy pattern is mainly about using different algorithm at different situation.

Strategy Pattern in JDK

1). Java.util.Collections#sort(List list, Comparator < ? super T > c)
2). java.util.Arrays#sort(T[], Comparator < ? super T > c)

The sort method use different Comparator in different situations. To know more about this method, check out Deep Understanding of Arrays.sort().

You may also want to check out The Difference between Comparator and Comparable.

Category >> Design Patterns Stories  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>
  • Nguyên Tánh

    perfect explanation!

  • Weidang Wang

    from ifeve.com good work

  • roya

    very good thanks

  • Jay

    Thank you tons for providing an example which is easy to understand/visualize.Very helpful for my learning.