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-10 10:37:56 UTCEvent JSON
{
"id": "468cb654983a2c9b19634a0b39fb11d7f12aeabedd4b23c19a3a2c9e8b992130",
"pubkey": "e68a0856e90d9997b1f721e905249896412dd570399a987b2e8fd5e7fcfe2279",
"created_at": 1791628676,
"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": "8c7c0ed686f87c56a335a52bf0e9f7b31825836b59ee8b1da22f07a3f1a2f72aafcbb8f2530974a2bf9fd2760de3e3da0767900e58b9352d9e8641651b49c557"
}