Book Image

Mastering Python Design Patterns

By : Sakis Kasampalis
Book Image

Mastering Python Design Patterns

By: Sakis Kasampalis

Overview of this book

Table of Contents (23 chapters)
Mastering Python Design Patterns
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Free Chapter
1
The Factory Pattern
Index

Summary


In this chapter, we saw the Strategy design pattern. Strategy is generally used when we want to be able to use multiple solutions for the same problem transparently. There is no perfect algorithm for all input data and all cases, and by using Strategy, we can dynamically decide which algorithm to use in each case. In reality, we use the Strategy pattern when we want to get to an airport to catch a flight.

Python uses the Strategy pattern to let the client code decide how to sort the elements of a data structure. We saw an example of how to sort programming languages based on their TIOBE index ranking.

The use of the Strategy design pattern is not limited to the sorting domain. Encryption, compression, logging, and other domains that deal with resources use Strategy to provide different ways to filter data. Portability is another domain where Strategy is applicable. Simulations are yet another good candidate.

We saw how Python with its first-class functions simplifies the implementation...