Skip to content

Latest commit

 

History

History

008-the-strategy-pattern-in-python

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

Start A New Project In Seconds With iTmpl

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.

Running the Code

All three stages of the code from the video are present in this directory:

  • 1-conditionals - the original state of the code
  • 2-strategy-pattern - the code after applying the strategy pattern
  • 3-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.