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

Chapter 15. The Strategy Pattern

Most problems can be solved in more than one way. Take, for example, the sorting problem, which is related to putting the elements of a list in a specific order. There are many sorting algorithms, and, in general, none of them is considered the best for all cases [j.mp/algocomp]. There are different criteria that help us pick a sorting algorithm on a per-case basis. Some of the things that should be taken into account are:

  • Number of elements that need to be sorted: This is called the input size. Almost all the sorting algorithms behave fairly well when the input size is small, but only a few of them have good performance with a large input size.

  • Best/average/worst time complexity of the algorithm: Time complexity is (roughly) the amount of time the algorithm takes to complete, excluding coefficients and lower order terms. This is often the most usual criterion to pick an algorithm, although it is not always sufficient.

  • Space complexity of the algorithm: Space...