There are several approaches in another languages like
But what about implementation in c#? Any ideas?
There are several approaches in another languages like
But what about implementation in c#? Any ideas?
In my opinion, the Strategy Pattern is an 'anti-pattern'.
Fundamentally, this pattern suggests to utilize interfaces in order to abstract functions, which in turn it rephrases as 'strategies'.
A function abstraction is simply a delegate or function-pointer.
We do not need to create any classes, interfaces or any other OO-mumbo-jumbo, just to call it a 'pattern' and invent silly names in order to do 'Modern OOP'.
It is utterly absurd.
Now, sometimes people will tell, that the strategy-pattern is meant to design abstractions that consist of multiple functions (or members)...
...that is simply an interface.
So in order to use a 'functional approach' instead of the 'strategy pattern'... or rather a 'common-sense approach', instead of inventing silly nonsense... we use a delegate in C#:
public static void ForEach<T>(this IEnumerable<T> source, Action<T> f)
{
foreach (T item in source)
{
f(item);
}
}
https://learn.microsoft.com/en-us/dotnet/api/system.action?view=net-5.0
https://learn.microsoft.com/en-us/dotnet/api/system.func-1?view=net-5.0