The Strategy Design Pattern is a behavioral pattern that defines a group of related algorithms, encapsulates each one in a separate class, and makes them interchangeable. It allows the algorithm to vary independently from the client that uses it, enabling behavior changes at runtime without altering existing code.
- Encapsulates different algorithms or behaviors into separate strategy classes.
- Allows dynamic selection or switching of algorithms at runtime.
- Promotes flexibility and reduces conditional logic in the code.

Components

1. Context: A class or object known as the Context assigns the task to a strategy object and contains a reference to it.
- It serves as an intermediary between the client and the strategy, offering an integrated approach for task execution without exposing every detail of the process.
- The Context maintains a reference to a strategy object and calls its methods to perform the task, allowing for interchangeable strategies to be used.
2. Strategy Interface: An abstract class or interface known as the Strategy Interface specifies a set of methods that all concrete strategies must implement.
- As a kind of agreement, it guarantees that all strategies follow the same set of rules and are interchangeable by the Context.
- The Strategy Interface promotes flexibility and modularity in the design by establishing a common interface that enables decoupling between the Context and the specific strategies.
3. Concrete Strategies: Concrete Strategies are the various implementations of the Strategy Interface. Each concrete strategy provides a specific algorithm or behavior for performing the task defined by the Strategy Interface.
- Concrete strategies encapsulate the details of their respective algorithms and provide a method for executing the task.
- They are interchangeable and can be selected and configured by the client based on the requirements of the task.
4. Client: The Client is responsible for selecting and configuring the appropriate strategy and providing it to the Context.
- It knows the requirements of the task and decides which strategy to use based on those requirements.
- The client creates an instance of the desired concrete strategy and passes it to the Context, enabling the Context to use the selected strategy to perform the task.
Working
The Strategy pattern works by delegating a specific task to interchangeable algorithm classes.
- A Strategy interface defines a common method for all algorithms.
- Concrete strategy classes implement this interface with different behaviors.
- The context class holds a reference to a strategy object.
- At runtime, the client selects or changes the strategy, and the context uses it to perform the task.
Uses
The Strategy pattern is used when an application needs to choose different behaviors or algorithms dynamically based on context.
- Selecting different algorithms at runtime (e.g., payment methods, sorting strategies)
- Eliminating large conditional statements (if-else or switch cases)
- Making behavior easily extendable without modifying existing code
Example
Problem Statement:
Let's consider a sorting application where we need to sort a list of integers. However, the sorting algorithm to be used may vary depending on factors such as the size of the list and the desired performance characteristics.
Challenges Without Using Strategy Pattern:
- Limited Flexibility: Implementing sorting algorithms directly within the main sorting class can make the code inflexible. Adding new sorting algorithms or changing existing ones would require modifying the main class, which violates the Open/Closed Principle.
- Code Duplication: Without a clear structure, you may end up duplicating sorting logic to handle different algorithms.
- Hard-Coded Logic: Implementing sorting logic directly within the main sorting class can make the code rigid and difficult to extend or modify.
Benefits of Using the Strategy Pattern to solve above challenges
Here's how the Strategy Pattern helps:
- Code Reusability: By encapsulating sorting algorithms into separate strategy classes, you can reuse these strategies across different parts of the system. This reduces code duplication and promotes maintainability.
- Flexibility and Extensibility: The Strategy Pattern makes it simple to adapt or add new sorting algorithms without changing the existing code. Since each strategy is independent, it is possible to change or expand it without impacting other system components.
- Separation of Concerns: The Strategy Pattern separates sorting logic into distinct strategy classes, which encourages a clear division of responsibilities. As a result, the code is easier to test, and maintain.

