Functional Strategy Pattern
Learn the functional strategy pattern in C# and F#. Discover how LINQ uses this widely adopted pattern and when to use functions over classes.
Another pattern that is widely used in the Object Oriented world. And as far as the .Net world is concerned, it may be the most used pattern in a wide range of .Net APIs. And it is heavily used in the heavily used LINQ API.
So, I guess giving an understanding of that is kinda a waste of space. Have a look at C# code.
People familiar with LINQ and lovers of Lambda will definitely argue that this can be replaced by delegates. And for the fact, I am arguing the same. But for now, I have taken this sample as a traditional practice.
Now, have a look at the F# code doing the same.
Now, it is definitely not a one to one replacement as I have done in the last few posts. But here I have replaced the class with a function, and we are achieving the same thing.
Now, what is the strategy pattern? As the name suggests, it is used to provide strategy from the consumer end. It is like I am saying that I want coffee and also I am telling you I want a cappuccino. And as shown in the sample for sorting, I am passing the sorting strategy also. For example, in this case I haven't passed an array; else I would have to pass an array or list and also the strategy or how to sort it. It is mainly used when the consumer has control on how to do the execution, but the execution is done by someone else.
Now, in the case of C# and Java8 where lambda is available, I have this functional behavior there also. But for the fact that it is a functional programming concept of passing functions around (or some may argue it is the Object Oriented concept in its purest form), and syntax-wise it is more suitable and human friendly in functional programming languages.
Here is a C# example.
It is very much near to the F# counterpart. Only F# syntax has less noise.
It's something altogether different when this pattern is shown in the context of Functional Programming. I am just composing functions. But it serves as the Strategy Pattern of OOP, so I have used that.
Frequently Asked Questions
The Strategy Pattern allows the consumer to specify how an operation should be executed by passing in a strategy or behavior. For example, you can pass a sorting strategy to a function, giving the consumer control over the execution method while the actual execution is handled elsewhere. It's widely used in .NET APIs, especially in LINQ.
Both C# and F# can implement the Strategy Pattern using lambda expressions and functions. However, F# has cleaner, less noisy syntax when passing functions around. In C#, you typically use delegates or lambda expressions, while F# treats function composition more naturally as a core language feature.
Yes, delegates and lambda expressions in C# can effectively replace traditional Strategy Pattern implementations. Modern C# with LINQ makes this a practical approach since lambdas provide the same functional behavior with more concise syntax, though the underlying concept remains the pattern itself.
The Strategy Pattern is fundamentally about passing functions around to control behavior, which is a core functional programming concept. While it originated in Object-Oriented programming, its essence—function composition and higher-order functions—aligns more naturally with functional languages where passing functions as parameters is a primary design technique.
The Strategy Pattern is heavily used throughout .NET APIs, particularly in LINQ. It appears anywhere you pass custom logic or behavior to a method, such as custom sorting, filtering, or transformation operations, allowing consumers to define the execution strategy themselves.