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-11-23 09:07:38 UTCEvent JSON
{
"id": "84c43c11db5c64a55256fef6db0ec4b74915b0aee8567302ab6fff3c92a8c48a",
"pubkey": "e68a0856e90d9997b1f721e905249896412dd570399a987b2e8fd5e7fcfe2279",
"created_at": 1795424858,
"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": "22696515e5a3eb0abb65b3224b1cbdf3d4aaab85a90053177e0be4f2d8897adafeff8ec219579cda4c474d48e79fc56e6e6cdf521eb874903587da8dbdeb4217"
}