Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Algorithm Selection with Strategy | Behavioral Patterns
C++ Design Patterns

bookAlgorithm Selection with Strategy

The Strategy pattern is a behavioral design pattern that enables you to define a family of algorithms, encapsulate each algorithm in its own class, and make these algorithms interchangeable within a context. This approach allows you to select an algorithm at runtime, rather than hardcoding it into the context class. By decoupling the algorithm from the context, you gain flexibility and can easily add, replace, or modify algorithms without altering the context or client code.

main.cpp

main.cpp

copy
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
#include <iostream> #include <vector> #include <algorithm> #include <memory> class SortStrategy { public: virtual void sort(std::vector<int>& data) const = 0; virtual ~SortStrategy() = default; }; class BubbleSort : public SortStrategy { public: void sort(std::vector<int>& data) const override { for (size_t i = 0; i + 1 < data.size(); ++i) for (size_t j = 0; j + 1 < data.size() - i; ++j) if (data[j] > data[j + 1]) std::swap(data[j], data[j + 1]); } }; class StdSort : public SortStrategy { public: void sort(std::vector<int>& data) const override { std::sort(data.begin(), data.end()); } }; class SortContext { std::unique_ptr<SortStrategy> strategy; public: void set(std::unique_ptr<SortStrategy> s) { strategy = std::move(s); } void execute(std::vector<int>& data) const { strategy->sort(data); } }; int main() { std::vector<int> data = {5, 2, 9, 1, 6}; SortContext sorter; sorter.set(std::make_unique<BubbleSort>()); sorter.execute(data); std::cout << "BubbleSort: "; for (int n : data) std::cout << n << " "; std::cout << "\n"; sorter.set(std::make_unique<StdSort>()); data = {5, 2, 9, 1, 6}; sorter.execute(data); std::cout << "StdSort: "; for (int n : data) std::cout << n << " "; }

By encapsulating each algorithm as a separate class that implements a common interface, the Strategy pattern allows you to easily switch between different behaviors at runtime. This leads to code that is both flexible and maintainable, as you can introduce new algorithms or modify existing ones without changing the context class or the code that uses it. This separation of concerns is especially valuable when you need to support multiple algorithmic options that might evolve independently.

question mark

What is the main purpose and benefit of using the Strategy pattern in C++ design?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 4. Kapitel 2

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Awesome!

Completion rate improved to 10

bookAlgorithm Selection with Strategy

Stryg for at vise menuen

The Strategy pattern is a behavioral design pattern that enables you to define a family of algorithms, encapsulate each algorithm in its own class, and make these algorithms interchangeable within a context. This approach allows you to select an algorithm at runtime, rather than hardcoding it into the context class. By decoupling the algorithm from the context, you gain flexibility and can easily add, replace, or modify algorithms without altering the context or client code.

main.cpp

main.cpp

copy
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
#include <iostream> #include <vector> #include <algorithm> #include <memory> class SortStrategy { public: virtual void sort(std::vector<int>& data) const = 0; virtual ~SortStrategy() = default; }; class BubbleSort : public SortStrategy { public: void sort(std::vector<int>& data) const override { for (size_t i = 0; i + 1 < data.size(); ++i) for (size_t j = 0; j + 1 < data.size() - i; ++j) if (data[j] > data[j + 1]) std::swap(data[j], data[j + 1]); } }; class StdSort : public SortStrategy { public: void sort(std::vector<int>& data) const override { std::sort(data.begin(), data.end()); } }; class SortContext { std::unique_ptr<SortStrategy> strategy; public: void set(std::unique_ptr<SortStrategy> s) { strategy = std::move(s); } void execute(std::vector<int>& data) const { strategy->sort(data); } }; int main() { std::vector<int> data = {5, 2, 9, 1, 6}; SortContext sorter; sorter.set(std::make_unique<BubbleSort>()); sorter.execute(data); std::cout << "BubbleSort: "; for (int n : data) std::cout << n << " "; std::cout << "\n"; sorter.set(std::make_unique<StdSort>()); data = {5, 2, 9, 1, 6}; sorter.execute(data); std::cout << "StdSort: "; for (int n : data) std::cout << n << " "; }

By encapsulating each algorithm as a separate class that implements a common interface, the Strategy pattern allows you to easily switch between different behaviors at runtime. This leads to code that is both flexible and maintainable, as you can introduce new algorithms or modify existing ones without changing the context class or the code that uses it. This separation of concerns is especially valuable when you need to support multiple algorithmic options that might evolve independently.

question mark

What is the main purpose and benefit of using the Strategy pattern in C++ design?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 4. Kapitel 2
some-alt