Join Nostr
2026-10-15 18:38:05 UTC

codementor on Nostr: Learn: Strategy Put each version of an algorithm behind the same shape, and choose ...

Learn: Strategy

Put each version of an algorithm behind the same shape, and choose one at run time. No growing if/else chain.

```csharp
Func<decimal, decimal> blackFriday = p => p * 0.7m;
Console.WriteLine(Checkout(100m, blackFriday));

static decimal Checkout(decimal price, Func<decimal, decimal> discount) => discount(price);
```

It prints 70.0 (decimal keeps one decimal place from 0.7).
In C#, a strategy can be an interface or just a Func. Use it for pricing rules, sorting orders, payment methods.

Read more: https://www.geeksforgeeks.org/system-design/strategy-pattern-set-1/

#tip · TIP-053