The strategy pattern is a widely used design pattern that help you write better Python code. I’ll show you how to apply the strategy using a practical example written in Python. I’ll cover both the classic object-oriented version of the pattern, and I’ll also show you a functional version that’s a bit shorter to write and works equally well. At the end of the video, there’s a more advanced bit where you’ll see how to add type hints for functions as parameters.

All parts in this series:
Part 1: Cohesion and coupling - https://youtu.be/eiDyK_ofPPM
Part 2: Dependency inversion - https://youtu.be/Kv5jhbSkqLE
Part 3: The strategy pattern - https://youtu.be/WQ8bNdxREHU
Part 4: The observer pattern - https://youtu.be/oNalXg67XEE
Part 5: Unit testing and code coverage - https://youtu.be/jmP3fp_BhmE
Part 6: Template method and bridge - https://youtu.be/t0mCrXHsLbI
Part 7: Exception handling - https://youtu.be/ZsvftkbbrR0
Part 7b: Monadic error handling - https://youtu.be/J-HWmoTKhC8
Part 8: Software architecture - https://youtu.be/ihtIcGkTFBU
Part 9: SOLID principles - https://youtu.be/pTB30aXS77U
Part 10: Object creation patterns - https://youtu.be/Rm4JP7JfsKY

Relevant books:

✅ Write cleaner code with Sourcery: https://sourcery.ai/?utm_source=YouTube&utm_medium=aff&utm_campaign=ArjanCodes

My gear:
📷 Sony A7C - https://amzn.to/3aShCtw
🎙 Rode Wireless GO - https://amzn.to/3pSLFVU
💡 GVM RGB LED light - https://amzn.to/3qRNJid
💡 GVM 100W light with lantern softbox - https://amzn.to/2NuU8lk

☕If you want to support this channel, you can buy me a coffee here: https://www.buymeacoffee.com/arjancodes.

You can find the code I worked on in this episode in my GitHub repository: https://github.com/arjancodes/betterpython

Chapters:
0:00 Intro
1:30 Code example
3:21 Main problem with the code
4:01 Switching to a strategy pattern
5:23 Creating concrete strategies
6:56 Updating the process_tickets method
10:09 Adding a new strategy
11:03 A functional version of the strategy
13:18 Adding a function type hint
14:49 Outro

If you enjoyed this content, give this video a like. If you want to watch more of my upcoming videos, consider subscribing to my channel!

DISCLAIMER - The links in this description might be affiliate links. If you purchase a product or service through one of those links, I may receive a small commission. There is no additional charge to you. Thanks for supporting my channel so I can continue to provide you with free content each week!

#python

The Strategy Pattern: Write BETTER PYTHON CODE Part 3
1.25 GEEK