If we have lots of different ways of doing the same thing, how do we best represent that in Python? If we want to take a payment, but want to change our payment processor on the fly, how do we write clean, idiomatic Python code without lots of conditional statements everywhere?
Introducing the strategy pattern!
The strategy pattern is a design pattern that allows us to change the behavior of a class at runtime. It's a great way to write code that is flexible, extensible, and easy to maintain.
All three stages of the code from the video are present in this directory:
1-conditionals
- the original state of the code2-strategy-pattern
- the code after applying the strategy pattern3-single-method-interfaces
- the code after applying the single method interface form of the strategy pattern
To run the code, just cd
into the directory of the stage you want to run,
and run python main.py
.