Strategy Design Pattern

Definition

The Strategy pattern involves separating an algorithm from its host class and putting it in a separate class. When there are multiple strategies available for an algorithm, for a given problem it is always better to separate them into different objects. If the algorithms are all kept in the one class, the class will become a big messy conditional statement.

The Strategy pattern enables a client to choose which algorithm to use from a family of algorithms and gives it a simple way to access it.

Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from the clients that use it.

Note:

  1. Strategy is like Template Method except the Strategy pattern consists of details whereas template provides a skeleton for the algorithm.
  2. State is like Strategy except in its objective.
  3. Strategy lets us change the internal object. Decorator lets us change the covering, the underlying object remains same.
Design

Let us say that we are implementing text hashing for the system. During the requirements definition the decision is to implement the MD5 hashing; during development, SHA1 is added for another requirement, and it kept continuing. Finally, the decision is to implement with a couple of options and based on the client strategy it can be changed.

UML Diagram
 
Strategy Design Pattern

Code
  1. public abstract class HashingStrategy  
  2. {  
  3.     public abstract void HashingInterface(string text);  
  4. }  
  5. public class MD5Hashing : HashingStrategy  
  6. {  
  7.     public override void HashingInterface(string text)  
  8.     {  
  9.         MD5 md5 = new MD5CryptoServiceProvider();  
  10.   
  11.         Byte[] bytes;  
  12.         bytes = ASCIIEncoding.Default.GetBytes(text);  
  13.   
  14.         Byte[] encodedBytes;  
  15.         encodedBytes = md5.ComputeHash(bytes);  
  16.   
  17.         string A = BitConverter.ToString(encodedBytes);  
  18.     }  
  19. }  
  20.   
  21. public class HashingContext  
  22.   
  23. {  
  24.     private HashingStrategy _hashingStrategy;  
  25.   
  26.     public HashingContext(HashingStrategy hashingStrategy)  
  27.     {  
  28.         _hashingStrategy = hashingStrategy;  
  29.     }  
  30.   
  31.     public void HashPassword(string text)  
  32.     {  
  33.         _hashingStrategy.HashingInterface(text);  
  34.     }  
  35. }  
  36.   
  37. public class SHA1Hashing : HashingStrategy  
  38. {  
  39.     public override void HashingInterface(string text)  
  40.     {  
  41.         SHA1 sha1 = new SHA1CryptoServiceProvider();  
  42.   
  43.         Byte[] bytes;  
  44.         bytes = ASCIIEncoding.Default.GetBytes(text);  
  45.   
  46.         Byte[] encodedBytes;  
  47.         encodedBytes = sha1.ComputeHash(bytes);  
  48.   
  49.         string A = BitConverter.ToString(encodedBytes);  
  50.     }  
  51. }  
  52.   
  53. public class SHA256Hashing : HashingStrategy  
  54. {  
  55.     public override void HashingInterface(string text)  
  56.     {  
  57.         SHA256 sha256 = new SHA256CryptoServiceProvider();  
  58.   
  59.         Byte[] bytes;  
  60.         bytes = ASCIIEncoding.Default.GetBytes(text);  
  61.   
  62.         Byte[] encodedBytes;  
  63.         encodedBytes = sha256.ComputeHash(bytes);  
  64.   
  65.         string A = BitConverter.ToString(encodedBytes);  
  66.     }  
  67. }  
  68.   
  69. public class SHA384Hashing : HashingStrategy  
  70. {  
  71.     public override void HashingInterface(string text)  
  72.     {  
  73.         SHA384 sha384 = new SHA384CryptoServiceProvider();  
  74.   
  75.         Byte[] bytes;  
  76.         bytes = ASCIIEncoding.Default.GetBytes(text);  
  77.   
  78.         Byte[] encodedBytes;  
  79.         encodedBytes = sha384.ComputeHash(bytes);  
  80.   
  81.         string A = BitConverter.ToString(encodedBytes);  
  82.     }  
  83. }  
Client
  1. HashingContext context;  
  2.   
  3. context = new HashingContext(new SHA1Hashing());  
  4. context.HashPassword("Chinna Sushma");  
  5.   
  6. context = new HashingContext(new SHA384Hashing());  
  7. h.HashPassword("Chinna Lohetha");