Step 1 of 7
Strategies as closures
A design pattern is a named, well-tested solution to a problem that keeps coming up. Knowing the names helps you recognize them in large codebases and talk about designs with other programmers. This module shows the ones you'll meet most, written the modern C++ way.
The Strategy pattern swaps an algorithm at run time: how to sort, how to price, how to compress. The textbook version uses an abstract base class with one subclass per strategy. When a strategy is a single function, modern C++ just uses std::function, and a function that returns a lambda builds configured strategies:
#include <functional>
#include <iostream>
#include <string>
#include <vector>
using Shipping = std::function<double(double weight_kg)>;
Shipping flat_rate(double price) {
return [price](double) { return price; };
}
Shipping per_kg(double rate, double minimum) {
return [rate, minimum](double kg) {
double cost = kg * rate;
return cost < minimum ? minimum : cost;
};
}
int main() {
std::vector<std::pair<std::string, Shipping>> options = {
{"standard", per_kg(1.5, 4.0)},
{"express", flat_rate(12.0)},
};
for (double kg : {1.0, 10.0}) {
for (const auto& [name, cost] : options) {
std::cout << kg << "kg " << name << ": " << cost(kg) << "\n";
}
}
}
1kg standard: 4
1kg express: 12
10kg standard: 15
10kg express: 12
Closures
Each lambda captures its settings (price, or rate and minimum), so every returned strategy remembers its own configuration after the factory function has returned. A lambda together with its captured values is called a closure. per_kg(1.5, 4.0) and per_kg(2.0, 5.0) are two independent strategies made by the same code.
Your task
percent_off(pct)returns a lambda capturingpctthat takes cents and subtractscents * pct / 100.flat_off(amount)subtractsamountbut never goes below 0.Cart::total()adds uppricesand then callsdiscount(sum)once on the total.
Your turn: write the strategy factories percent_off(pct) and flat_off(amount) (never below 0), and Cart::total(), which sums the prices and applies the cart's discount strategy once to the sum.