Strategy Pattern C#

Contents

What Is Strategy Pattern C# ?

The Strategy pattern in C# lets the algorithm vary independently from clients that use it.

There may be different algorithms (strategies) that apply to a given problem. If the algorithms are all kept in the client, messy code with lots of conditional statements will result.

Strategy pattern defines a family of algorithms, encapsulate each one, and make them interchangeable.

The Strategy pattern enables a client to choose which algorithm to use from a family of algorithms and gives it a simple way to access it.

Below is the UML and sequence diagram of Strategy pattern from Wikipedia.

Strategy pattern

Strategy Pattern C# Example

Strategy Pattern Using Context Class

Let’s start with the problem statement,

Now, In our imaginary store we 25% discount on price of item for the months of July-December. And, we do not provide any discount for the months of Jan-June.

Let’s see how this fit in strategy pattern,

 1 using System;
 2 using System.Collections.Generic;
 3 
 4 /* Interface for Strategy */
 5 interface IOfferStrategy
 6 {
 7     string Name { get; }
 8     double GetDiscountPercentage();
 9 }
10 
11 /* Concrete implementation of base Strategy */
12 class NoDiscountStrategy : IOfferStrategy
13 {
14     public string Name => nameof(NoDiscountStrategy);
15 
16     public double GetDiscountPercentage()
17     {
18         return 0;
19     }
20 }
21 
22 /* Concrete implementation of base Strategy */
23 class QuarterDiscountStrategy : IOfferStrategy
24 {
25     public string Name => nameof(QuarterDiscountStrategy);
26     
27     public double GetDiscountPercentage()
28     {
29         return 0.25;
30     }
31 }

Here we declared two strategies, NoDiscountStrategy and QuarterDiscountStrategy, as per problem statement.

Let’s continue with the example and implement the context,

 1 class StrategyContext
 2 {
 3     double price; // price for some item or air ticket etc.
 4     Dictionary<string, IOfferStrategy> strategyContext 
 5         = new Dictionary<string, IOfferStrategy>();
 6     public StrategyContext(double price)
 7     {
 8         this.price = price;
 9         strategyContext.Add(nameof(NoDiscountStrategy), 
10                 new NoDiscountStrategy());
11         strategyContext.Add(nameof(QuarterDiscountStrategy), 
12                 new QuarterDiscountStrategy());
13     }
14 
15     public void ApplyStrategy(IOfferStrategy strategy)
16     {
17         /*
18         Currently applyStrategy has simple implementation. 
19         You can Context for populating some more information,
20         which is required to call a particular operation
21         */
22         Console.WriteLine("Price before offer :" + price);
23         double finalPrice 
24             = price - (price * strategy.GetDiscountPercentage());
25         Console.WriteLine("Price after offer:" + finalPrice);
26     }
27 
28     public IOfferStrategy GetStrategy(int monthNo)
29     {
30         /*
31         In absence of this Context method, client has to import 
32         relevant concrete Strategies everywhere.
33         Context acts as single point of contact for the Client 
34         to get relevant Strategy
35         */
36         if (monthNo < 6)
37         {
38             return strategyContext[nameof(NoDiscountStrategy)];
39         }
40         else
41         {
42             return strategyContext[nameof(QuarterDiscountStrategy)];
43         }
44     }
45 }

In strategy pattern context is optional. But if it is present, it acts as single point of contact for client.

Now, we can implement the client code,

 1 static void Main(string[] args)
 2 {
 3     StrategyContext context = new StrategyContext(100);
 4     Console.WriteLine("Enter month number between 1 and 12");
 5     var input = Console.ReadLine();
 6     int month = Convert.ToInt32(input);
 7     Console.WriteLine("Month =" + month);
 8     IOfferStrategy strategy = context.GetStrategy(month);
 9     context.ApplyStrategy(strategy);
10     Console.ReadLine();
11 }

Above example shows the usage of Strategy pattern with Context. Context can be used as single point of contact for the Client.

As shown in the output below, you will get discount depending on the month you have entered.

1 output:
2 Enter month number between 1 and 12
3 Month =1
4 Price before offer :100.0
5 Price after offer:100.0
6 Enter month number between 1 and 12
7 Month =7
8 Price before offer :100.0
9 Price after offer:75.0

Multiple uses of Context

  1. It can populate data to execute an operation of strategy
  2. It can take independent decision on Strategy creation.
  3. In absence of Context, client should be aware of concrete strategies. Context acts a wrapper and hides internals
  4. Code re-factoring will become easy

Strategy Pattern Without Using A Context Class

The following is a simple example of using the strategy pattern without a context class.

 1 using System;
 2 
 3 class Program
 4 {
 5     // The strategy interface
 6     public interface ITranslationStrategy
 7     {
 8         string Translate(string phrase);
 9     }
10     // American strategy implementation
11     public class AmericanTranslationStrategy : ITranslationStrategy
12     {
13         
14         public string Translate(string phrase)
15         {
16             return phrase + ", bro";
17         }
18     }
19 
20     // Australian strategy implementation
21     public class AustralianTranslationStrategy : ITranslationStrategy
22     {
23         
24         public string Translate(string phrase)
25         {
26             return phrase + ", mate";
27         }
28     }
29 
30     // The main class which exposes a translate method
31     public class EnglishTranslation
32     {
33         // translate a phrase using a given strategy
34         public static string Translate(string phrase, 
35                 ITranslationStrategy strategy)
36         {
37             return strategy.Translate(phrase);
38         }
39 
40         // example usage
41         static void Main(string[] args)
42         {
43             // translate a phrase using the AustralianTranslationStrategy class
44             string aussieHello = Translate("Hello", 
45                     new AustralianTranslationStrategy());
46             Console.WriteLine(aussieHello);
47             // Hello, mate
48             // translate a phrase using the AmericanTranslationStrategy class
49             string usaHello = Translate("Hello", 
50                     new AmericanTranslationStrategy());
51             Console.WriteLine(usaHello);
52             // Hello, bro
53 
54             Console.ReadLine();
55         }
56     }
57 
58 }

