Open In App

Strategy Design Pattern

Last Updated : 26 Sep, 2025
Comments
Improve
Suggest changes
36 Likes
Like
Report

Strategy Design Pattern is a behavioral design pattern that allows you to define a family of algorithms or behaviors, put each of them in a separate class, and make them interchangeable at runtime. This pattern is useful when you want to dynamically change the behavior of a class without modifying its code.

Strategy-design-pattern
Strategy Design Pattern

Real Life Software Examples

  • 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.

Components of the Strategy Design Pattern

Components-of-Strategy-Design-Pattern-

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.

Communication between the Components

In the Strategy Design Pattern, communication between the components occurs in a structured and decoupled manner. Here's how the components interact with each other:

Client -> Context: The client selects and configures a suitable strategy, then passes it to the context to execute the task.
Context -> Strategy: The context holds the strategy reference and delegates execution to it via the common interface.
Strategy -> Context: The strategy executes its algorithm, returns results, or performs necessary actions for the context to use.
Strategy Interface: Defines a contract ensuring all strategies are interchangeable.
Decoupling: Context remains unaware of strategy details, enabling flexibility and easy substitution.

Real-World Analogy of Strategy Design Pattern

Imagine you're planning a trip to a new city, and you have several options for getting there: by car, by train, or by plane. Each mode of transportation offers its own set of advantages and disadvantages, depending on factors such as cost, travel time, and convenience.

  • Context: You, as the traveler, represent the context in this analogy. You have a specific goal (reaching the new city) and need to choose the best transportation strategy to achieve it.
  • Strategies: The different modes of transportation (car, train, plane) represent the strategies in this analogy. Each strategy (mode of transportation) offers a different approach to reaching your destination.
  • Interface: The interface in this analogy is the set of common criteria you consider when choosing a transportation mode, such as cost, travel time, and convenience.
  • Flexibility: Just as the Strategy Design Pattern allows you to dynamically switch between different algorithms at runtime, you have the flexibility to choose a transportation mode based on your specific requirements and constraints.
  • Dynamic Selection: The Strategy Design Pattern allows you to dynamically select the best strategy (transportation mode) based on changing circumstances. For instance, if your initial flight is canceled due to bad weather, you can quickly switch to an alternative mode of transportation

Strategy Design Pattern Example(with implementation)

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.

How Strategy Pattern helps 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.

StrategyDesignPatternExample

Below is the implementation of the above example:

1. Context(SortingContext)

C++
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);
    }
}
Java
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);
    }
}
Python
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)
JavaScript
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)

C++
public interface SortingStrategy {
    void sort(int[] array);
}
Java
public interface SortingStrategy {
    void sort(int[] array);
    // Implement sorting logic here
}
Python
from abc import ABC, abstractmethod

class SortingStrategy(ABC):
    @abstractmethod
    def sort(self, array):
        pass

    # Implement sorting logic here
    def sort(self, array):
        array.sort()
JavaScript
const SortingStrategy = {
    sort: function(array) {
        // Implement sorting logic here
        array.sort();
    }
};

3. Concrete Strategies

C++
// 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");
    }
}
Java
// 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");
    }
}
Python
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")
JavaScript
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

C++
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
    }
}
Java
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
    }
}
Python
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
JavaScript
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

Below is the complete code for the above example:

C++
#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;
}
Java
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
    }
}
Python
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
JavaScript
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



Strategy Design Pattern
Visit Course explore course icon
Article Tags :

Explore