How to Implement the Strategy Design Pattern in Python

preview_player
Показать описание

This video covers the Strategy pattern, but also looks at a few varieties of the pattern using Python features such as Protocol classes, dataclasses, the __call__ dunder method, and closures.

🔖 Chapters:
0:00 Intro
1:23 Explaining the example
3:54 About the Strategy pattern
4:40 #1: Classic OO version
11:58 #2: Using protocols
14:20 #2: Using the __call__ dunder method
16:21 #4: Using functions
22:45 #5: Using closures
26:01 Final thoughts

#arjancodes #softwaredesign #python
Рекомендации по теме
Комментарии
Автор

It's been more than two years working as a Python Developer and nothing excites me more than getting a notification, Arjan Codes has uploaded a new video 🔥

Thanks for the premium quality content.

talhaamir
Автор

I'm glad you're finally deviating from the "write Python like Java" approach. That's my gripe with a lot of the patterns used meticulously "as conceived" - they are often overengineered due to too many classes everywhere and can be made far easier using constructs like closures or dictionary dispatch.

Alche_mist
Автор

Thanks again Arjan, becoming a better developer every time you release a video! If I ever get rich, ill send a couple million your way!

cameronnorman
Автор

I really love the idea of using functions instead of classes if the class is just there to hold a single method and no state!

I also think it further reduces coupling because the function that is calling the strategy doesn't need to know what the method name is or anything. Might even be good to pass the strategy as a parameter to the calling function where you can provide the type alias as type hint as well.

imadetheuniversefun
Автор

You should use dj to remove 2 lines in a row or d#d to remove any # of lines instead of using dd all the time. :)

maxcp
Автор

As a JavaScript fanboy, I always used to like to say "YES! but 'my language' has closures and 'yours' doesn't!".... This is becoming less and less valid all the time. ;)
I love how you keep your examples so minimal - your videos are never too complex to be able to follow and not everyone realises how important that is.

edgeeffect
Автор

I really think that a series on architectures would be amazing..
This is the senior stuff us noobs need to learn 😄

anelm.
Автор

super cool and informative video! thank you for the great effort!

howeichin
Автор

Hey @Arjan ! Thanks a lot for your videos. They're really advanced and easy to follow at the same time. I noticed you make your type hints lower case. I'm importing from `typing` instead. I'd be interested in your reasons. Thanks a lot!

MarkWernsdorfer
Автор

I didn’t get why you need to set the callable before setting the functions 20:04

FabioRBelotto
Автор

As far as I understand Callable with typed arguments is deprecated in favour of Protocol. You could have kept the Protocol class to type hint the functions.

Автор

I'm reading through the comments and 2 things strike me. 1. I love seeing novel ways to solve a problem. 2. But standard ways (e.g. GoF patterns) work well where you have big code bases and you have a high turn over of devs. In the 2nd scenario, theres nothing worse than inheriting someone elses code and it is full of novel solutions.

derekreed
Автор

great work! Could you make a video about typing in Python and cool features that can be used with them?

dmytrokorbanytskyi
Автор

Is there a way to rewrite the last example with a decorator instead of an explicit closure?

ikopysitsky
Автор

I think you should have mentioned that __call__ is only usable/executed after the class has been already instanced.

PetrSUsername
Автор

To be honest all this does for me is confirm my preconception that nobody that thinks about design patterns should ever use them. I've never seen an example where a 'strategy' isn't just effectively just a closure/bound function object, and elevating this to being worthy of a named type just results in inexperienced developers wanting to define classes just so they can name them 'BlahBlahStrategy'.

jamiekydd
visit shbcf.ru