Strategy Pattern


  • The Strategy pattern involves removing an algorithm from its host class and putting it in a separate class.
  • The context will often contain a switch statement or a cascading if statement, where information is processed to reach a decision on which Strategy to adopt.

Class diagram: (see Strategy Pattern code review)

Strategy Pattern class diagram of sample code download


Use the Strategy pattern when:

  • Many related classes differ only in their behaviour.
  • There are different algorithms for a given purpose, and the selection criteria can be codified.
  • The algorithm uses data to which the client should not have access.