Strategy
Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
The Problem
Your class does the same job multiple ways depending on context — different sort orders, different sync mechanisms, different payment providers. Using if/else chains or subclasses for each combination leads to explosion of code paths and makes adding new variants painful.
Structure
Execution Walkthrough
Define interface
Create a Strategy interface with the single method the Context will call (e.g. lock/unlock, execute, compress).
Concrete impls
Context stores
Inject at runtime
Delegate call
Code Comparison
Full C++ Implementation
Participants
Where it is used
C++ standard library
std::sort(begin, end, comparator) — sort algorithm fixed, ordering strategy injected as a callable.
Multicore simulators
Celeris plugs in CoarseGrained, FineGrained, or Atomic sync strategy without touching the engine loop.
Payment gateways
A checkout service swaps Stripe/PayPal/Razorpay by replacing one strategy object — no checkout logic changes.
Compression codecs
A file archiver selects zstd, lz4, or brotli at runtime — all implement compress()/decompress().