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 ...

Get Python Programming with Design Patterns 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.