Strategy Design Pattern Using C# Sample

Introduction

 
Strategic design pattern is a behavioral design pattern in which the behavior of the object is encapsulated with a common function name.
 
Where to use Strategic design pattern?
 
When different customers have a common strategy (common operation name) but a different action to perform, then we can use strategic design pattern.
 
Why use Strategic design pattern?
 
As we discussed previously, customers have a common operation name to perform, so that we can share  a common interface to all customers to perform their actions as per their need.
 
Players in this pattern,
  • Strategy: It defines contract with common operation.
  • ConcreteStrategy : It implements contact defined by Strategy.
  • Product: It fullfils customer requirements using ConcreteStrategy objects by referring Strategy contract.
We will see strategic design pattern with an example. 

Problem definition

 
Define a common operation for students to access different actions like addition, subtraction, multiplication, division.
 
Players in this case,
  • Strategy : IOperator
  • ConcreteStrategy: AddOperator, SubtractOperator, MultiplyOperator, DivisionOperator.
  • Product : Calculator
Below is the IOperator contract which defined common operation called ExecuteOperation
  1. namespace Strategy.Design.Pattern.Contract  
  2. {  
  3.     public interface IOperator  
  4.     {  
  5.         void ExecuteOperation(int a, int b);  
  6.     }  
  7. }  
Below are the concrete operators which implement contract defined by IOperator
  1. using Strategy.Design.Pattern.Contract;  
  2. using static System.Console;  
  3.   
  4. namespace Strategy.Design.Pattern.Business  
  5. {  
  6.     public class AddOperator : IOperator  
  7.     {  
  8.         public void ExecuteOperation(int a, int b)  
  9.         {  
  10.             WriteLine($"{nameof(AddOperator)} result: {a + b}");  
  11.         }  
  12.     }  
  13. }  
  1. using Strategy.Design.Pattern.Contract;  
  2. using static System.Console;  
  3.   
  4. namespace Strategy.Design.Pattern.Business  
  5. {  
  6.     public class SubtractOperator : IOperator  
  7.     {  
  8.         public void ExecuteOperation(int a, int b)  
  9.         {  
  10.             WriteLine($"{nameof(SubtractOperator)} result: {a - b}");  
  11.         }  
  12.     }  
  13. }  
  1. using Strategy.Design.Pattern.Contract;  
  2. using static System.Console;  
  3.   
  4. namespace Strategy.Design.Pattern.Business  
  5. {  
  6.     public class MultiplyOperator : IOperator  
  7.     {  
  8.         public void ExecuteOperation(int a, int b)  
  9.         {  
  10.             WriteLine($"{nameof(MultiplyOperator)} result: {a * b}");  
  11.         }  
  12.     }  
  13. }  
  1. using Strategy.Design.Pattern.Contract;  
  2. using static System.Console;  
  3.   
  4. namespace Strategy.Design.Pattern.Business  
  5. {  
  6.     public class DivisionOperator : IOperator  
  7.     {  
  8.         public void ExecuteOperation(int a, int b)  
  9.         {  
  10.             WriteLine($"{nameof(DivisionOperator)} result: {a / b}");  
  11.         }  
  12.     }  
  13. }  
Below is the our product calculator which is going to be used by customers with the help of IOperator contract
  1. using Strategy.Design.Pattern.Contract;  
  2.   
  3. namespace Strategy.Design.Pattern.Business  
  4. {  
  5.     public class Calculator  
  6.     {  
  7.         private IOperator _operator;  
  8.   
  9.         public Calculator(IOperator ioperator)  
  10.         {  
  11.             _operator = ioperator;  
  12.         }  
  13.   
  14.         public void Calculate(int a, int b)  
  15.         {  
  16.             _operator.ExecuteOperation(a, b);  
  17.         }  
  18.     }  
  19. }  
Below is the Student class in which Student uses a calculator to fullfil his/her requirement.
  1. using Strategy.Design.Pattern.Business;  
  2. using static System.Console;  
  3.   
  4. namespace Strategy.Design.Pattern  
  5. {  
  6.     class Student  
  7.     {  
  8.         static void Main(string[] args)  
  9.         {  
  10.             Calculator oprt = new Calculator(new AddOperator());  
  11.             oprt.Calculate(20, 10);  
  12.   
  13.             oprt = new Calculator(new SubtractOperator());  
  14.             oprt.Calculate(20, 10);  
  15.   
  16.             oprt = new Calculator(new MultiplyOperator());  
  17.             oprt.Calculate(20, 10);  
  18.   
  19.             oprt = new Calculator(new DivisionOperator());  
  20.             oprt.Calculate(20, 10);  
  21.   
  22.             ReadLine();  
  23.         }  
  24.     }  
  25. }  
Below is the snap of the output window of the above code:
 
 

Summary

 
In this article we understood about what strategic design pattern is and how to use it.