Here we have two implementation strategies which implement the interface and solve the same problem in different ways.

Users of the EnglishTranslation class can call the translate method and choose which strategy they would like to use for the translation, by specifying the desired strategy.

Strategy Pattern Using Service Class

The purpose of this example is to show how we can implement Strategy pattern using a Service Class.

The example problem we using is a family of algorithms (strategies) that describe different ways to communicate over a distance.

The contract for our family of algorithms is defined by the following interface:

1 public interface ICommunicateInterface
2 {
3     string Communicate(string destination);
4 }

Then we can implement a number of algorithms, as follows:

 1 public class CommunicateViaPhone : ICommunicateInterface
 2 {
 3     public string Communicate(string destination)
 4     {
 5         return "communicating " + destination + " via Phone..";
 6     }
 7 }
 8 
 9 public class CommunicateViaEmail : ICommunicateInterface
10 {
11     public string Communicate(string destination)
12     {
13         return "communicating " + destination + " via Email..";
14     }
15 }
16 
17 public class CommunicateViaVideo : ICommunicateInterface
18 {
19     public string Communicate(string destination)
20     {
21         return "communicating " + destination + " via Video..";
22     }
23 }

These can be instantiated as follows:

1 CommunicateViaPhone communicateViaPhone = new CommunicateViaPhone();
2 CommunicateViaEmail communicateViaEmail = new CommunicateViaEmail();
3 CommunicateViaVideo communicateViaVideo = new CommunicateViaVideo();

Next, we implement a service that uses the strategy:

 1 public class CommunicationService
 2 {
 3     private ICommunicateInterface communcationMeans;
 4     public void SetCommuncationMeans(ICommunicateInterface communcationMeans)
 5     {
 6         this.communcationMeans = communcationMeans;
 7     }
 8     public void Communicate(string destination)
 9     {
10         var communicate = communcationMeans.Communicate(destination);
11         Console.WriteLine(communicate);
12     }
13 }

Finally, we can use the different strategies as follows:

 1 static void Main(string[] args)
 2 {
 3     CommunicateViaPhone communicateViaPhone = new CommunicateViaPhone();
 4     CommunicateViaEmail communicateViaEmail = new CommunicateViaEmail();
 5     CommunicateViaVideo communicateViaVideo = new CommunicateViaVideo();
 6 
 7     CommunicationService communicationService = new CommunicationService();
 8     // via phone
 9     communicationService.SetCommuncationMeans(communicateViaPhone);
10     communicationService.Communicate("1234567");
11     // via email
12     communicationService.SetCommuncationMeans(communicateViaEmail);
13     communicationService.Communicate("hi@me.com");
14 
15     Console.ReadLine();
16 
17 }

Strategy Pattern Using C# Delegates

The contract of the different algorithm implementations does not need a dedicated interface.

Instead, we can describe it using .NET built-in generic delegate type Func. For more information on Delegates, see my blog post Delegates And Events In C# and C# Generic Delegate.

The different algorithms composing the family of algorithms can be expressed as lambda expressions. This replaces the strategy classes and their instantiations.

1 string communicateViaEmail(string destination) 
2     => "communicating " + destination + " via Email..";
3 string communicateViaPhone(string destination) 
4     => "communicating " + destination + " via Phone..";

Next, we can code the “service” as follows:

 1 public class CommunicationService
 2 {
 3     private Func<string, string> communcationMeans;
 4     public void SetCommuncationMeans(Func<string, string> communcationMeans)
 5     {
 6         this.communcationMeans = communcationMeans;
 7     }
 8     public void Communicate(string destination)
 9     {
10         var communicate = communcationMeans(destination);
11         Console.WriteLine(communicate);
12     }
13 }

Finally we use the strategies as follows:

1 CommunicationService communicationService = new CommunicationService();
2 // via phone
3 communicationService.SetCommuncationMeans(communicateViaPhone);
4 communicationService.Communicate("1234567");
5 // via email
6 communicationService.SetCommuncationMeans(communicateViaEmail);
7 communicationService.Communicate("hi@me.com");

Or even:

1 //via Video
2 communicationService.SetCommuncationMeans((string destination) 
3         => "communicating " + destination + " via Video..");
4 communicationService.Communicate("1234567");

Note: You can download the complete solution demo from my github repository.

Where To Apply Strategy Pattern?

  • When many related classes differ only in their behavior. Strategies provide a way to configure a class with one of many behaviors.

  • You need different variants of an algorithm. For example, you might define algorithms reflecting different space/time trade-offs. Strategies can be used when these variants are implemented as a class hierarchy of algorithms.

  • An algorithm uses data that clients shouldn’t know about. Use the Strategy pattern to avoid exposing complex, algorithm-specific data structures.

  • A class defines many behaviors, and these appear as multiple conditional statements in its operations. Instead of many conditionals, move-related conditional branches into their Strategy class.

Further Reading

Strategy Pattern C#
Share this

Subscribe to Code with Shadman