1

In computer programming, the strategy pattern (also known as the policy pattern) is a behavioural software design pattern that enables an algorithm's behavior to be selected at runtime.

The strategy pattern...

  • Defines a family of algorithms.
  • Encapsulates each algorithm.
  • Makes the algorithms interchangeable within that family.

(source: Wikipedia)

In my case, I want to be able to inject different hashing algorithms into a service. C# has several hashing algorithms that derive from HashAlgorithm, such as:

  • MD5
  • SHA256Managed
  • RIPEMD160Managed

Given this hierarchy, this looks like the strategy pattern, but had I never heard of the strategy pattern, I might just say this was a classic example of polymorphism.

Whilst designing the code to solve my particular problem, I designed an interface based on the strategy pattern to inject different hashing algorithms:

public interface IHashStrategy
{
    Hash ComputeHash(byte[] data);
}

Usage

public sealed class HashCreator
{
    public Hash GetHash(IHashStrategy strategy, byte[] data)
    {
        return strategy.ComputeHash(data);
    }
}

Going back to my previous example, I could equally get rid of the interface altogether and just use HashAlgorithm:

public sealed class HashCreator
{
    public Hash GetHash(HashAlgorithm algorithm, byte[] data)
    {
        return new Hash(algorithm.ComputeHash(data));
    }
}

Question 1: Is the strategy pattern different in any way from polymorphism, or is it because of polymorphism that the strategy pattern exists?

Question 2: Which is considered better practice here; abstract out the functionality I require into an interface (IHashStrategy) or use the base type (HashAlgorithm)?

Matthew Layton
  • 39,871
  • 52
  • 185
  • 313
  • in your last code snippet you are using both Polymorphism and strategy pattern. Polymorphism is for `HashAlgorithm` that may be `MD5Algorithm` or `shanonAlgorithm` ... strategy pattern is for `HashCreator` that can work with reference of any of these `HashAlgorithm`s – M.kazem Akhgary Dec 28 '16 at 10:26
  • @M.kazemAkhgary how is `HashCreator` using the strategy pattern? (there wont be any sub-types of `HashCreator` and it does not implement anything) – Matthew Layton Dec 28 '16 at 10:30
  • strategy pattern does not deal with inheritance. that's the point. with strategy pattern you separate classes without making one inherit from another. but be able to use a behavior of other classes with same interface. – M.kazem Akhgary Dec 28 '16 at 10:37
  • hmmm but sorry your last code snippet is using `Dependency injection pattern` because you are injecting `HashAlgorithm` to `HashCreator` from outside. it would be strategy pattern if you chose the algorithm **inside** the `HashCreator` itself – M.kazem Akhgary Dec 28 '16 at 10:39
  • 1
    @M.kazemAkhgary The Strategy pattern does not define that the decision for which strategy to use will be made internally. In fact, in GoF's Design Patterns book, they state that the client is responsible for changing strategies. – o_weisman Dec 28 '16 at 12:30
  • This is not the Strategy pattern as your HashCreator class doesn't store the algorithm as a member and therefore you are not injecting it either. – o_weisman Dec 28 '16 at 12:34
  • @o_weisman omitting the `HashCreator` class (which was added as a use-case example) Do either `HashAlgorithm` or `IHashStrategy` and their implementations follow the strategy pattern, or am I missing something here? – Matthew Layton Dec 28 '16 at 13:08
  • I do not know how HashAlgorithm is implemented so can't say, and as for IHashStrategy, it's only an interface with no implementation that I can see. An example of the Strategy pattern would be for your HashCreator class to have a member of type IHashStrategy (I know it's not the type but I mean declared as). The creator decides when it needs to compute the Hash and uses its IHashStrategy member to do so, but the actual hash algorithm is not contained within the HashCreator class but rather in the IHashStrategy implementing classes. The member can be changed when needed. – o_weisman Dec 28 '16 at 13:22
  • Possible duplicate of [Is 'Strategy Design Pattern' no more than the basic use of polymorphism?](http://stackoverflow.com/questions/37670842/is-strategy-design-pattern-no-more-than-the-basic-use-of-polymorphism) – o_weisman Dec 28 '16 at 13:43

1 Answers1

4

Polymorphism is just a feature of OO-languages, that allows you to have one interface for different types. Strategy is conceptual pattern, that uses polymorphism in OO language, as it can be done with functions in functional programming for example.

As you mentioned,

the strategy pattern (also known as the policy pattern) is a behavioural software design pattern that enables an algorithm's behavior to be selected at runtime

so, it is not only the polymorphism, you can set different strategies for object behavior, you can change the strategy for example, your object can have few strategies, different objects can have the same strategy, objects of one type can have different strategies, there is a point. Polymorphism is just the one way(the best one for OO languages imho), how you could implement it.

Orest Savchak
  • 4,529
  • 1
  • 18
  • 27