Chapter 24. The Strategy Pattern

The Strategy pattern is much like the State pattern in outline but a little different in intent. The Strategy pattern consists of a number of related algorithms encapsulated in a driver class called Context. Your client program can select one of these algorithms, or in some cases Context might select the best one for you. The intent is to make these algorithms interchangeable and provide a way to choose the most appropriate one. The State and Strategy differ in that the user generally chooses which of several strategies to apply, and only one strategy at a time is likely to be instantiated and active within the Context class. By contrast, all of the different states could possibly be active at once, and switching ...

Get Java™ Design Patterns: A Tutorial 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.