Using The Strategy Pattern In C#

Introduction

In today’s article, we will look at the Strategy pattern. We will look at what this pattern is, the advantages of using it, when it should and should not be used, and finally, we will look at a sample implementation of this pattern in a C# application.

The Strategy Pattern

The strategy pattern is used when we want to create different algorithms for the implementation of a particular task and want to keep them independent and make them interchangeable with the clients that are using them. This might sound a bit confusing but after looking at a sample application using this pattern, things will be clearer.

A simple implementation of the Strategy Pattern

We will now look at a simple implementation of the strategy pattern. This application has been created using the Visual Studio 2019 Community Edition, which is a free version.

We create a new console application in .NET Core 3.1, as below.

 .NET Core

Project

Configure

The final solution looks like this.

 Final solution

The complete code is as below.

// Program.cs 
using System; 
namespace ConsoleAppStrategyPattern { 
    class Program { 
        static void Main(string[] args) { 
            var calculator = new InterestCalculator(); 
            calculator.SetPrincipleAmount(5000.00); 
            calculator.SetInterestCalculationStrategy(new StatedRateMethod()); 
            calculator.CalculateInterest(); 
            Console.WriteLine($"The interest amount by stated rate method is " + calculator.GetInterestAmount()); 
            calculator.SetInterestCalculationStrategy(new BankMethod()); 
            calculator.CalculateInterest(); 
            Console.WriteLine($"The interest amount by bank method is " + calculator.GetInterestAmount()); 
            Console.ReadKey(); 
        } 
    } 
} 

// CalculateInterestStrategy.cs 
namespace ConsoleAppStrategyPattern { 
    public abstract class CalculateInterestStrategy { 
        public abstract double CalculateInterest(double amount); 
    } 
} 

// StatedRateMethod.cs 
using System; 
namespace ConsoleAppStrategyPattern { 
    public class StatedRateMethod: CalculateInterestStrategy { 
        public override double CalculateInterest(double amount) { 
            Console.WriteLine("Stated Rate Method applied"); 
            return amount / 365; 
        } 
    } 
} 

// BankMethod.cs 
using System; 
namespace ConsoleAppStrategyPattern { 
    public class BankMethod: CalculateInterestStrategy { 
        public override double CalculateInterest(double amount) { 
            Console.WriteLine("Bank Method applied"); 
            return amount / 360; 
        } 
    } 
} 

// InterestCalculator.cs 
namespace ConsoleAppStrategyPattern { 
    public class InterestCalculator { 
        private double _principleAmount = 0.0; 
        private CalculateInterestStrategy _interestStrategy; 
        private double _interestAmount = 0.0; 
        public void SetInterestCalculationStrategy(CalculateInterestStrategy interestStrategy) { 
            this._interestStrategy = interestStrategy; 
        } 
        public void SetPrincipleAmount(double amount) { 
            _principleAmount = amount; 
        } 
        public void CalculateInterest() { 
            _interestAmount = _interestStrategy.CalculateInterest(_principleAmount); 
        } 
        public double GetInterestAmount() { 
            return _interestAmount; 
        } 
    } 
} 

In the above code, we see that we have created an interest calculator class that calculates the interest for a principal amount provided. Please note that in this example I have not really implemented the two methods of calculating interest. I am simply dividing the principle amount by 365 for the stated rate method and dividing the principle amount by 360 for the bank method. The thing to note here is that the calling code, located in the main method calls the interest calculator class and also sets the strategy by which the interest is to be calculated and these strategy classes are separate from the calculator class itself. Hence, extending this pattern is extremely easy. This pattern might not be needed where such variations in strategy are not required.

When we run the application, we get the below.

 Application

Summary

In this article, we looked at the strategy pattern and what are the advantages of using this pattern. The example given here is a simple one, but the main idea was to give an outline of how this pattern is to be implemented. Happy Coding!


Similar Articles