Algorithm 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
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.
Merci pour vos commentaires !
Demandez à l'IA
Demandez à l'IA
Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion
Can you give an example of the Strategy pattern in code?
What are some real-world scenarios where the Strategy pattern is useful?
How does the Strategy pattern differ from other behavioral design patterns?
Awesome!
Completion rate improved to 10
Algorithm Selection with Strategy
Glissez pour afficher le menu
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
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.
Merci pour vos commentaires !