Strategy puts a family of interchangeable algorithms behind one interface. The client (the context) holds a strategy and delegates the work to it, so the algorithm can be chosen or replaced without changing the client. It is the object-oriented answer to a growing if/elif chain.

The problem: an if-chain

Here the order total depends on a discount kind. Every new discount edits total, and a typo in kind surfaces only at runtime. The function knows all algorithms, so it breaks the open/closed principle.

python
def total(items, kind, value=0):
    amount = sum(items)
    if kind == "none":
        return amount
    elif kind == "percent":
        return amount * (100 - value) // 100
    elif kind == "fixed":
        return max(amount - value, 0)
    raise ValueError(kind)

print(total([40, 60, 50], "percent", 10), total([40, 60, 50], "fixed", 30))
text
135 120

Strategies as classes

Order holds a Discount strategy; three classes implement it
Order holds a Discount strategy; three classes implement it