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,
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. namespace Strategy.Design.Pattern.Business
  4. {
  5. public class AddOperator : IOperator
  6. {
  7. public void ExecuteOperation(int a, int b)
  8. {
  9. WriteLine($"{nameof(AddOperator)} result: {a + b}");
  10. }
  11. }
  12. }
  1. using Strategy.Design.Pattern.Contract;
  2. using static System.Console;
  3. namespace Strategy.Design.Pattern.Business
  4. {
  5. public class SubtractOperator : IOperator
  6. {
  7. public void ExecuteOperation(int a, int b)
  8. {
  9. WriteLine($"{nameof(SubtractOperator)} result: {a - b}");
  10. }
  11. }
  12. }
  1. using Strategy.Design.Pattern.Contract;
  2. using static System.Console;
  3. namespace Strategy.Design.Pattern.Business
  4. {
  5. public class MultiplyOperator : IOperator
  6. {
  7. public void ExecuteOperation(int a, int b)
  8. {
  9. WriteLine($"{nameof(MultiplyOperator)} result: {a * b}");
  10. }
  11. }
  12. }
  1. using Strategy.Design.Pattern.Contract;
  2. using static System.Console;
  3. namespace Strategy.Design.Pattern.Business
  4. {
  5. public class DivisionOperator : IOperator
  6. {
  7. public void ExecuteOperation(int a, int b)
  8. {
  9. WriteLine($"{nameof(DivisionOperator)} result: {a / b}");
  10. }
  11. }
  12. }
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. namespace Strategy.Design.Pattern.Business
  3. {
  4. public class Calculator
  5. {
  6. private IOperator _operator;
  7. public Calculator(IOperator ioperator)
  8. {
  9. _operator = ioperator;
  10. }
  11. public void Calculate(int a, int b)
  12. {
  13. _operator.ExecuteOperation(a, b);
  14. }
  15. }
  16. }
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. namespace Strategy.Design.Pattern
  4. {
  5. class Student
  6. {
  7. static void Main(string[] args)
  8. {
  9. Calculator oprt = new Calculator(new AddOperator());
  10. oprt.Calculate(20, 10);
  11. oprt = new Calculator(new SubtractOperator());
  12. oprt.Calculate(20, 10);
  13. oprt = new Calculator(new MultiplyOperator());
  14. oprt.Calculate(20, 10);
  15. oprt = new Calculator(new DivisionOperator());
  16. oprt.Calculate(20, 10);
  17. ReadLine();
  18. }
  19. }
  20. }
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.