The Strategy design pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
Intent
The intent of the Strategy design pattern helps us to divide an algorithm from a host class and then move it to another class. By doing so the client can choose which algorithm will be performed in runtime from a set of algorithms that were implemented earlier.
Motivation & Applicability
There are common situations when classes differ only in their behaviour. For this case, it is a good idea to isolate the algorithms in separate classes in order to have the ability to select different algorithms at runtime. The Strategy pattern allows us to provide an alternative to subclassing the Context class to get a variety of algorithms or behaviours, eliminates large conditional statements and provides a choice of implementations for the same behaviour.
- Many related classes differ only in their behaviour
- You need different variants of an algorithm
- 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 own Strategy class.
- Strategy (CalculateStrategy using ICalculateInterface) - declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy
- ConcreteStrategy (Minus, Plus ) - implements the algorithm using the Strategy interface
- Context (CalculateClient) - is configured with a ConcreteStrategy object; maintains a reference to a Strategy object and may define an interface that lets Strategy access it's data.

C# Code Snippet
- /* Select the strategy and execute
- Here the strategies are Minus and Plus
- */
- class MainApp
- {
- static void Main()
- {
- CalculateClient minusClient = new CalculateClient(new Minus());
- Response.Write("Minus: " + minusClient.Calculate(7, 1).ToString());
- CalculateClient plusClient = new CalculateClient(new Plus());
- Response.Write("Plus: " + plusClient.Calculate(7, 1).ToString());
- // Wait for user
- Console.ReadKey();
- }
- }
- public interface ICalculateInterface
- {
- //define method
- int Calculate(int value1, int value2);
- }
The 'ConcreteAggregate' classes
Strategy 1: Minus
- class Minus : ICalculateInterface
- {
- public int Calculate(int value1, int value2)
- {
- //define logic
- return value1 - value2;
- }
- }
- class Plus : ICalculateInterface
- {
- public int Calculate(int value1, int value2)
- {
- //define logic
- return value1 + value2;
- }
- }
- class CalculateClient
- {
- private ICalculateInterface calculateInterface;
- //Constructor: assigns strategy to interface
- public CalculateClient(ICalculateInterface strategy)
- {
- calculateInterface = strategy;
- }
- //Executes the strategy
- public int Calculate(int value1, int value2)
- {
- return calculateInterface.Calculate(value1, value2);
- }
- }
Minus: 6
Plus: 8
subeer haldarPosted Feb 28, 2020, 2:16 PM
Strategy make the algorithms interchangeable, so could you please explain how plus and minus are interchangeable?
Hamid KhanPosted Feb 7, 2020, 10:28 PM
Good job.................
sures ctPosted Aug 1, 2017, 8:45 AM
But it is not clear in what contexts the strategy is applied. Could you please explain?
Anshu SinghPosted Jan 8, 2013, 1:19 AM
Take a look here... http://infostuffs.com/2012/12/29/2-understanding-net-the-c-environment/
Blocked AccountPosted Feb 24, 2011, 11:05 PM
Nice Article Shinuraj, Keep it up.
Raja KrishnamurthyPosted Feb 24, 2011, 8:41 AM
It was nice to see a pattern explained in such a simple manner :-)....great job!!!