Home  >  Article  >  Backend Development  >  How to implement the Strategy Design Pattern in C++?

How to implement the Strategy Design Pattern in C++?

WBOY
WBOYOriginal
2024-06-06 16:16:17508browse

The steps to implement the strategy pattern in C++ are as follows: define the strategy interface and declare the methods that need to be executed. Create specific strategy classes, implement the interface respectively and provide different algorithms. Use a context class to hold a reference to a concrete strategy class and perform operations through it.

How to implement the Strategy Design Pattern in C++?

How to implement the Strategy Design Pattern in C++

Introduction

Strategy Pattern is a behavioral design pattern that allows you to change algorithms or behavior at runtime without modifying client code. This gives you the flexibility to switch algorithms without changing references to them.

Implementing the Strategy Pattern

Implementing the Strategy Pattern in C++ requires several steps:

  1. Define the basic strategy interface:This is the base class for all strategies, which declares the required methods.
  2. Create specific strategy classes: These classes implement the basic strategy interface and provide different algorithms or behaviors.
  3. Using context classes: It holds a reference to a specific strategy class and uses it to perform specific operations.

Practical Case

Suppose you have a sorting algorithm that requires different comparison algorithms. You can easily achieve this using Strategy Pattern.

Basic policy interface:

class Comparator {
public:
    virtual bool compare(int a, int b) = 0;
};

Specific policy class:

class AscendingComparator : public Comparator {
public:
    bool compare(int a, int b) override { return a < b; }
};

class DescendingComparator : public Comparator {
public:
    bool compare(int a, int b) override { return a > b; }
};

Context class:

class Sorter {
public:
    Sorter(Comparator* comparator) : comparator(comparator) {}

    void sort(int* arr, int size) {
        for (int i = 0; i < size - 1; i++) {
            for (int j = i + 1; j < size; j++) {
                if (comparator->compare(arr[i], arr[j])) {
                    std::swap(arr[i], arr[j]);
                }
            }
        }
    }

private:
    Comparator* comparator;
};

Usage:

int main() {
    int arr[] = {5, 3, 1, 2, 4};
    int size = sizeof(arr) / sizeof(int);

    Sorter sorter(new AscendingComparator());
    sorter.sort(arr, size);

    for (int i = 0; i < size; i++) {
        cout << arr[i] << " ";  // Output: 1 2 3 4 5
    }
    cout << "\n";

    sorter.setComparator(new DescendingComparator());
    sorter.sort(arr, size);

    for (int i = 0; i < size; i++) {
        cout << arr[i] << " ";  // Output: 5 4 3 2 1
    }
    cout << "\n";

    return 0;
}

In this example, the Sorter class can sort the array according to the provided comparison strategy. By changing the comparison strategy, we can easily switch between ascending and descending sorting without modifying the Sorter logic.

The above is the detailed content of How to implement the Strategy Design Pattern in C++?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn