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
Published at
2026-10-15 18:38:05 UTCEvent JSON
{
"id": "a64db4c19f88344f65598aa11f6c82996cb4b33631b07bc8b98ef0b8024968a6",
"pubkey": "e68a0856e90d9997b1f721e905249896412dd570399a987b2e8fd5e7fcfe2279",
"created_at": 1792089485,
"kind": 1,
"tags": [
[
"t",
"tip"
]
],
"content": "Learn: Strategy\n\nPut each version of an algorithm behind the same shape, and choose one at run time. No growing if/else chain.\n\n```csharp\nFunc\u003cdecimal, decimal\u003e blackFriday = p =\u003e p * 0.7m;\nConsole.WriteLine(Checkout(100m, blackFriday));\n\nstatic decimal Checkout(decimal price, Func\u003cdecimal, decimal\u003e discount) =\u003e discount(price);\n```\n\nIt prints 70.0 (decimal keeps one decimal place from 0.7).\nIn C#, a strategy can be an interface or just a Func. Use it for pricing rules, sorting orders, payment methods.\n\nRead more: https://www.geeksforgeeks.org/system-design/strategy-pattern-set-1/\n\n#tip · TIP-053",
"sig": "1ff954479c04c61130616740935ae7875bbda0aeb20485c45a62c3eb6dba3c7ce5801f2e0763b70a47d2594c94e5ddba92ec44ee7369e2c8a4a30b9200858a04"
}