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:
- Strategy is like Template Method except the Strategy pattern consists of details whereas template provides a skeleton for the algorithm.
- State is like Strategy except in its objective.
- Strategy lets us change the internal object. Decorator lets us change the covering, the underlying object remains same.
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

Code
- public abstract class HashingStrategy
- {
- public abstract void HashingInterface(string text);
- }
- public class MD5Hashing : HashingStrategy
- {
- public override void HashingInterface(string text)
- {
- MD5 md5 = new MD5CryptoServiceProvider();
- Byte[] bytes;
- bytes = ASCIIEncoding.Default.GetBytes(text);
- Byte[] encodedBytes;
- encodedBytes = md5.ComputeHash(bytes);
- string A = BitConverter.ToString(encodedBytes);
- }
- }
- public class HashingContext
- {
- private HashingStrategy _hashingStrategy;
- public HashingContext(HashingStrategy hashingStrategy)
- {
- _hashingStrategy = hashingStrategy;
- }
- public void HashPassword(string text)
- {
- _hashingStrategy.HashingInterface(text);
- }
- }
- public class SHA1Hashing : HashingStrategy
- {
- public override void HashingInterface(string text)
- {
- SHA1 sha1 = new SHA1CryptoServiceProvider();
- Byte[] bytes;
- bytes = ASCIIEncoding.Default.GetBytes(text);
- Byte[] encodedBytes;
- encodedBytes = sha1.ComputeHash(bytes);
- string A = BitConverter.ToString(encodedBytes);
- }
- }
- public class SHA256Hashing : HashingStrategy
- {
- public override void HashingInterface(string text)
- {
- SHA256 sha256 = new SHA256CryptoServiceProvider();
- Byte[] bytes;
- bytes = ASCIIEncoding.Default.GetBytes(text);
- Byte[] encodedBytes;
- encodedBytes = sha256.ComputeHash(bytes);
- string A = BitConverter.ToString(encodedBytes);
- }
- }
- public class SHA384Hashing : HashingStrategy
- {
- public override void HashingInterface(string text)
- {
- SHA384 sha384 = new SHA384CryptoServiceProvider();
- Byte[] bytes;
- bytes = ASCIIEncoding.Default.GetBytes(text);
- Byte[] encodedBytes;
- encodedBytes = sha384.ComputeHash(bytes);
- string A = BitConverter.ToString(encodedBytes);
- }
- }
- HashingContext context;
- context = new HashingContext(new SHA1Hashing());
- context.HashPassword("Chinna Sushma");
- context = new HashingContext(new SHA384Hashing());
- h.HashPassword("Chinna Lohetha");

Debasis SahaPosted Jan 20, 2016, 12:19 AM
Nice article..
Bhavik PatelPosted Jan 19, 2016, 9:57 PM
good one
Santhakumar MunuswamyPosted Jan 19, 2016, 1:55 PM
Thanks for nice article
Pradeep SahooPosted Jan 19, 2016, 10:18 AM
nice information, thanks for sharing
Mohammed IbrahimPosted Jan 19, 2016, 8:58 AM
nice
Humayun Kabir MamunPosted Jan 19, 2016, 7:39 AM
Nice...
Ace MaryPosted Jan 19, 2016, 5:29 AM
Hi All, I was reading about the Strategy Design Pattern. And, I observed that whatever we achieve in Strategy Design pattern the same thing we can achieve using Runtime Polymorphism in Java. So, I would like to know the actual difference between the two and how to decide which to choose when? Thanks in advance! http://www.trainingintambaram.in/web-designing-training-in-chennai.html | http://www.traininginsholinganallur.in/web-designing-training-in-chennai.html