728x90 AdSpace

Monday, 7 August 2017

Design Patterns - Strategy Pattern

Strategy Pattern


Use strategy when you need to define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it. Related patterns include State, Flyweight, Decorator, Composite.

Where to use

•When you need to use one of several algorithms dynamically.
•When you want to configure a class with one of many related classes (behaviors).
•When an algorithm uses data that clients shouldn't know about.


•Reduces multiple conditional statements in a client.
•Hides complex, algorithmic-specific data from the client.
•Provides an alternative to subclassing.
•Can be used to hide data that an algorithm uses that clients shouldn't know about.


•Clients must be aware of different strategies. A client must understand how strategies differ before it can select the appropriate one.
•Increases the number of objects in an application.

Strategy Pattern Class Diagram

In the class-diagram above:
•The Strategy interface defines the behavior that is common to all the concrete implementations.
•The ConcreteStrategy encapsulates an implementation of a specific algorithm or behavior that is defined through the Strategy interface.
•The Context provides certain services that is defined by the Strategy interface and implemented by different ConcreteStrategy classes depending on behavior.

Strategy Pattern Example

This example uses sorting algorithms. Two sorting algorithms (Bubble sort and Quick sort) are implemented and the client can select either of the algorithms. The SortInterface describes what the algorithms must be able to do, sort(). The classes QuickSort and BubbleSort both implements the
SortInterface and each have their own algorithm for sorting. SortingContext maintains a reference to a Strategy object and forwards client requests to the strategy. SortingClient set the concrete strategy in the context and invokes the context to run the algorithm.

public interface SortInterface {
public void sort(double[] list);
public class QuickSort implements SortInterface {
public void sort(double[] u) {
sort(u, 0, u.length - 1);
private void sort(double[] a, int left, int right) {
if (right <= left) return;
int i = part(a, left, right);
sort(a, left, i-1);
sort(a, i+1, right);
private int part(double[] a, int left, int right) {
int i = left;
int j = right;
while (true) {
while (a[i]< a[right])
while (smaller(a[right], a[--j]))
if (j == left) break;
if (i >= j) break;
swap(a, i, j);
swap(a, i, right);
return i;
private boolean smaller(double x, double y) {
return (x < y);
private void swap(double[] a, int i, int j) {
double swap = a[i]; a[i] = a[j]; a[j] = swap;
public class BubbleSort implements SortInterface {
public void sort(double[] list) {
//Bubblesort algorithm here

public class SortingContext {
private SortInterface sorter = null;
public void sortDouble(double[] list) {
public SortInterface getSorter() {
return sorter;
public void setSorter(SortInterface sorter) {
this.sorter = sorter;
public class SortingClient {
public static void main(String[] args) {
double[] list = {1,2.4,7.9,3.2,1.2,0.2,10.2,
SortingContext context = new SortingContext();
context.setSorter(new QuickSort());
for(int i =0; i< list.length; i++) {

When SortingClient is executed the result is:

Strategy Pattern Usage Example

The strategy pattern is used in the implementation of the LayoutManager in Java. The LayoutManager can be configured to work with different layout objects, such as FlowLayout, CardLayout, GridLayout, etc. These classes encapsulate the actual algorithms for laying out visual components that the LayoutManager uses to render the interface on the screen. By changing the
Layout (algorithm) we can dynamically change how the interface is rendered.
Annamalai Thangaraj

Annamalai is working as Technical Lead in Leading Telecom company with 5+ years experience in Identity and Access Management , Telecom and Networks, BigData, Java, Spring, Struts, Hibernate, AngularJS, and Enterprise Web Application Development.

Website: Java Tutorials Corner

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments


Post a Comment

Item Reviewed: Design Patterns - Strategy Pattern Rating: 5 Reviewed By: Annamalai Thangaraj