Strategy – It allows the programs to define a family of an algorithm, put each one into a separate class, and make their objects interchangeable.
From Design Pattern – Behavioral Patterns
Structure

Applicability
- Use this pattern when the programs want to use different variants of an algorithm within an object and switch from one algorithm to another during runtime.
- Use this pattern when the programs have a lot of similar classes, which only differ in the way and execute some behavior.
- Use this pattern to isolate the BLL class from the implementation details of an algorithm that may not be as important in the context of that logic.
- Use this pattern when the program’s class has a massive conditional operator, which switches between variants of the same algorithm.
Pros and Cons
PROS
- The program can swap algorithms used inside an object at runtime.
- The program can isolate the implementation details of an algorithm from the code that uses it.
- Replace inheritance with composition.
- Open-closed principle – the programs can introduce new strategies without changing the context object.
CONS
- If the program only has a couple of algorithms and it rarely changes, it’s no real reason to overcomplicate the program with new classes and interfaces, which come along with the pattern.
- The client’s side must be aware of the differences between the strategies to select a proper one.
- Many modern program languages have a few functional type support, which lets the programs implement different versions of an algorithm inside a set of anonymous functions. The programs could use these functions precisely as the program had have used the strategy objects, but without bloating the program’s code with extra class objects and the interface objects.
How to Implement
#1 Basic Sample
static void Main(string[] args)
{
var context = new Context();
context.SetStrategy(new ConcreteStrategyA());
context.DoSomething();
context.SetStrategy(new ConcreteStrategyB());
context.DoSomething();
Console.ReadKey();
}
public class Context
{
private IStrategy _strategy;
public Context() { }
public Context(IStrategy strategy) => _strategy = strategy;
public void SetStrategy(IStrategy strategy) => _strategy = strategy;
public void DoSomething()
{
var result = this._strategy.DoAlgorithm(new List<string> { "a", "b", "c", "d", "e" });
string resultStr = string.Empty;
foreach (var element in result as List<string>)
{
resultStr += element + ",";
}
Console.WriteLine(resultStr);
}
}
public interface IStrategy
{
object DoAlgorithm(object obj);
}
public class ConcreteStrategyA : IStrategy
{
public object DoAlgorithm(object obj)
{
var list = obj as List<string>;
list.Sort();
return list;
}
}
public class ConcreteStrategyB : IStrategy
{
public object DoAlgorithm(object obj)
{
var list = obj as List<string>;
list.Sort();
list.Reverse();
return list;
}
}
#2 Web Application Project
Step 1 Create the two interface class files in the Core projects. One interface class file name sets “IStrategy.” Another interface class file name sets “IStrategyServices.”


Step 2 Create the three class files. One class file name sets “StrategyContext.” Another class file name sets “ConcreteStrategySort.” Other class file name sets “ConcreteStrategyReverse.” The “ConcreteStrategySort” and the “ConcreteStrategyReverse” inherit the “IStrategy” interface class files.



Step 3 Create the service class file for the strategy pattern. This class file name sets “StrategyServices,” inheriting the “IStrategyServices” interface class files.

Step 4 Create the strategy pattern-related files in the web projects and register the Core project’s class file in the “Startup.cs” file.
// Strategy
services.AddScoped(typeof(IStrategyServices<>), typeof(StrategyServices<>));

Reference
1 Comment