28
The Strategy Pattern
The Strategy pattern is much like the State pattern in outline but is a little different in intent. The Strategy pattern consists of related algorithms encapsulated in a driver class called the Context. Either your client program can select one of these differing algorithms, or, in some cases, the Context might select the best one for you. As with the State pattern, the intent is to switch easily between algorithms without any monolithic conditional statements. The difference between the State and Strategy patterns is 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, as we have seen, it is ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access