The Strategy design pattern offers a powerful solution for making algorithms interchangeable within your applications. Let's cover C# application and implementation in this article.
By encapsulating related algorithms within a family and allowing them to be swapped seamlessly, this pattern enhances flexibility and maintainability.
In this post, we'll dive into the Strategy pattern, explore its key concepts, and implement a simple example in C#.
Understanding the Strategy Pattern
The Strategy pattern is a behavioral design pattern that defines a family of algorithms, encapsulates each one, and makes them interchangeable. It allows the algorithm to vary independently from the clients that use it.
Key Components
- Strategy Interface : A common interface for all supported algorithms.
- Concrete Strategies : Implementations of the strategy interface with specific algorithms.
- Context : Maintains a reference to a strategy object and allows clients to set or change the strategy at runtime.
Implementing the Strategy Pattern in C
Let's implement the Strategy pattern with a simple example of sorting algorithms.
1. Define the Strategy Interface
First, we define an interface that all sorting strategies will implement:
public interface ISortStrategy
{
void Sort(List<int> list);
}
2. Implement Concrete Strategies
Next, we create concrete classes that implement the sorting algorithms:
BubbleSortStrategy
public class BubbleSortStrategy : ISortStrategy
{
public void Sort(List<int> list)
{
for (int i = 0; i < list.Count - 1; i++)
{
for (int j = 0; j < list.Count - i - 1; j++)
{
if (list[j] > list[j + 1])
{
int temp = list[j];
list[j] = list[j + 1];
list[j + 1] = temp;
}
}
}
}
}
QuickSortStrategy
public class QuickSortStrategy : ISortStrategy
{
public void Sort(List<int> list)
{
QuickSort(list, 0, list.Count - 1);
}
private void QuickSort(List<int> list, int low, int high)
{
if (low < high)
{
int pi = Partition(list, low, high);
QuickSort(list, low, pi - 1);
QuickSort(list, pi + 1, high);
}
}
private int Partition(List<int> list, int low, int high)
{
int pivot = list[high];
int i = (low - 1);
for (int j = low; j < high; j++)
{
if (list[j] <= pivot)
{
i++;
int temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
int temp1 = list[i + 1];
list[i + 1] = list[high];
list[high] = temp1;
return i + 1;
}
}
3. Create the Context Class
The context class maintains a reference to a strategy object and allows clients to change the strategy:
public class SortContext
{
private ISortStrategy _sortStrategy;
public SortContext(ISortStrategy sortStrategy)
{
_sortStrategy = sortStrategy;
}
public void SetStrategy(ISortStrategy sortStrategy)
{
_sortStrategy = sortStrategy;
}
public void Sort(List<int> list)
{
_sortStrategy.Sort(list);
}
}
4. Using the Strategy Pattern
Finally, we use the context class to apply different sorting strategies:
class Program
{
static void Main(string[] args)
{
List<int> numbers = new List<int> { 34, 7, 23, 32, 5, 62 };
// Use BubbleSort strategy
SortContext context = new SortContext(new BubbleSortStrategy());
context.Sort(numbers);
Console.WriteLine("Bubble Sorted List: " + string.Join(", ", numbers));
// Change to QuickSort strategy
context.SetStrategy(new QuickSortStrategy());
numbers = new List<int> { 34, 7, 23, 32, 5, 62 }; // Reset the list
context.Sort(numbers);
Console.WriteLine("Quick Sorted List: " + string.Join(", ", numbers));
}
}
Conclusion
The Strategy design pattern in C# provides a robust framework for creating interchangeable algorithms, promoting flexibility and maintainability in your applications.
By encapsulating each algorithm within its own class and using a common interface, the Strategy pattern adheres to the open/closed principle, making it easier to introduce new algorithms without modifying existing code. Embrace the Strategy pattern to enhance your software development practices and build more adaptable systems.
For more detailed articles and hands-on tutorials on .NET and C#, check out my blog at rmauro.dev.
Top comments (0)