Strategy Design Pattern in C#

strategy design pattern using C#

In this article, we will learn what is a strategy design pattern and how to implement it using C#. We will use a real-world example of ImageProcessor to explain.

What is a design pattern?

In computer science and software development, a design pattern solves common problems in software design. A design pattern is not a fully completed design that can be translated into code directly. Instead, it is a blueprint or template for solving a specific problem that can be used in many situations. 

A design pattern is useful when moving from the analysis model to the development model. It speeds up the development process by providing proven development paradigms, which helps save time.

Strategy Design Pattern

Strategy is a behavioral design pattern that lets you design a family of algorithms and a strategy of the algorithm that can be changed at run time. Every algorithm of a class is implemented in a different, and while creating an object, we can assign the strategy we want to use. It may sound a bit complex, but I'll explain it in code, which will be easily understandable.

Implementation in C#

Suppose we are working on an Image Processor which is implementing various features. One of the features is Image Filtering. There can be different kinds of filtration, like Greyscale, Sepia, and Blur filters. Whenever the Image Processor runs, it will apply a selected filter. We'll define the filtration strategy in the processor, and when it will run, it will use that strategy. For that, let's define an Interface IFilterStrategy with a function ApplyFilter. ApplyFilter function takes imagePath as a parameter and will apply a filter on that Image.

public interface IFilterStrategy
{
    void ApplyFilter(string imagePath);
}

Now we'll define the algorithm of GreyscaleFilterStrategy, SepaFilterStrategy, and BlurFilterStrategy, which will inherit from the IFilterStrategy interface and will implement the ApplyFilter method accordingly.

// Implementation of Grayscale filter strategy
public class GrayscaleFilterStrategy : IFilterStrategy
{
    public void ApplyFilter(string imagePath)
    {
        // Logic to Apply grayscale filter to the image
    }
}

// Implementation ofSepia filter strategy
public class SepiaFilterStrategy : IFilterStrategy
{
    public void ApplyFilter(string imagePath)
    {
        //Logic to Apply sepia filter to the image
    }
}

// Implementation ofBlur filter strategy
public class BlurFilterStrategy : IFilterStrategy
{
    public void ApplyFilter(string imagePath)
    {
        //Logic to Apply blur filter to the image
    }
}

Now we will create a class of ImageProcessor

public class ImageProcessor
{
    private IFilterStrategy _filterStrategy;

    public void SetFilterStrategy(IFilterStrategy filterStrategy)
    {
        _filterStrategy = filterStrategy;
    }

    public void ApplyFilter(string imagePath)
    {
        if (_filterStrategy != null)
        {
            _filterStrategy.ApplyFilter(imagePath);
        }
        else
        {
            throw new InvalidOperationException("Filter strategy not set.");
        }
    }
}

In the above code, ImageProcessor class has a SetFilterStrategy method which takes filterStrategy as a parameter, and when executing the ApplyFilter method of ImageProcessor, it will execute the selected strategy. Let's do it in code.

ImageProcessor imageProcessor = new ImageProcessor();

IFilterStrategy filterStrategy = new GrayscaleFilterStrategy();
imageProcessor.SetFilterStrategy(filterStrategy);
imageProcessor.ApplyFilter("image.jpg");

filterStrategy = new SepiaFilterStrategy();
imageProcessor.SetFilterStrategy(filterStrategy);
imageProcessor.ApplyFilter("image.jpg");

I hope this example helps you understand strategy design patterns in the context of filtering strategies. 


Similar Articles