STRATEGY PATTERN

The Strategy pattern is useful when there is a set of related algorithms and a client object needs to be able to dynamically pick and choose an algorithm from this set that suits its current need. The Strategy pattern suggests keeping the implementation of each of the algorithms in a separate class. Each such algorithm encapsulated in a separate class is referred to as a strategy. An object that uses a Strategy object is often referred to as a context object.

In other words, Strategy pattern is used when we have multiple algorithms for a specific task and client decides the actual implementation to be used at runtime. Strategy pattern is also known as Policy Pattern. We define multiple algorithms and let client application pass the algorithm to be used as a parameter. One of the best example of this pattern is Collections.sort () method that takes Comparator parameter. Based on the different implementations of Comparator interfaces, the Objects are getting sorted in different ways.

The strategy pattern is one way that composition can be used as an alternative to subclassing. Rather than providing different behaviors via subclasses overriding methods in super classes, the strategy pattern allows different behaviors to be placed in Concrete Strategy classes which share the common Strategy interface. A Context object contains a reference to a Strategy. By changing the Context’s Strategy, different behaviors can be obtained.

Example:

package com.myjavablog.behavioural.strategy;

public interface Strategy {

public void sort(int[] numbers);
}

package com.myjavablog.behavioural.strategy;

public class Context {

private Strategy strategy;

Context(Strategy strategy){
this.strategy = strategy;
}

public void arrange(int[] input) {
strategy.sort(input);
}
}

package com.myjavablog.behavioural.strategy;

public class BubbleSort implements Strategy {
@Override
public void sort(int[] numbers) {
System.out.println("Sorted using bubble sort!!");
}
}

package com.myjavablog.behavioural.strategy;

public class MergeSort implements Strategy {
@Override
public void sort(int[] numbers) {
System.out.println("Sorted using Merge sort!!");
}
}

package com.myjavablog.behavioural.strategy;

public class InsertionSort implements Strategy {
@Override
public void sort(int[] numbers) {
System.out.println("Sorted using Insertion sort!!");
}
}

package com.myjavablog.behavioural.strategy;

public class StrategyPatternTest {

public static void main(String[] args) {

int[] var = {2, 4, 1, 3 , 9, 6};

//We can provide any strategy for sorting
Context ctx = new Context(new BubbleSort());
ctx.arrange(var);

//We can change the strategy without changing Context class
ctx = new Context(new MergeSort());
ctx.arrange(var);
}
}

Output:

Sorted using bubble sort!!
Sorted using Merge sort!!

When to use the Strategy Design Pattern:

Use the Strategy pattern when:

  • Many related classes differ only in their behavior. Strategies provide a way to configure a class with one of many behaviors.
  • You need different variants of an algorithm. For example, you might define algorithms reflecting different space/time trade-offs. Strategies can be used when these variants are implemented as a class hierarchy of algorithms.
  • An algorithm uses data that clients shouldn’t know about. Use the Strategy pattern to avoid exposing complex, algorithm-specific data structures.
  • A class defines many behaviors, and these appear as multiple conditional statements in its operations. Instead of many conditionals, move related conditional branches into their own Strategy class.

Strategy Pattern in JDK:

  • java.util.Comparator#compare ()
  • javax.servlet.http.HttpServlet
  • javax.servlet.Filter#doFilter ()

Leave a Comment

Bitnami