Implementation of the above example:
This example shows the practical application of the design pattern using code.
1. Context(SortingContext)
public class SortingContext {
private SortingStrategy sortingStrategy;
public SortingContext(SortingStrategy sortingStrategy) {
this.sortingStrategy = sortingStrategy;
}
public void setSortingStrategy(SortingStrategy sortingStrategy) {
this.sortingStrategy = sortingStrategy;
}
public void performSort(int[] array) {
sortingStrategy.sort(array);
}
}
public interface SortingStrategy {
void sort(int[] array);
}
public class SortingContext {
private SortingStrategy sortingStrategy;
public SortingContext(SortingStrategy sortingStrategy) {
this.sortingStrategy = sortingStrategy;
}
public void setSortingStrategy(SortingStrategy sortingStrategy) {
this.sortingStrategy = sortingStrategy;
}
public void performSort(int[] array) {
sortingStrategy.sort(array);
}
}
from abc import ABC, abstractmethod
class SortingStrategy(ABC):
@abstractmethod
def sort(self, array):
pass
class SortingContext:
def __init__(self, sorting_strategy):
self.sorting_strategy = sorting_strategy
def set_sorting_strategy(self, sorting_strategy):
self.sorting_strategy = sorting_strategy
def perform_sort(self, array):
self.sorting_strategy.sort(array)
class SortingStrategy {
sort(array) {
throw new Error('sort method must be implemented');
}
}
class SortingContext {
constructor(sortingStrategy) {
this.sortingStrategy = sortingStrategy;
}
setSortingStrategy(sortingStrategy) {
this.sortingStrategy = sortingStrategy;
}
performSort(array) {
this.sortingStrategy.sort(array);
}
}
2. Strategy Interface(SortingStrategy)
public interface SortingStrategy {
void sort(int[] array);
}
public interface SortingStrategy {
void sort(int[] array);
// Implement sorting logic here
}
from abc import ABC, abstractmethod
class SortingStrategy(ABC):
@abstractmethod
def sort(self, array):
pass
# Implement sorting logic here
def sort(self, array):
array.sort()
const SortingStrategy = {
sort: function(array) {
// Implement sorting logic here
array.sort();
}
};
3. Concrete Strategies
// BubbleSortStrategy
public class BubbleSortStrategy implements SortingStrategy {
@Override
public void sort(int[] array) {
// Implement Bubble Sort algorithm
System.out.println("Sorting using Bubble Sort");
}
}
// MergeSortStrategy
public class MergeSortStrategy implements SortingStrategy {
@Override
public void sort(int[] array) {
// Implement Merge Sort algorithm
System.out.println("Sorting using Merge Sort");
}
}
// QuickSortStrategy
public class QuickSortStrategy implements SortingStrategy {
@Override
public void sort(int[] array) {
// Implement Quick Sort algorithm
System.out.println("Sorting using Quick Sort");
}
}
// BubbleSortStrategy
public class BubbleSortStrategy implements SortingStrategy {
@Override
public void sort(int[] array) {
// Implement Bubble Sort algorithm
System.out.println("Sorting using Bubble Sort");
}
}
// MergeSortStrategy
public class MergeSortStrategy implements SortingStrategy {
@Override
public void sort(int[] array) {
// Implement Merge Sort algorithm
System.out.println("Sorting using Merge Sort");
}
}
// QuickSortStrategy
public class QuickSortStrategy implements SortingStrategy {
@Override
public void sort(int[] array) {
// Implement Quick Sort algorithm
System.out.println("Sorting using Quick Sort");
}
}
class BubbleSortStrategy:
def sort(self, array):
# Implement Bubble Sort algorithm
print("Sorting using Bubble Sort")
class MergeSortStrategy:
def sort(self, array):
# Implement Merge Sort algorithm
print("Sorting using Merge Sort")
class QuickSortStrategy:
def sort(self, array):
# Implement Quick Sort algorithm
print("Sorting using Quick Sort")
class BubbleSortStrategy {
sort(array) {
// Implement Bubble Sort algorithm
console.log("Sorting using Bubble Sort");
}
}
class MergeSortStrategy {
sort(array) {
// Implement Merge Sort algorithm
console.log("Sorting using Merge Sort");
}
}
class QuickSortStrategy {
sort(array) {
// Implement Quick Sort algorithm
console.log("Sorting using Quick Sort");
}
}
4. Client Component
public class Client {
public static void main(String[] args) {
// Create SortingContext with BubbleSortStrategy
SortingContext sortingContext = new SortingContext(new BubbleSortStrategy());
int[] array1 = {5, 2, 9, 1, 5};
sortingContext.performSort(array1); // Output: Sorting using Bubble Sort
// Change strategy to MergeSortStrategy
sortingContext.setSortingStrategy(new MergeSortStrategy());
int[] array2 = {8, 3, 7, 4, 2};
sortingContext.performSort(array2); // Output: Sorting using Merge Sort
// Change strategy to QuickSortStrategy
sortingContext.setSortingStrategy(new QuickSortStrategy());
int[] array3 = {6, 1, 3, 9, 5};
sortingContext.performSort(array3); // Output: Sorting using Quick Sort
}
}
public interface SortingStrategy {
void sort(String[] array);
}
class BubbleSortStrategy implements SortingStrategy {
@Override
public void sort(String[] array) {
System.out.println("Sorting using Bubble Sort");
// Bubble sort implementation
}
}
class MergeSortStrategy implements SortingStrategy {
@Override
public void sort(String[] array) {
System.out.println("Sorting using Merge Sort");
// Merge sort implementation
}
}
class QuickSortStrategy implements SortingStrategy {
@Override
public void sort(String[] array) {
System.out.println("Sorting using Quick Sort");
// Quick sort implementation
}
}
class SortingContext {
private SortingStrategy strategy;
public SortingContext(SortingStrategy strategy) {
this.strategy = strategy;
}
public void performSort(String[] array) {
strategy.sort(array);
}
public void setSortingStrategy(SortingStrategy strategy) {
this.strategy = strategy;
}
}
public class Client {
public static void main(String[] args) {
// Create SortingContext with BubbleSortStrategy
SortingContext sortingContext = new SortingContext(new BubbleSortStrategy());
String[] array1 = {"cpp","c","java","python3","csharp","html","css","javascript","php","cpp14","cobol","dart","go","julia","kotlin","lisp","matlab","node","objc","perl","r","rust","ruby","scala","swift","solidity","xml"};
sortingContext.performSort(array1); // Output: Sorting using Bubble Sort
// Change strategy to MergeSortStrategy
sortingContext.setSortingStrategy(new MergeSortStrategy());
String[] array2 = {"cpp","c","java","python3","csharp","html","css","javascript","php","cpp14","cobol","dart","go","julia","kotlin","lisp","matlab","node","objc","perl","r","rust","ruby","scala","swift","solidity","xml"};
sortingContext.performSort(array2); // Output: Sorting using Merge Sort
// Change strategy to QuickSortStrategy
sortingContext.setSortingStrategy(new QuickSortStrategy());
String[] array3 = {"cpp","c","java","python3","csharp","html","css","javascript","php","cpp14","cobol","dart","go","julia","kotlin","lisp","matlab","node","objc","perl","r","rust","ruby","scala","swift","solidity","xml"};
sortingContext.performSort(array3); // Output: Sorting using Quick Sort
}
}
from abc import ABC, abstractmethod
class SortingStrategy(ABC):
@abstractmethod
def sort(self, array):
pass
class BubbleSortStrategy(SortingStrategy):
def sort(self, array):
print('Sorting using Bubble Sort')
# Bubble sort implementation
class MergeSortStrategy(SortingStrategy):
def sort(self, array):
print('Sorting using Merge Sort')
# Merge sort implementation
class QuickSortStrategy(SortingStrategy):
def sort(self, array):
print('Sorting using Quick Sort')
# Quick sort implementation
class SortingContext:
def __init__(self, strategy):
self.strategy = strategy
def perform_sort(self, array):
self.strategy.sort(array)
def set_sorting_strategy(self, strategy):
self.strategy = strategy
# Create SortingContext with BubbleSortStrategy
sorting_context = SortingContext(BubbleSortStrategy())
array1 = ['cpp','c','java','python3','csharp','html','css','javascript','php','cpp14','cobol','dart','go','julia','kotlin','lisp','matlab','node','objc','perl','r','rust','ruby','scala','swift','solidity','xml']
sorting_context.perform_sort(array1) # Output: Sorting using Bubble Sort
# Change strategy to MergeSortStrategy
sorting_context.set_sorting_strategy(MergeSortStrategy())
array2 = ['cpp','c','java','python3','csharp','html','css','javascript','php','cpp14','cobol','dart','go','julia','kotlin','lisp','matlab','node','objc','perl','r','rust','ruby','scala','swift','solidity','xml']
sorting_context.perform_sort(array2) # Output: Sorting using Merge Sort
# Change strategy to QuickSortStrategy
sorting_context.set_sorting_strategy(QuickSortStrategy())
array3 = ['cpp','c','java','python3','csharp','html','css','javascript','php','cpp14','cobol','dart','go','julia','kotlin','lisp','matlab','node','objc','perl','r','rust','ruby','scala','swift','solidity','xml']
sorting_context.perform_sort(array3) # Output: Sorting using Quick Sort
class SortingStrategy {
sort(array) {}
}
class BubbleSortStrategy extends SortingStrategy {
sort(array) {
console.log('Sorting using Bubble Sort');
// Bubble sort implementation
}
}
class MergeSortStrategy extends SortingStrategy {
sort(array) {
console.log('Sorting using Merge Sort');
// Merge sort implementation
}
}
class QuickSortStrategy extends SortingStrategy {
sort(array) {
console.log('Sorting using Quick Sort');
// Quick sort implementation
}
}
class SortingContext {
constructor(strategy) {
this.strategy = strategy;
}
performSort(array) {
this.strategy.sort(array);
}
setSortingStrategy(strategy) {
this.strategy = strategy;
}
}
// Create SortingContext with BubbleSortStrategy
let sortingContext = new SortingContext(new BubbleSortStrategy());
let array1 = ['cpp','c','java','python3','csharp','html','css','javascript','php','cpp14','cobol','dart','go','julia','kotlin','lisp','matlab','node','objc','perl','r','rust','ruby','scala','swift','solidity','xml'];
sortingContext.performSort(array1); // Output: Sorting using Bubble Sort
// Change strategy to MergeSortStrategy
sortingContext.setSortingStrategy(new MergeSortStrategy());
let array2 = ['cpp','c','java','python3','csharp','html','css','javascript','php','cpp14','cobol','dart','go','julia','kotlin','lisp','matlab','node','objc','perl','r','rust','ruby','scala','swift','solidity','xml'];
sortingContext.performSort(array2); // Output: Sorting using Merge Sort
// Change strategy to QuickSortStrategy
sortingContext.setSortingStrategy(new QuickSortStrategy());
let array3 = ['cpp','c','java','python3','csharp','html','css','javascript','php','cpp14','cobol','dart','go','julia','kotlin','lisp','matlab','node','objc','perl','r','rust','ruby','scala','swift','solidity','xml'];
sortingContext.performSort(array3); // Output: Sorting using Quick Sort
Complete code for the above example
#include <iostream>
#include <vector>
class SortingStrategy {
public:
virtual void sort(std::vector<std::string>& array) = 0;
};
class BubbleSortStrategy : public SortingStrategy {
public:
void sort(std::vector<std::string>& array) override {
std::cout << "Sorting using Bubble Sort" << std::endl;
// Actual Bubble Sort Logic here
}
};
class MergeSortStrategy : public SortingStrategy {
public:
void sort(std::vector<std::string>& array) override {
std::cout << "Sorting using Merge Sort" << std::endl;
// Actual Merge Sort Logic here
}
};
class QuickSortStrategy : public SortingStrategy {
public:
void sort(std::vector<std::string>& array) override {
std::cout << "Sorting using Quick Sort" << std::endl;
// Actual Quick Sort Logic here
}
};
class SortingContext {
private:
SortingStrategy* sortingStrategy;
public:
SortingContext(SortingStrategy* sortingStrategy) : sortingStrategy(sortingStrategy) {}
void setSortingStrategy(SortingStrategy* sortingStrategy) {
this->sortingStrategy = sortingStrategy;
}
void performSort(std::vector<std::string>& array) {
sortingStrategy->sort(array);
}
};
int main() {
std::vector<std::string> array1 = {"cpp","c","java","python3","csharp","html","css","javascript","php","cpp14","cobol","dart","go","julia","kotlin","lisp","matlab","node","objc","perl","r","rust","ruby","scala","swift","solidity","xml"};
SortingContext sortingContext(new BubbleSortStrategy());
sortingContext.performSort(array1); // Output: Sorting using Bubble Sort
sortingContext.setSortingStrategy(new MergeSortStrategy());
std::vector<std::string> array2 = {"cpp","c","java","python3","csharp","html","css","javascript","php","cpp14","cobol","dart","go","julia","kotlin","lisp","matlab","node","objc","perl","r","rust","ruby","scala","swift","solidity","xml"};
sortingContext.performSort(array2); // Output: Sorting using Merge Sort
sortingContext.setSortingStrategy(new QuickSortStrategy());
std::vector<std::string> array3 = {"cpp","c","java","python3","csharp","html","css","javascript","php","cpp14","cobol","dart","go","julia","kotlin","lisp","matlab","node","objc","perl","r","rust","ruby","scala","swift","solidity","xml"};
sortingContext.performSort(array3); // Output: Sorting using Quick Sort
return 0;
}
class SortingContext {
private SortingStrategy sortingStrategy;
public SortingContext(SortingStrategy sortingStrategy) {
this.sortingStrategy = sortingStrategy;
}
public void setSortingStrategy(SortingStrategy sortingStrategy) {
this.sortingStrategy = sortingStrategy;
}
public void performSort(int[] array) {
sortingStrategy.sort(array);
}
}
// SortingStrategy.java
interface SortingStrategy {
void sort(int[] array);
}
// BubbleSortStrategy.java
class BubbleSortStrategy implements SortingStrategy {
@Override
public void sort(int[] array) {
// Implement Bubble Sort algorithm
System.out.println("Sorting using Bubble Sort");
// Actual Bubble Sort Logic here
}
}
// MergeSortStrategy.java
class MergeSortStrategy implements SortingStrategy {
@Override
public void sort(int[] array) {
// Implement Merge Sort algorithm
System.out.println("Sorting using Merge Sort");
// Actual Merge Sort Logic here
}
}
// QuickSortStrategy.java
class QuickSortStrategy implements SortingStrategy {
@Override
public void sort(int[] array) {
// Implement Quick Sort algorithm
System.out.println("Sorting using Quick Sort");
// Actual Quick Sort Logic here
}
}
// Client.java
public class Client {
public static void main(String[] args) {
// Create SortingContext with BubbleSortStrategy
SortingContext sortingContext = new SortingContext(new BubbleSortStrategy());
int[] array1 = {5, 2, 9, 1, 5};
sortingContext.performSort(array1); // Output: Sorting using Bubble Sort
// Change strategy to MergeSortStrategy
sortingContext.setSortingStrategy(new MergeSortStrategy());
int[] array2 = {8, 3, 7, 4, 2};
sortingContext.performSort(array2); // Output: Sorting using Merge Sort
// Change strategy to QuickSortStrategy
sortingContext.setSortingStrategy(new QuickSortStrategy());
int[] array3 = {6, 1, 3, 9, 5};
sortingContext.performSort(array3); // Output: Sorting using Quick Sort
}
}
from abc import ABC, abstractmethod
class SortingStrategy(ABC):
@abstractmethod
def sort(self, array):
pass
class BubbleSortStrategy(SortingStrategy):
def sort(self, array):
print('Sorting using Bubble Sort')
# Actual Bubble Sort Logic here
class MergeSortStrategy(SortingStrategy):
def sort(self, array):
print('Sorting using Merge Sort')
# Actual Merge Sort Logic here
class QuickSortStrategy(SortingStrategy):
def sort(self, array):
print('Sorting using Quick Sort')
# Actual Quick Sort Logic here
class SortingContext:
def __init__(self, sorting_strategy):
self.sorting_strategy = sorting_strategy
def set_sorting_strategy(self, sorting_strategy):
self.sorting_strategy = sorting_strategy
def perform_sort(self, array):
self.sorting_strategy.sort(array)
# Client
array1 = ["cpp","c","java","python3","csharp","html","css","javascript","php","cpp14","cobol","dart","go","julia","kotlin","lisp","matlab","node","objc","perl","r","rust","ruby","scala","swift","solidity","xml"]
sorting_context = SortingContext(BubbleSortStrategy())
sorting_context.perform_sort(array1) # Output: Sorting using Bubble Sort
sorting_context.set_sorting_strategy(MergeSortStrategy())
array2 = ["cpp","c","java","python3","csharp","html","css","javascript","php","cpp14","cobol","dart","go","julia","kotlin","lisp","matlab","node","objc","perl","r","rust","ruby","scala","swift","solidity","xml"]
sorting_context.perform_sort(array2) # Output: Sorting using Merge Sort
sorting_context.set_sorting_strategy(QuickSortStrategy())
array3 = ["cpp","c","java","python3","csharp","html","css","javascript","php","cpp14","cobol","dart","go","julia","kotlin","lisp","matlab","node","objc","perl","r","rust","ruby","scala","swift","solidity","xml"]
sorting_context.perform_sort(array3) # Output: Sorting using Quick Sort
class SortingContext {
constructor(sortingStrategy) {
this.sortingStrategy = sortingStrategy;
}
setSortingStrategy(sortingStrategy) {
this.sortingStrategy = sortingStrategy;
}
performSort(array) {
this.sortingStrategy.sort(array);
}
}
class SortingStrategy {
sort(array) {
throw new Error('This method should be overridden!');
}
}
class BubbleSortStrategy extends SortingStrategy {
sort(array) {
console.log('Sorting using Bubble Sort');
// Actual Bubble Sort Logic here
}
}
class MergeSortStrategy extends SortingStrategy {
sort(array) {
console.log('Sorting using Merge Sort');
// Actual Merge Sort Logic here
}
}
class QuickSortStrategy extends SortingStrategy {
sort(array) {
console.log('Sorting using Quick Sort');
// Actual Quick Sort Logic here
}
}
// Client
const sortingContext = new SortingContext(new BubbleSortStrategy());
const array1 = ['cpp','c','java','python3','csharp','html','css','javascript','php','cpp14','cobol','dart','go','julia','kotlin','lisp','matlab','node','objc','perl','r','rust','ruby','scala','swift','solidity','xml'];
sortingContext.performSort(array1); // Output: Sorting using Bubble Sort
sortingContext.setSortingStrategy(new MergeSortStrategy());
const array2 = ['cpp','c','java','python3','csharp','html','css','javascript','php','cpp14','cobol','dart','go','julia','kotlin','lisp','matlab','node','objc','perl','r','rust','ruby','scala','swift','solidity','xml'];
sortingContext.performSort(array2); // Output: Sorting using Merge Sort
sortingContext.setSortingStrategy(new QuickSortStrategy());
const array3 = ['cpp','c','java','python3','csharp','html','css','javascript','php','cpp14','cobol','dart','go','julia','kotlin','lisp','matlab','node','objc','perl','r','rust','ruby','scala','swift','solidity','xml'];
sortingContext.performSort(array3); // Output: Sorting using Quick Sort
Output
Sorting using Bubble Sort Sorting using Merge Sort Sorting using Quick Sort
Real Life Software Examples
Below are some real-life software examples where the Strategy Pattern is commonly used:
- Multiple payment methods (Credit Card, PayPal, Bank Transfer, UPI etc.) on an E-Commerce site. Each payment method has its own way to process, verify, handle failures, possibly different fees. Defining a PaymentStrategy interface and concrete implementations for each payment method picks the right one based on user choice at runtime.
- File compression utilities can employ the Strategy pattern to offer different compression methods (e.g., ZIP, GZIP, TAR) to the user, allowing them to choose the desired compression strategy.
Advantages
This pattern improves flexibility and maintainability by separating behavior from the main logic.
- Promotes open/closed principle by allowing new strategies to be added easily
- Makes code cleaner and easier to maintain
- Enables runtime behavior changes without code modification
Disadvantages
While flexible, the pattern can introduce extra complexity.
- Increases the number of classes in the system
- Clients must be aware of different strategies
- May add slight overhead due to additional object creation

