I am here to discuss one of the popular behavioral design patterns, called Strategy. Before going through its implementation, let’s begin by defining it.
As per GOF guys, Strategy Pattern is defined as following.
“Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.”
Well! Let’s understand what they mean and where this pattern can be fit.
It simply means that this pattern is about having a set of concrete strategies or family of algorithms and choosing the appropriate one at runtime based on the need. Some of the use cases can be classified.
- Choosing an appropriate sort algorithm at runtime out of several (Quick sort, Merge sort, Insertion sort etc.).
- Selecting the right hash algorithm at runtime out of the bunch (MD5, SHA1, SHA256 etc.).
- Opting a relevant average calculation method (Mean, Median, Mode etc.) at runtime.
- Picking an applicable billing strategy (Regular, Happy hour etc.) at runtime.
- Setting a suitable shipping courier object (FedEx, Bluedart, DTDC) at runtime.
Another example of strategy pattern in .NET framework is Sort method (Array.Sort). It uses insertion, heap, and quicksort at runtime based on the incoming data.
Excerpts from MSDN tell,
- If the partition size is fewer than 16 elements, it uses an insertion sort algorithm.
- If the number of partitions exceeds 2 * LogN, where N is the range of the input array, it uses a Heapsort algorithm.
- Otherwise, it uses a Quicksort algorithm.
Now, let’s see the custom implementation of the pattern. Please have a look at the code map diagram of the demo we have created.

As you can see, Strategy pattern has some key components as given below.
- Strategy: -> Interface/abstract class for all supported algorithms. Here ISortStrategy is created to serve the purpose.
- ConcreteStrategy -> implements the algorithm defined in Strategy interface. Here QuickSort, HeapSort, and InsertionSort have been created for it.
- Context -> Sets the right strategy object and configured to use concrete strategy classes. Here, SortContext is doing this job.
- Client -> Calls the context object to set the context and then call the required method in context to do the job. Client class is serving the need here.
How Strategy pattern works
Let’s understand this by a demo project that we have created. First, create the Strategy interface to start with.
- using System.Collections.Generic;
- namespace StrategyPatternDemo
- {
- interface ISortStrategy
- {
- void Sort<T>(List<T> data);
- }
- }
As you can see, we have created a generic method to support all types of data. Now, let's create a concrete strategy class to implement the interface we have created above.
- using System;
- using System.Collections.Generic;
- namespace StrategyPatternDemo
- {
- class HeapSort : ISortStrategy
- {
- public void Sort<T>(List<T> list)
- {
- list.Sort();
- //We used default sort (.NET Fx) however you can write custom code as well
- Console.WriteLine($"After Sort using {this.GetType().Name}:");
- }
- }
- }
- //Namespace same as above
- class InsertionSort : ISortStrategy
- {
- public void Sort<T>(List<T> list)
- {
- list.Sort();
- //We used default sort (.NET Fx) however you can write custom code as well
- Console.WriteLine($"After Sort using {this.GetType().Name}:");
- }
- }
- //Namespace same as above
- class QuickSort : ISortStrategy
- {
- public void Sort<T>(List<T> list)
- {
- list.Sort();
- //We used default sort (.NET Fx) however you can write custom code as well
- Console.WriteLine($"After Sort using {this.GetType().Name}:");
- }
- }
As you can see in the above code that we have given unique implementations to each sort strategy, although we have used FCL Sort method in all three implementations as objective here is to explain design pattern instead of sorting algorithm, however, you can write your own code in each of them.
So at this point, we are done with strategy and concrete strategies and that is more than half of the code. Now, let’s create the remaining once and here is the code for Context i.e. SortContext.


Join the conversation! Your thoughts help the community grow.