Using The Strategy Pattern In C#


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 from 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,
Using The Strategy Pattern In C#
Using The Strategy Pattern In C#
Using The Strategy Pattern In C# 
The final solution looks like this,
Using The Strategy Pattern In C#
The complete code is as below,
  1. // Program.cs  
  2. using System;  
  3. namespace ConsoleAppStrategyPattern {  
  4.     class Program {  
  5.         static void Main(string[] args) {  
  6.             var calculator = new InterestCalculator();  
  7.             calculator.SetPrincipleAmount(5000.00);  
  8.             calculator.SetInterestCalculationStrategy(new StatedRateMethod());  
  9.             calculator.CalculateInterest();  
  10.             Console.WriteLine($ "The interest amount by stated rate method is " + calculator.GetInterestAmount());  
  11.             calculator.SetInterestCalculationStrategy(new BankMethod());  
  12.             calculator.CalculateInterest();  
  13.             Console.WriteLine($ "The interest amount by bank method is " + calculator.GetInterestAmount());  
  14.             Console.ReadKey();  
  15.         }  
  16.     }  
  17. }  
  18. // CalculateInterestStrategy.cs  
  19. namespace ConsoleAppStrategyPattern {  
  20.     public abstract class CalculateInterestStrategy {  
  21.         public abstract double CalculateInterest(double amount);  
  22.     }  
  23. }  
  24. // StatedRateMethod.cs  
  25. using System;  
  26. namespace ConsoleAppStrategyPattern {  
  27.     public class StatedRateMethod: CalculateInterestStrategy {  
  28.         public override double CalculateInterest(double amount) {  
  29.             Console.WriteLine("Stated Rate Method applied");  
  30.             return amount / 365;  
  31.         }  
  32.     }  
  33. }  
  34. // BankMethod.cs  
  35. using System;  
  36. namespace ConsoleAppStrategyPattern {  
  37.     public class BankMethod: CalculateInterestStrategy {  
  38.         public override double CalculateInterest(double amount) {  
  39.             Console.WriteLine("Bank Method applied");  
  40.             return amount / 360;  
  41.         }  
  42.     }  
  43. }  
  44. // InterestCalculator.cs  
  45. namespace ConsoleAppStrategyPattern {  
  46.     public class InterestCalculator {  
  47.         private double _principleAmount = 0.0;  
  48.         private CalculateInterestStrategy _interestStrategy;  
  49.         private double _interestAmount = 0.0;  
  50.         public void SetInterestCalculationStrategy(CalculateInterestStrategy interestStrategy) {  
  51.             this._interestStrategy = interestStrategy;  
  52.         }  
  53.         public void SetPrincipleAmount(double amount) {  
  54.             _principleAmount = amount;  
  55.         }  
  56.         public void CalculateInterest() {  
  57.             _interestAmount = _interestStrategy.CalculateInterest(_principleAmount);  
  58.         }  
  59.         public double GetInterestAmount() {  
  60.             return _interestAmount;  
  61.         }  
  62.     }  
  63. }  
In the above code, we see that we have created an interest calculator class which 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,
Using The Strategy Pattern In C#


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!