The Strategy Pattern

According to the Gang of Four, the Strategy pattern's intent is to

Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from the clients that use it.[6]

[6] ibid, p. 315.

The Strategy pattern is based on a few principles:

  • Objects have responsibilities.

  • Different, specific implementations of these responsibilities are manifested through the use of polymorphism.

  • There is a need to manage several different implementations of what is, conceptually, the same algorithm.

  • It is a good design practice to separate behaviors that occur in the problem domain from each other—that is, to decouple them. This allows me to change the class responsible for one behavior ...

Get Design Patterns Explained: A New Perspective on Object-Oriented Design now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.