The Strategy Pattern: Write BETTER PYTHON CODE Part 3

Published: 05 February 2021
on channel: ArjanCodes
113,178
4.8k

💡 Learn how to design great software in 7 steps: https://arjan.codes/designguide.

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 -    • Cohesion and Coupling: Write BETTER P...  
Part 2: Dependency inversion -    • Dependency Inversion: Write BETTER PY...  
Part 3: The strategy pattern -    • The Strategy Pattern: Write BETTER PY...  
Part 4: The observer pattern -    • Observer Pattern Tutorial: I NEVER Kn...  
Part 5: Unit testing and code coverage -    • 100% CODE COVERAGE - Think You're Don...  
Part 6: Template method and bridge -    • Two UNDERRATED Design Patterns 💡 Writ...  
Part 7: Exception handling -    • Exception Handling Tips in Python ⚠ W...  
Part 7b: Monadic error handling -    • Monadic Error Handling in Python ⚠ Wr...  
Part 8: Software architecture -    • Why You Should Think About SOFTWARE A...  
Part 9: SOLID principles -    • Uncle Bob’s SOLID Principles Made Eas...  
Part 10: Object creation patterns -    • QUESTIONABLE Object Creation Patterns...  

Relevant books:
Design Patterns: Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides: https://amzn.to/3jllgyH
Principles of Package Design: Creating Reusable Software Components by Matthias Noback: https://amzn.to/2NETK3l
Clean Code: A Handbook of Agile Software Craftsmanship by Robert Martin: https://amzn.to/3qVZgNs
The original Design Principles and Design Patterns article by Robert Martin: https://fi.ort.edu.uy/innovaportal/fi...

🎓 Courses:
The Software Designer Mindset: https://www.arjancodes.com/mindset
The Software Designer Mindset Team Packages: https://www.arjancodes.com/sas
The Software Architect Mindset: Pre-register now! https://www.arjancodes.com/architect
Next Level Python: Become a Python Expert: https://www.arjancodes.com/next-level...
The 30-Day Design Challenge: https://www.arjancodes.com/30ddc

🛒 GEAR & RECOMMENDED BOOKS: https://kit.co/arjancodes.

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

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!