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 in to different objects. If the algorithms are all kept in the one class, the class will be become a big messy conditional statements.

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 patter 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, 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

image2.gif

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);
        }
    }

Client

HashingContext context;

context = new HashingContext(new SHA1Hashing());
context.HashPassword("Chinna Sushma");

context = new HashingContext(new SHA384Hashing());
h.HashPassword("Chinna Lohetha");

Up Next
    Ebook Download
    View all
    Learn
    View all