A checkout that learned Pix
The first version of an online shop's checkout took cards only, and Checkout.pay(order) called the card processor directly. Then the shop added Pix, Brazil's instant-payment system, and the method grew an if. Pix does not work like a card. Instead of answering approved or declined, the shop shows the customer a QR code and learns a little later that it was paid. So the Pix branch brought its own fields, its own error handling and its own tests, all inside the same method as the card branch.
Boleto, a bank slip the customer pays days later, is planned for next quarter, and it will be a third branch in the same place. No single branch is wrong. The trouble is that several algorithms for the same job live inside the class that uses them, so that class has to know every one of them, and a change to any payment method means editing and retesting the class every purchase goes through.
Each payment method becomes an object
Strategy captures a family of interchangeable algorithms behind one interface and lets the caller pick which one to use at runtime. Here the interface is PaymentStrategy, with a single method, pay(order), and each payment method implements it: CardStrategy, PixStrategy, later BoletoStrategy. The class that uses them, which the pattern calls the Context, holds a reference to a PaymentStrategy and calls pay(order) on it without knowing which one it holds.
Read the diagram from the left. Checkout talks to one interface, and each concrete strategy points at it because it implements it. Adding boleto means one more box on the right, and Checkout's code does not change, which is the open/closed principle from SOLID design principles. Because Checkout only holds a reference, and that reference can be replaced, the strategy can also change while the program runs: the customer switches from card to Pix on the payment page, and the next call to pay() goes to a different object.
The if did not disappear, it moved
Someone still has to decide which strategy to use, and that decision depends on what the customer picked, which Checkout should not need to know. So the decision moves to the code that does know it: the request handler that reads the payment method from the form and hands Checkout the matching strategy. That is often a lookup rather than a chain of ifs, a map from card, pix and boleto to the code that builds each strategy for this request.
The gain is not that the choice vanished. It is that the choice now happens in one place, once, instead of inside every method that touches payment. Handing an object its collaborators from outside like this is Dependency Injection, and it is how a Context usually receives its strategy.
if/else on strategy type inside the Context instead of letting the caller inject the concrete strategy. That is the exact sprawl Strategy is meant to remove, and every new payment method then means editing Checkout again.One interface for algorithms that finish differently
The hard part of Strategy is usually the interface, not the classes. A card charge typically comes back approved or declined within the request, while a Pix charge comes back with a QR code and is confirmed later. If pay() returns a plain true or false, Pix has no honest way to answer. The tempting patch is for Checkout to check which strategy it holds and treat Pix specially, and that brings the if straight back.
The fix is a result every strategy can fill in truthfully, for example approved, declined or pending, with whatever the next screen needs (the QR code, in Pix's case). Inputs work the same way: pass pay() what every strategy needs, and give each strategy its own extras, such as a card token, when it is created.
When it is too much, and what it is not
Strategy pays off when there are several algorithms that change independently of each other, or that are chosen at runtime. Two branches that have not changed in years do not need an interface and three classes. In languages where functions are values, a strategy is often just a function passed in, checkout.pay(order, payWithPix): the same idea without a class per algorithm.
Three patterns look like Strategy on a diagram, and the difference is who decides what. In State, the Context also delegates to an interchangeable object, but the state objects usually switch the Context to the next state themselves as things happen; strategies do not know about each other, and the choice comes from outside. Template Method varies a step of an algorithm through inheritance, fixed when the subclass is written, where Strategy swaps the whole algorithm through composition, at runtime. Bridge uses the same composition behind an interface, but to let two hierarchies grow independently rather than to choose an algorithm.
Swap the payment without touching Checkout
Open the Strategy Pattern challenge and focus on one thing: the Context depending only on the Strategy interface. Use a Component node for the Context (Checkout, here), a Port / Interface node for PaymentStrategy, and a Strategy node for each payment method. Then check the arrows. Checkout should point at the interface and nothing else; if it points at a concrete strategy, adding or swapping a payment method means changing Checkout, and a good run needs no change to Checkout at all.