Strategy
- C++ course
- Advanced
- Lesson 33/83
- 5 min
Learn the family of algorithms with the Strategy pattern.
What is the Strategy pattern?
Strategy encapsulates algorithms behind an interface and makes them swappable at runtime. When several methods do the same job and the choice is made at runtime, Strategy fits.
In C++ you define an abstract Strategy class; each algorithm subclasses it. A Context holds a Strategy pointer and can swap it with setStrategy. Example: a sort context might use bubble or quick sort.
Context and setStrategy
A Sorter context holds a SortStrategy pointer. The setStrategy method swaps the algorithm at runtime. The context does not know how the algorithm works; it only calls the sort() virtual method.
Example: sorter.setStrategy(std::make_unique<AscendingSort>()); picks ascending sort. sorter.run(v); sorts the vector. To add a new algorithm you only write a new SortStrategy subclass; Sorter stays unchanged. This gives the open-closed principle.
Practice Write a Sorter with Strategy
Define an abstract SortStrategy with virtual void sort(std::vector<int>& v) const = 0;. An AscendingSort subclass calls std::sort(v.begin(), v.end()). A Sorter context holds a unique_ptr<SortStrategy> and its run(v) method calls the strategy. In main sort {3,1,2} ascending and print the elements side by side.
Starter code
#include <iostream>
#include <memory>
#include <vector>
#include <algorithm>
// Define the SortStrategy abstract class and AscendingSort class
// Define the Sorter context
int main() {
// Sort {3,1,2} and print it
return 0;
}Sample solution
#include <iostream>
#include <memory>
#include <vector>
#include <algorithm>
class SortStrategy {
public:
virtual ~SortStrategy() = default;
virtual void sort(std::vector<int>& v) const = 0;
};
class AscendingSort : public SortStrategy {
public:
void sort(std::vector<int>& v) const override {
std::sort(v.begin(), v.end());
}
};
class Sorter {
std::unique_ptr<SortStrategy> strategy;
public:
explicit Sorter(std::unique_ptr<SortStrategy> s) : strategy(std::move(s)) {}
void run(std::vector<int>& v) const { strategy->sort(v); }
};
int main() {
std::vector<int> v = {3, 1, 2};
Sorter s(std::make_unique<AscendingSort>());
s.run(v);
for (int x : v) std::cout << x;
return 0;
}Expected output
123
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 When do you use the Strategy pattern?
- When the same task has multiple algorithms Correct answer
- When a single instance is needed
- When object creation should be hidden
- When event notification is needed
Explanation: Strategy is used when the same task has different algorithms.
2 Match each piece to its role
| Term | Meaning |
|---|---|
class SortStrategy |
Abstract algorithm |
class AscendingSort |
Concrete algorithm |
class Sorter |
Context |
setStrategy(...) |
Swaps the algorithm |
3 Complete the setStrategy call
sorter.____(std::make_unique<AscendingSort>());
sorter.setStrategy(std::make_unique<AscendingSort>());
Explanation: setStrategy changes the context's algorithm.