Open In App
Related Articles

Strategy Method Design Pattern | C++ Design Patterns

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Strategy Pattern is a behavioral design pattern that defines a family of interchangeable algorithms and allows them to be used interchangeably within a context. This pattern enables the algorithm to be selected at runtime, providing flexibility and promoting code reusability.

Strat

Example of the Strategy Pattern in C++

Problem Statement:

Suppose you are working on a data processing application, and you need to implement a sorting feature that allows users to sort data in various ways. You decide to use the Strategy Pattern to handle different sorting algorithms.

Implementation of the Strategy Pattern in C++

Key Component of Strategy Patterns :

  • Context: The class that contains a reference to the strategy interface and is responsible for executing the algorithm.
  • Strategy Interface: An interface or abstract class that declares the method(s) for the algorithm. Different strategies implement this interface.
  • Concrete Strategies: The different algorithms that implement the strategy interface.

Step 1: Define the Strategy Interface

C++

class SortingStrategy {
public:
    virtual void sort(std::vector<int>& arr) = 0;
};

                    


Step 2: Implement Concrete Strategies

C++

class BubbleSort : public SortingStrategy {
public:
    void sort(std::vector<int>& arr) override {
        // Implement Bubble Sort algorithm
    }
};
 
class QuickSort : public SortingStrategy {
public:
    void sort(std::vector<int>& arr) override {
        // Implement Quick Sort algorithm
    }
};
 
// Add more sorting algorithms as needed

                    


Step 3: Create the Context

C++

class SortContext {
private:
    SortingStrategy* strategy;
 
public:
    void setStrategy(SortingStrategy* strategy) {
        this->strategy = strategy;
    }
 
    void executeStrategy(std::vector<int>& arr) {
        strategy->sort(arr);
    }
};

                    


Step 4: Utilize the Strategy Pattern

C++

int main() {
    std::vector<int> data = {5, 2, 7, 1, 9};
 
    SortContext context;
    BubbleSort bubbleSort;
    QuickSort quickSort;
 
    context.setStrategy(&bubbleSort);
    context.executeStrategy(data); // Executes Bubble Sort
 
    context.setStrategy(&quickSort);
    context.executeStrategy(data); // Executes Quick Sort
 
    return 0;
}

                    

Overall Code for the above Example:

C++

#include <bits/stdc++.h>
class SortingStrategy {
public:
    virtual void sort(std::vector<int>& arr) = 0;
};
class BubbleSort : public SortingStrategy {
public:
    void sort(std::vector<int>& arr) override
    {
        // Implement Bubble Sort algorithm
    }
};
 
class QuickSort : public SortingStrategy {
public:
    void sort(std::vector<int>& arr) override
    {
        // Implement Quick Sort algorithm
    }
};
 
// Add more sorting algorithms as needed
 
class SortContext {
private:
    SortingStrategy* strategy;
 
public:
    void setStrategy(SortingStrategy* strategy)
    {
        this->strategy = strategy;
    }
 
    void executeStrategy(std::vector<int>& arr)
    {
        strategy->sort(arr);
    }
};
int main()
{
    std::vector<int> data = { 5, 2, 7, 1, 9 };
 
    SortContext context;
    BubbleSort bubbleSort;
    QuickSort quickSort;
 
    context.setStrategy(&bubbleSort);
    context.executeStrategy(data); // Executes Bubble Sort
 
    context.setStrategy(&quickSort);
    context.executeStrategy(data); // Executes Quick Sort
 
    return 0;
}

                    

Diagrammatic Representation of Strategy Patterns in C++

S

  • Strategy Pattern allows the client code (in this case, the Context class) to choose between different sorting algorithms (represented by BubbleSort and QuickSort) at runtime, without modifying the client’s code.
  • This promotes flexibility because you can switch between different sorting algorithms without altering the client’s implementation.
  • It also promotes code reusability because you can add new sorting algorithms (concrete strategies) by implementing the SortingStrategy interface without modifying the existing code.

The use of an abstract class or interface (SortingStrategy) ensures that all concrete strategies have a consistent interface (sort(arr) method) that can be used interchangeably by the Context class.

Advantages of the Strategy Pattern in C++ Design Patterns

  • Flexibility: Easily switch between different algorithms at runtime.
  • Code Reusability: Strategies can be reused across different contexts.
  • Promotes Single Responsibility: Each strategy focuses on a specific algorithm.

Disadvantages of the Strategy Pattern in C++ Design Patterns

  • May lead to an increased number of classes, which can be overwhelming for small-scale applications.
  • Context and the Strategy classes normally communicate through the interface specified by the abstract Strategy base class. Strategy base class must expose interface for all the required behaviours, which some concrete Strategy classes might not implement.
  • In most cases, the application configures the Context with the required Strategy object. Therefore, the application needs to create and maintain two objects in place of one.


Last Updated : 05 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads