Search code examples
oopdesign-patternsstrategy-pattern

Does the Strategy Pattern violate the Single Responsibility Principle?


If the Single Responsibility Principle states that every object must have a single reason to change and a single strategy class implemented with the Strategy pattern (by definition) has multiple methods that can change for any number of reasons, does that mean that it's impossible to implement the Strategy pattern without violating the SRP?


Solution

  • How so? Strategy pattern if I recollect is basically a way to decouple the logic/algorithm being used. So Client has m_IAlgorithm. IAlgorithm should have a small set of methods if not one.

    So the only reason that a AlgoImplementation class can change is

    • if there is a change in the algorithm it implements. (change in its responsibility/behavior)
    • or if IAlgoritm changes.. which would be rare unless you made a mistake in defining the interface. (Its a change in its own public interface - so don't think its a violation of SRP.)