A Generic Strategy Pattern implementation using C#

Strategy Pattern - according to this pattern, there will be an interface, few concrete classes which will implement the interface and there will be selection process of the concrete classes. There is good definition in here.

I created a Generic class for Strategy Pattern. Generic Strategy Pattern class will accept the type of Concrete class and types of the parameters and it will take parameter for the argument of the concrete class.

Usage of the StrategyPattern,
 class Program
 {
   static void Main(string[] args)
   {
     int resultOfInt = StrategyPattern.StrategyAddExecutor<intIntProgrammingCalculatorImp>(1, 2);
     string resultString = StrategyPattern.StrategyAddExecutor<stringStringProgrammingCalculatorImp>("1AA2AA""2");
   }
 }
The Generic StrategyPattern class will implement the access of different methods defined in the IProgrammingCalculator interface for instance in here Add and Sub methods. In here T denotes the type of parameter of the methods and S denotes the concrete class type.
 public static class StrategyPattern
 {
    public static T StrategyAddExecutor<T, S>(T firstItem, T secondItem)
        where S : IProgrammingCalculator<T>, new()
   {
        return new S().Add(firstItem, secondItem);
   }
 
   public static T StrategySubExecutor<T, S>(T firstItem, T secondItem)
       where S : IProgrammingCalculator<T>, new()
   {
       return new S().Sub(firstItem, secondItem);
   }
 }
IProgrammingCalculator defines the definition of the calculator. There three different types of calculator IntProgrammingCalculator, LongProgrammingCalculator and StringProgrammingCalculator with its own implementation of the methods defined in the IProgrammingCalculator interface. 
 public interface IProgrammingCalculator<T>
 {
     T Add(T firstItem, T secondItem);
     T Sub(T firstItem, T secondItem);
 }
 
 public class IntProgrammingCalculatorImp : IProgrammingCalculator<int>
 {
     public int Add(int firstItem, int secondItem)
     {
         return firstItem + secondItem;
     }
 
     public int Sub(int firstItem, int secondItem)
     {
         return secondItem - firstItem;
     }
 }
 
 public class LongProgrammingCalculatorImp : IProgrammingCalculator<long>
 {
     public long Add(long firstItem, long secondItem)
     {
         return firstItem + secondItem;
     }
 
     public long Sub(long firstItem, long secondItem)
     {
        return secondItem - firstItem;
     }
 }
 
 public class StringProgrammingCalculatorImp : IProgrammingCalculator<string>
 {
     public string Add(string firstItem, string secondItem)
     {
       return firstItem + secondItem;
     }
 
     public string Sub(string firstItem, string secondItem)
     {
       return firstItem.Replace(secondItem, string.Empty);
     }
 }
There are few good articles in the web for further reading,

dofactory.com
Strategy pattern in the wikipedia

1 comment:

  1. Good Article, but I have one question like in my scenario I have parameterized constructor in my ConcreteStrategy classes (IntProgrammingCalculatorImp, LongProgrammingCalculatorImp ), so how can I handle it in StrategyPattern static class.

    ReplyDelete