Strategy design pattern is also called as policy design pattern, which comes under behavioral design pattern category. In this design pattern, we select the algorithm to be executed dynamically during run time.

GoF Definition : Define a family of algorithms, encapsulate each one, and make them interchangeable. The strategy pattern lets the algorithm vary independently from client to client.

Strategy pattern example implementation in java

Following example will explain how to implement strategy design pattern in java. Here, we will have a common interface called Strategy.java and different implementations. These implementations are invoked during runtime by Context.java class.

Strategy.java

This is the interface, which contains the required method(s) which is implemented by different strategy implementation classes.

package com.asb.example;

public interface Strategy {
	
	public void strategy();
}

Next step is to create the implementation classes for the strategy interface created above. Let’s create two Strategy implementation classes StrategyOne.java and StrategyTwo.java as shown below. These classes provide required implementation for the methods defined in the Strategy.java interface.

StrategyOne.java

package com.asb.example;

public class StrategyOne implements Strategy {

	@Override
	public void strategy() {
		
		System.out.println("Strategy One is applied!!");
	}
}

StrategyTwo.java

package com.asb.example;

public class StrategyTwo implements Strategy {

	@Override
	public void strategy() {
		
		System.out.println("Strategy Two is applied!!");
	}
}

Our strategies are ready. Now we need to create a Context.java class. This class decides which strategy to be applied dynamically during run time. This context class contains a generic strategy instance. We can use the setStrategy() method to set the strategy dynamically by passing required strategy implementation.

Context.java

package com.asb.example;

public class Context {
	
	private Strategy strategy;
	
	public void setStrategy(Strategy strategy) {
		this.strategy = strategy;
	}
	
	public void applyStrategy() {
		strategy.strategy();
	}
}

Finally, it’s time to check our strategy pattern design class. Create a StrategyDesignPatternExample.java class with main() method as shown below. Here, we are getting the user input from console to select one of the strategy implemented in the above section. If user inputs with value 1, StrategyOne.java implementation is applied and StrategyTwo.java is applied for other input values.

StrategyDesignPatternExample.java

package com.asb.example;

import java.util.Scanner;

public class StrategyDesignPatternExample {

	public static void main(String srgs[]) {
		
		System.out.println("**Strategy Design Pattern Example**");
		System.out.println("Select a startegy by entering 1 or 2..");
		Scanner in= new Scanner(System.in);
		Integer startegy = in.nextInt();
		Context context = new Context();
		
		switch(startegy) {
		
			case 1 : 
				context.setStrategy(new StrategyOne());
				break;
			default : 
				context.setStrategy(new StrategyTwo());
		}
		in.close();
		context.applyStrategy();
	}
}

Output

Strategy Design Pattern
Strategy Design Pattern

A real world example of strategy design pattern used in java util library is java.util.Comparator#compare(). Also, doFilter() method of javax.servlet.Filter

Main advantage of this design pattern is, we can dynamically apply required algorithm during runtime. But a disadvantage of this pattern is context should be aware of all available strategies to select one among them during run time.

You may be interested in