The Strategy Pattern: Write BETTER PYTHON CODE Part 3

แชร์
ฝัง
  • เผยแพร่เมื่อ 11 ม.ค. 2025

ความคิดเห็น • 207

  • @ArjanCodes
    @ArjanCodes  3 ปีที่แล้ว +4

    Also checkout this video about how to launch your software product faster: th-cam.com/video/MU20ah5s9ww/w-d-xo.html

    • @manuelderra94
      @manuelderra94 3 ปีที่แล้ว

      What kind of keyboard do you use? It sounds super smooth!

    • @skan90
      @skan90 5 หลายเดือนก่อน

      Thanks for the content over the years, it's helping me a lot.

  • @murghay01
    @murghay01 3 ปีที่แล้ว +145

    Cannot begin to express how much I am loving this series. I just feel like no one ever addresses these concepts as much. It's always algorithms and system design in regards to nailing the interview. I'm trying to craft software! Please keep this series going. I just found you today and expect to finish all of this by tonight.

    • @ArjanCodes
      @ArjanCodes  3 ปีที่แล้ว +5

      Thank you so much Gamal, glad you like them!

    • @cetilly
      @cetilly 3 ปีที่แล้ว +3

      Exactly! This kind of content just isn't getting discussed but yet it is so important. Loving these vids.

  • @TheMako101
    @TheMako101 ปีที่แล้ว +2

    Your free videos are way better than the paid courses I bought, thank you Arjan

    • @ArjanCodes
      @ArjanCodes  ปีที่แล้ว +1

      Thank you for the kind words and support! I really appreciate it :)

  • @ares112302
    @ares112302 3 ปีที่แล้ว +31

    Super underrated channel. You provide some of the most helpful python content on youtube. It seems like nobody else is addressing the concepts that you are, or with the same depth.

    • @ArjanCodes
      @ArjanCodes  3 ปีที่แล้ว

      Thank you Jake, much appreciated!

    • @cetilly
      @cetilly 3 ปีที่แล้ว

      @@ArjanCodes I want to second this comment; Jake is absolutely right. This is really great content that you present exceptionally well. Props!

  • @imadetheuniverse4fun
    @imadetheuniverse4fun 2 ปีที่แล้ว +6

    Thank you so much for not ignoring functional programming in your videos. My eye was twitching the entire first half because of the classes each having only a single method. Was so relieved when you switched them all to functions! :D

  • @dawid_dahl
    @dawid_dahl 3 ปีที่แล้ว +29

    ”The perfect way to deal with customer support.”
    Amen! 😂

  • @mrdbourke
    @mrdbourke 2 ปีที่แล้ว +1

    Wow. Now so many of the Python libraries I use are starting to make even more sense. These videos are sensational. Thank you again!

    • @ArjanCodes
      @ArjanCodes  2 ปีที่แล้ว

      Glad to hear you like the videos, thanks! :)

  • @sandmasterful
    @sandmasterful 3 ปีที่แล้ว +34

    Hey Arjan, I never leave comments. But I just wanted to say you have some QUALITY content. I have no doubt in my mind that your channel will blow up. Keep it up!

    • @ArjanCodes
      @ArjanCodes  3 ปีที่แล้ว +3

      Hi Sandeep, thank you, you’re so kind! I’m happy you are enjoying it and I’ll make sure to keep posting every week - lots of videos are in the pipeline!

  • @chiefxtrc
    @chiefxtrc 3 ปีที่แล้ว +3

    I don't even use python but these are the most effective lectures on design patterns I've watched. I think it's in part because you include just enough code to give the necessary context but not so much as to distract from the actual topic. I wouldn't change anything about your teaching style I think you pretty much nailed it so cheers

  • @felixsponholz6760
    @felixsponholz6760 3 ปีที่แล้ว +12

    I really enjoy how you present and demonstrate those pattern! Thank you!
    Best Part: 10:40 ;)

    • @ArjanCodes
      @ArjanCodes  3 ปีที่แล้ว +1

      Thanks Felix! Customers are a necessary evil ;)

  • @yojansh
    @yojansh 3 ปีที่แล้ว +4

    Not a lot of people go into distinguishing this pattern between OO and functional paradigm. Most just harp on creating objects when most of the modern languages these days support first class function. Thank you for making that distinctions and showing us why latter is simpler.

    • @ArjanCodes
      @ArjanCodes  3 ปีที่แล้ว

      Thanks - glad you enjoyed the video!

  • @mikec64
    @mikec64 3 ปีที่แล้ว +5

    I really appreciated seeing both the more formal solution and the streamlined Python solution, and finally the solution with type hints. Exploring the variations helped me see what was at the heart of the solution. Thank you, this is really leveling up my code.

    • @ArjanCodes
      @ArjanCodes  3 ปีที่แล้ว +1

      Glad it helped, Mike!

  • @felipedeabreuprazeres5690
    @felipedeabreuprazeres5690 2 ปีที่แล้ว

    Arjan, I am in a while True loop here.
    I watch a video. Refactor my code. Watch another video before going to sleep. Wake up and refactor based on what I learned last night. Watch another video before going to sleep. Acknowledge new patterns and think " I should refactor my code tomorrow and implement this".
    Much appreciate your precious content.

  • @LaSculleFCS
    @LaSculleFCS 3 หลายเดือนก่อน

    quite funny i didn't know about this design method and unconsciously implemented in my latest library. Glad to see that it is well documented !

  • @hungryskelly
    @hungryskelly 3 ปีที่แล้ว +1

    The quality of these videos is phenomenal

    • @ArjanCodes
      @ArjanCodes  3 ปีที่แล้ว

      Thank you so much! I’m happy you’re enjoying the videos,

  • @ozbjd
    @ozbjd ปีที่แล้ว

    Very clear presentation. Thanks!

    • @ArjanCodes
      @ArjanCodes  ปีที่แล้ว

      Thank you so much for your support, Bernard!

  • @stalinthomas9850
    @stalinthomas9850 2 ปีที่แล้ว

    Great series, lovin it so far. My only obvious code smell that triggered me, was that you named your argument `list` which is a builtin type in python

  • @macleayy
    @macleayy 3 ปีที่แล้ว

    Your content is 100% making me a better programmer, easy to follow and examples that mimic real systems. I have been able to implement these topics in my day to day work with ease!

  • @beckaddison5827
    @beckaddison5827 3 ปีที่แล้ว +1

    This series is so well done! This is the sort of stuff that I've constantly run into but was never explicitly taught. I really appreciate this your work!

    • @ArjanCodes
      @ArjanCodes  3 ปีที่แล้ว +1

      Thank you - glad you like the videos!

  • @rlclark50
    @rlclark50 3 ปีที่แล้ว +1

    Your videos do such a great job explaining how to actually implement these ideas!

    • @ArjanCodes
      @ArjanCodes  3 ปีที่แล้ว

      Thanks Ryan, glad you like the videos!

  • @Moody0101
    @Moody0101 2 ปีที่แล้ว

    I am so grateful because I found this channel

  • @lucassimon6122
    @lucassimon6122 2 ปีที่แล้ว

    Nice video Arjan! It's awesome you've added the functional approach as well!

  • @KaruseTang
    @KaruseTang 2 ปีที่แล้ว

    This is so cool even I don't really comprehend what you are talking about

  • @PS-dp8yg
    @PS-dp8yg ปีที่แล้ว +1

    I get the whole point of encapsulating the algorithm either into a class o function. However, correct me if I'm wrong, but somewhere in your program, you still have to add some sort of check, whether it is an if or switch statement, to swap out the algorithms. Anyway, this was an awesome content.

  • @braverodger
    @braverodger 3 ปีที่แล้ว +2

    Really enjoying this series so far. Thank you!

    • @ArjanCodes
      @ArjanCodes  3 ปีที่แล้ว +1

      Thank you, Dave! Happy you’re enjoying it.

  • @АлексейБыков-и6ю
    @АлексейБыков-и6ю 3 ปีที่แล้ว

    You are one of the best, if not the best. Thank you so much for such a high-quality, simple and understandable presentation of the material.

    • @ArjanCodes
      @ArjanCodes  3 ปีที่แล้ว

      Thank you so much, glad you like the videos!

  • @astronemir
    @astronemir 3 ปีที่แล้ว

    Yeah this is top notch. Thanks for sharing. I love the simpler functional version still using the strategy principle.

  • @dimalurie
    @dimalurie 3 ปีที่แล้ว

    Just used the functional strategy pattern in a real production code. thank you Arjan!

    • @ArjanCodes
      @ArjanCodes  3 ปีที่แล้ว

      Glad to hear that! You're welcome!

  • @empraticacursos4475
    @empraticacursos4475 3 ปีที่แล้ว

    Cheers from Brazil. Very clear and calm explanations. Subscribed and watching playlists. Thanks a lot.

  • @Baroquepassion
    @Baroquepassion 3 ปีที่แล้ว +3

    thanks for this, clear explanation and nice to see how to implement strategy pattern in Python. I especially like the use of functions, but I do prefer the version where you specify the expected function input and return type as I think it is better to be explicit about what is expected.

    • @ArjanCodes
      @ArjanCodes  3 ปีที่แล้ว +1

      Thank you Matthew, glad you liked it. Indeed, being more specific is better. I’m not much of a fan of the Callable typing though, hopefully Python will introduce a better solution for this at some point.

    • @Baroquepassion
      @Baroquepassion 3 ปีที่แล้ว

      @@ArjanCodes yeah, I'm not that familiar with python but seems a little verbose.

  • @Darthjin93
    @Darthjin93 3 ปีที่แล้ว +1

    Hi Arjan,
    thank you tons, especially the software patterns videos help me out a lot.
    Cheers

    • @ArjanCodes
      @ArjanCodes  3 ปีที่แล้ว

      Thank you, happy you are enjoying the videos!

  • @qwertyuiopsdfgh
    @qwertyuiopsdfgh 3 ปีที่แล้ว

    At first, I felt the use of abstract base class, while certainly elegant OOP style code, was too much. I thought, "this guy's writing Python with a Java mentality. Why doesn't he use functions instead?" And then, you finished it off by using functions instead. I'm sorry to have doubted you! This is very informative. Thank you

  • @abdelghafourfid8216
    @abdelghafourfid8216 2 ปีที่แล้ว

    12:07 Select self in the first function press Ctrl+D multiple times to select it in the next function, then delete them all at once

  • @martink3442
    @martink3442 2 ปีที่แล้ว +2

    Thank you for you great videos, they help me a lot. I just want to point out that naming an argumemt of a method list is not a good idea. By doing this you in fact overwrite the built in function list inside the function which could lead to unexpected behaviour.

  • @anthonyflury3979
    @anthonyflury3979 ปีที่แล้ว +1

    Another way (in Python) of doing your strategy Pattern is to have the orderings as generator functions (using yield), and the for loop inside your process_tickets method uses that generator. This save you make a copy of the list for a start.

  • @qwertyfinger
    @qwertyfinger 3 ปีที่แล้ว

    great video! something i do sometimes with this pattern is have a lookup map of string keys to strategy functions so i can associate a particular strategy with a particular object in a database. an example of this could be an AI strategy for an NPC in an online RPG that picks an attack based on the current state of the battle.

  • @subhampolpagedar203
    @subhampolpagedar203 3 ปีที่แล้ว

    Thanks, @Arjan, This series is no less than any top-class web series on Netflix, I think I am gonna watch it as Binge! Thanks for such quality content!! Keep adding new videos and concepts; whenever you create the JOIN button I will be there!

    • @ArjanCodes
      @ArjanCodes  3 ปีที่แล้ว +1

      Thank you so much! I'm happy you're enjoying the videos!

    • @subhampolpagedar203
      @subhampolpagedar203 3 ปีที่แล้ว

      @@ArjanCodes Merry Christmas! 🥂🎉🎊🌲🎄

  • @ddctechinstitute6861
    @ddctechinstitute6861 2 ปีที่แล้ว

    This was my first thought process on the Example, on seeing the if else statement, I knew something was wrong and following the "O" in SOLID, refactoring to fufil OPEN & CLOSE principle takes you closer to the strategy solution. This simply means that software design concepts are related to each other but we need practice to see them.

  • @nilupulmanodya
    @nilupulmanodya 2 ปีที่แล้ว

    Great and very simple explanation... love this

  • @tyrantus6
    @tyrantus6 2 ปีที่แล้ว

    @ArjanCodes Hi, why in line 6 (9:39) in classSupportTicket, you are defining types before def __init__ , instead of using type hints inside init params?

  • @heyheyjc
    @heyheyjc ปีที่แล้ว

    Man, this channel has spoiled me for the other Python channels. You've set a high bar, Arjun.

    • @ArjanCodes
      @ArjanCodes  ปีที่แล้ว

      Thank you so much for the kind words :)

  • @felipeporto4396
    @felipeporto4396 3 ปีที่แล้ว

    This is absolutely awesome!
    Watching your videos are inspiring!

    • @ArjanCodes
      @ArjanCodes  3 ปีที่แล้ว

      Thanks so much, Felipe, glad you enjoy the videos!

  • @beratbalkl4544
    @beratbalkl4544 3 ปีที่แล้ว

    I am so happy to find this channel.

    • @ArjanCodes
      @ArjanCodes  3 ปีที่แล้ว

      Thank you Berat, happy that you’re enjoying the videos.

  • @jeremiasmedrano1384
    @jeremiasmedrano1384 ปีที่แล้ว

    Thanks from Argentina Arjan!

  • @anupjoseph7368
    @anupjoseph7368 3 ปีที่แล้ว +20

    Hi Arjan, love the videos.
    I was wondering if you could do a similar series on Testing in Python. I couldn't find any good content about the testing processes in Python on TH-cam

    • @ArjanCodes
      @ArjanCodes  3 ปีที่แล้ว +15

      Hi Anup, thank you! And yes, excellent suggestion- this is on my list as it’s been requested by a few others as well.

  • @SamuelTaylorUK
    @SamuelTaylorUK 3 ปีที่แล้ว

    Fantastic channel. Learning so much. Keep up the good work!

    • @ArjanCodes
      @ArjanCodes  3 ปีที่แล้ว

      Great to hear, Samuel!

  • @JonathanScholbach
    @JonathanScholbach 2 ปีที่แล้ว +1

    Nice video. Just two sidenotes: First: the parameter of the functions should not be called `list`, as this is overwriting the builtin type of the same name. Second, but maybe out of the scope of this video: The ordering functions you showed could be typed with a generic `TypeVar` (def fifo_ordering(lst: list[T]) -> list[T]: ...`), as they do not really need to take a list of `SupportTicket` instances, but basically any list.

  • @iliqnew
    @iliqnew 3 ปีที่แล้ว

    your videos are the best. So sorry i didn't find your channel earlier

    • @ArjanCodes
      @ArjanCodes  3 ปีที่แล้ว +1

      Thank you so much, glad you like the content!

  • @kvasios
    @kvasios 3 ปีที่แล้ว

    Οh the quality of this content! Congrats mate.

    • @ArjanCodes
      @ArjanCodes  3 ปีที่แล้ว

      Thank you Konstantinos, glad you like it!

  • @sirajansari2848
    @sirajansari2848 3 หลายเดือนก่อน

    Thanks for making this video.
    One request though, Can you please create a playlist containing all the videos related to Design Patterns in Python ? And maybe another playlist containing videos related to Solid principles in Python ? When I usually search different design patterns in python, I always get a video from your channel. But those videos are scattered, and they can be efficiently grouped into dedicated playlists like "Design Patterns" & "Solid principles".
    This will be very helpful to the viewers.
    Keep up the good work. Thanks !

  • @littleconan7929
    @littleconan7929 2 ปีที่แล้ว

    I have a problem with the docstring (used for typing) and a derived problem.
    I have a base class for some specific treatment (BaseTreatmentClass) and several subclasses that implement precisely the treatment (ATreatmentClass, BTreatmentClass, ...)
    On top of that, I have "monitoring classes" (well not exactly just monitoring, but its easier for the exemple) with the same subclass logic :
    BaseMonitoringClass => AMonitoringClass, BMonitoringClass
    As you may already guess, an instance of AMonitoringClass is designed to use an instance of ATreatmentClass and what it specific to it (for instance methods specific to ATreatmentClass ). Same idea for BMonitoringClass and so on.
    You also can imagine the same family of class with results:
    BaseResultClass => AResultClass, BResultClass
    let's say i have a compute method on my BaseMonitoringClass:
    def compute(self, data):
    """
    :rtype: BaseResultClass
    """
    result = self.__treatment_class.compute(data)
    return result
    The problem is that also it works with the different dedicated treatments, the method is flagged as returning an instance of BaseResultClass which is not wrong... yet not enoug.
    if then for some reason i want to analyse and filter the result within monitoring class, my IDE (PyCharm) will not understand (and as it is written, it is right) and display warning when attempting to use a method of BResultClass even if within a method of BMonitorignClass
    For now I have to rewrite the method in the subclasses and it is VERY tedious especially if it is only for the IDE to understand
    def compute(self, data):
    """
    :rtype: AResultClass
    """
    return super().compute(data)
    Another solution is to use typehints within the code.
    Ex with a method of AMonitoringClass:
    def exemple_method(self, data):
    result = self.compute(data) # type: AResultClass
    result.method_of_a_result_class(....)
    First Pycharm yields another error (basicaly it says compute returns an instance of BaseResultClass not AResultClass)
    But worst (and that is why i prefere typing within docstring rather than in method signatures), you have to import AResultClass for the type hint to work and this may sometime create undesired loop import.
    Is their a way to solve this type hint/docstring problem ?
    - not overloading methods in subclasses
    - not importing the desired result subclass

  • @Christian-xz9yw
    @Christian-xz9yw 3 ปีที่แล้ว +3

    Thanks a lot for these great videos! Just one thought: You suggest using functions instead of classes. For a small example like it is shown here this is great. But when I think about bigger projects and clients who like to change their mind in the middle of a project, classes are much more flexible.
    If you start a project using functions and later on see that one of your functions becomes really complex, you might want to change it into a class. Imo now you have two options:
    1. You could use a __call__ method for your class and treat it like any other function
    2. Change every other strategy function into a class
    What is your opinion and experience on this thought? Or would you switch to any other design pattern when confronted with this kind of problem?

    • @ArjanCodes
      @ArjanCodes  3 ปีที่แล้ว +1

      Hi Christian, thank you! The main idea of the strategy pattern is that it allows you to easily choose a particular *behavior* for a part of your application. This is where functions shine. I always felt like the class-based strategy was a necessity for purely object-oriented languages like Java. Classes, are useful if you also need state (store data), but I believe that goes against the grain of what the strategy pattern is. Overall, I think the best way to approach it is when you design a solution for a client, you try to understand what the main concepts are in the software and how they are related (= the domain model). Then figure out the design that makes the most sense. Change later on will surely be needed, but if you chose the right design patterns, you’ll have a design with low coupling / high cohesion, and as a result, refactoring work should be minimal.

    • @Christian-xz9yw
      @Christian-xz9yw 3 ปีที่แล้ว

      @@ArjanCodes Thanks a lot for your answer, it was exactly what I wanted to know (: I will keep it in mind when I stumble upon the next opportunity I can use the strategy pattern. Keep up the great work!

  • @jovid6410
    @jovid6410 ปีที่แล้ว

    Thank you for all what you do. However the 9:10 moment was hilarious.

  • @pablocodeglia
    @pablocodeglia 2 ปีที่แล้ว

    10:40 "This is the perfect way to deal with customer support" this killed me hahaha 😂

    • @ArjanCodes
      @ArjanCodes  2 ปีที่แล้ว +1

      Thanks Pabio, happy you’re enjoying the content!

  • @bokistotel
    @bokistotel 3 ปีที่แล้ว

    phenomenal course""" Btw could you make a video about your VSCode setup?

  • @urbansan
    @urbansan 3 ปีที่แล้ว +1

    I would use an abtract class with __call__ as the abstract method. Simplifies type hints becuase they're classes but then again are used as functions.

  • @CrushingJosch
    @CrushingJosch 3 ปีที่แล้ว

    Thank you so much for your videos. And thanks for also showing functional examples :)
    Just as a small tip, to avoid the "class is not defined" error that forces you to reorder your script file so that your type-hints don't crash it :D Just put the typehint in '' signs, then it works perfectly in-editor and gets essentially ignored at run-time (as it's then only a string).

    • @ArjanCodes
      @ArjanCodes  3 ปีที่แล้ว

      Thanks for the suggestion - I'm happy you like the videos!

  • @iansparks634
    @iansparks634 2 ปีที่แล้ว

    Great content. I know this video is old but the class-based Strategy approach the self parameter to create_ordering was never used so @staticmethod would be appropriate?

  • @tigerfanxiao
    @tigerfanxiao 3 ปีที่แล้ว

    great video and lecture!

  • @otmaneben351
    @otmaneben351 ปีที่แล้ว

    you were used to smile more than the latest videos :D

    • @ArjanCodes
      @ArjanCodes  ปีที่แล้ว

      Haha, I’m getting grumpy as I get older 😁.

  • @NegriniDaniel
    @NegriniDaniel 3 ปีที่แล้ว

    Thank you for this series

  • @GOTHICforLIFE1
    @GOTHICforLIFE1 ปีที่แล้ว

    Kinda curious about python queues considering that you're using effectively a list to do what queues were designed to do such as python's deque(ue)

  • @RocketLR
    @RocketLR ปีที่แล้ว

    I'm at the point in my career where my knowledge of python is no longer my limiting factor (Very much thanks to you).
    But what I'm struggling with now is structuring bigger projects.
    For example, I am no flask/Django developer but I have to develop a booking system with flask.
    How should I separate the API endpoints, backend functions, and such?
    In smaller projects, I've just kept everything in the app.py file. But now I have to separate some database-relative functions and API intended routes. Not sure how I should structure the directories and separate files.
    Do you have videos covering this?
    While writing this comment i found a post on DigitalOcean that has some good instructions for how to structure a large flask application. I'll give that a read as well.

  • @Victorinoeng
    @Victorinoeng 3 ปีที่แล้ว

    Awesome content! Just found this series out and having a blast! Very well explained and practical material that can be implemented on day to day work!
    Loved the twist of using functional style.
    Would love so see a series about proper/useful FP in Python.
    Thanks for this! Great work and please, keep at it!!!

  • @MicheleHjorleifsson
    @MicheleHjorleifsson 3 ปีที่แล้ว

    does the abstratction and addition of the strategy classes impact performance by using more memory for class storage then the uglier if or case statement method of addressing the strategy?

  • @swehba
    @swehba 3 ปีที่แล้ว +2

    Arjan, how would you characterize the difference between the Dependency Inversion PRINCIPLE and the Strategy PATTERN? It seems like they are one and the same. Are they essential the same except that one (i.e., DI) is a CONCEPT and the other (i.e., Strategy) is an IMPLEMENTATION of the concept?

    • @ArjanCodes
      @ArjanCodes  3 ปีที่แล้ว +2

      The dependency inversion principle tells us that we should depend on abstractions rather than concretions. This idea is applied in lots of design patterns, such as the strategy (where you inject a strategy object via an interface/abstract class), but for example also the factory pattern and the bridge pattern rely on this principle. You can view the principles as the more language/concept-independent ideas that lie at the basis of most of the design patterns.

  • @MrRiVoS
    @MrRiVoS 3 ปีที่แล้ว

    Man, I love your videos! =]

  • @focusifeanyi8560
    @focusifeanyi8560 2 ปีที่แล้ว

    Hello @ArjanCodes, just wondering if there was any reason why the FIFOOrderingStrategy class didn't just return the Support ticket list direct, why the copy of it?

  • @cHrIs16249
    @cHrIs16249 3 ปีที่แล้ว

    Thank you for your videos, they are helping me a lot! Im completely new to OOP and want to code my own software for my PhD Thesis, that will make my work much easier.
    Could you explain what you do when you use ":" and describe something behind. For example at 7:11 you change the string to a class. Does that give the function more information about the input "processing_strategy" or is it just so one can read the code more easily? You sometimes also do that right after you defined a class and just type: variable: type.

    • @Thebiggestgordon
      @Thebiggestgordon 3 ปีที่แล้ว +1

      The way he used : is something called type hinting or annotating.
      This technically doesn’t affect the way your code runs, but it gives any potential users a bit of information on how they should use your functions and classes.
      You can use them at the start of class definitions to show what data the class stores and how it stores it, e.g. at 7:11, or inside function definitions to tell users what sort of data they should pass as parameters. You can also use -> at the end of a function definition to express what type of data the function should return, you can see this at 14:33
      Hope this helps and good luck with phd:)

    • @cHrIs16249
      @cHrIs16249 3 ปีที่แล้ว +1

      @@Thebiggestgordon Thank you, also for your answer! With the name "type hinting" I managed to find the documentation.

  • @markcampanelli
    @markcampanelli 2 ปีที่แล้ว

    Thanks for the lesson 🙂.

    • @ArjanCodes
      @ArjanCodes  2 ปีที่แล้ว

      You’re welcome!

  • @quantux
    @quantux ปีที่แล้ว

    This may be too off topic, is there good way to know when it is better to use iterators vs passing lists around?

  • @Moody0101
    @Moody0101 2 ปีที่แล้ว

    What if the program will get arguments in runtime ? Instead of directly putting the args in code, I think that anyway u would check the passed args by the user ?
    How can I optimize that, is using a match statement better than if/else statements ?

  • @michaelmarinos
    @michaelmarinos 2 ปีที่แล้ว

    Thank you very much for your valuable content. Would it be better (and more pythonic) to return an iterable (yield ticket) instead of copying, processing and returning the whole list, inside the create_ordering function ? What may be the cons of the latter solution? Thanks in advance and keep up the good work

  • @martinuphoffs3
    @martinuphoffs3 2 ปีที่แล้ว

    Great videos! What I don't really get is the use of copy.copy() here to return the original list: Isn't that unnecessary because Python is Call-by-Value and hence calling the copy mechanism in the background anyway? Greets from across the border in Germany

  • @peijunwu7354
    @peijunwu7354 3 ปีที่แล้ว

    The client still needs to handle the if-else statement to choose which strategy to use at runtime based on ticket type / other input values. Any cleaner way for these logic?

  • @MrYyope15555
    @MrYyope15555 3 ปีที่แล้ว

    Thank you so much that was so helpful

    • @ArjanCodes
      @ArjanCodes  3 ปีที่แล้ว

      Thanks - glad it was helpful!

  • @mohameddarwish4910
    @mohameddarwish4910 3 ปีที่แล้ว

    I have a question, why we didn't inherit directly from the strategy class rather than the interface class?

  • @zacky7862
    @zacky7862 3 ปีที่แล้ว

    May I ask, is it really necessary to include Abstract? Because I always see that in your videos.
    I've also watch some videos about abstract but still can't understand what is really the purpose.
    Thank you.

  • @whitmanbohorquez184
    @whitmanbohorquez184 3 ปีที่แล้ว

    How can we differentiate between two patterns like strategy and adapter? i cant sea a clear line between them, Thanks! great content!

  • @EW-mb1ih
    @EW-mb1ih 2 ปีที่แล้ว

    What's the link between the strategy pattern and the dependency inversion principle?

  • @tbtitans21
    @tbtitans21 3 ปีที่แล้ว

    Another great video! This is the important stuff, how to manage scale.
    Unrelated question: What type of jacket is that? What is the square on it?

    • @ArjanCodes
      @ArjanCodes  3 ปีที่แล้ว

      Thank you Steven! I bought that sweater on Zalando I think. The square is a very illogically placed pocket. 😊

  • @StephenRayner
    @StephenRayner 2 ปีที่แล้ว

    Excellent videos 🎉

    • @ArjanCodes
      @ArjanCodes  2 ปีที่แล้ว

      Thanks Stephen, happy you’re enjoying the content!

  • @harish.bhuvanachandar
    @harish.bhuvanachandar ปีที่แล้ว

    Great video!

    • @ArjanCodes
      @ArjanCodes  ปีที่แล้ว

      Thank you! Glad you enjoyed it :)

  • @AndresTalks
    @AndresTalks 3 ปีที่แล้ว

    Great video ! TY so much

    • @ArjanCodes
      @ArjanCodes  3 ปีที่แล้ว

      Thanks Andres, glad you liked it!

  • @mishikookropiridze
    @mishikookropiridze 3 ปีที่แล้ว

    Hey Arjan, why do you check for list length to be zero instead of using built in __bool__ i.e ‘’’’if not my_list’’’ ?

    • @ArjanCodes
      @ArjanCodes  3 ปีที่แล้ว +1

      I've not always applied this consistently throughout my examples, but following the idea of "explicit is better than implicit" I tend to choose to write out the conditions in this way nowadays because it's clearer what it means. You can introduce unexpected bugs if you're not careful with the shorter version because both None and [] are falsy.

  • @SweetSummerChildern
    @SweetSummerChildern 3 ปีที่แล้ว

    Hi! I was wondering how we could use this pattern to handle dynamic conditions. For example, we know here that we want "fifo" for example so we just pass it in. What if we are reading the input from a user, file, or API request? Could you make a video on that? Thanks in advance!

    • @vadimljagatsov8190
      @vadimljagatsov8190 3 ปีที่แล้ว

      If you want to choose behaviour based on API = strings - then making a strategy_lookup dictionary of {string: fn} e.g. {"FIFO": processfifofunction, "filo":....}
      To quickly pull the right function for the job with strategy_lookup.get(your_string)
      You can consider it as an alternative to a set of if statements or a case switch but much more nice to maintain.

  • @fabriciolorenzo2795
    @fabriciolorenzo2795 3 ปีที่แล้ว +1

    The error with "SupportTicket" happend because the class was used as a typehint from a previous class in the code. Beside moving the class you can avoid the error importing "from __future__ import annotations". It will be fixed in python 3.10 and beyond.

    • @ArjanCodes
      @ArjanCodes  3 ปีที่แล้ว +2

      Good to know, thank you!

  • @asdfghjkl36958
    @asdfghjkl36958 3 ปีที่แล้ว

    Great video! Can you please explain the potential downsides of just creating different methods for the processing strategies within the Customer Support class (rather than creating external classes/functions)? So rather than a single process_tickets method, you'd have process_tickets_fifo, process_tickets_lifo, etc. Thanks!

    • @ArjanCodes
      @ArjanCodes  3 ปีที่แล้ว +1

      Thanks! One advantage of using a class instead of a function is that a class allows you to easily store state. So for example, if you wanted to choose parameters when running the strategy, you could pass them to the class initializer, store them as attributes and then use them in the strategy function. With functions, you can do something like this as well, but I find it’s easier to do with classes.

  • @architmaheshwari9106
    @architmaheshwari9106 2 ปีที่แล้ว

    Hey Arjan, thanks for the amazing content. I have one doubt regarding the functional strategy pattern. Doesn't this design pattern contradict the basic approach where we need an abstract interface between low-level methods and high-level methods? Please correct me if I missed something here.

  • @hcubill
    @hcubill ปีที่แล้ว

    Dependency injection seems to be the same as the Strategy Pattern? Is it not?

  • @MrArtod
    @MrArtod 2 ปีที่แล้ว

    What if we just pass a lambda that returns desired ordering instead of the object?

  • @MagnusAnand
    @MagnusAnand 2 ปีที่แล้ว

    ONE QUESTION: This looks like using Dependency Inversion in SOLID.
    Define an interface, implement each case.
    Isn' it?

  • @caxdream
    @caxdream 2 ปีที่แล้ว

    Why the SupportTocket class needs to be above everything? I didn't get it...

  • @RoamingAdhocrat
    @RoamingAdhocrat 3 ปีที่แล้ว

    I was wondering why you'd used ABCs to basically describe the signature of a pure function!

  • @drum-computer
    @drum-computer 3 ปีที่แล้ว

    Hey, thanks a lot for your videos! I have a question: what if I need to have a method that only one certain strategy should implement? The problem is if I put this method to my abstract base class then for every strategy that doesn't use this method I still have to implement it with simple pass, or else I would get an error "Can't instantiate abstract class"

    • @ArjanCodes
      @ArjanCodes  3 ปีที่แล้ว

      You’re welcome! Regarding your question: I would simply add that method only to the subclass that needs it?

    • @drum-computer
      @drum-computer 3 ปีที่แล้ว

      @@ArjanCodes thanks for your answer! My end goal was to be able to iterate over my objects that has a certain strategy in a for loop and don’t care if they have a particular method or not. But after watching more of your videos, I’m thinking that I actually need a bridge pattern rather the strategy. Thx again!

  • @arnoldwolfstein
    @arnoldwolfstein 3 ปีที่แล้ว

    Good to know these possibilities. But are we sure these 2 implementations are better than first one? "explicit is better than implicit"?

    • @ArjanCodes
      @ArjanCodes  3 ปีที่แล้ว +2

      It depends on how many variations you expect. If you don't expect a lot of variations, or you won't need to add any in a later stage, then a strategy pattern is probably overkill.

    • @arnoldwolfstein
      @arnoldwolfstein 3 ปีที่แล้ว

      @@ArjanCodes totally agree, fair enough. Thanks for the great content btw. Your examples(real business logics) far better than usual "todo app" bs examples.

    • @ArjanCodes
      @ArjanCodes  3 ปีที่แล้ว

      Thank you Arnold - I’m happy you like it!

  • @jlimkw
    @jlimkw 3 ปีที่แล้ว

    I am a little confused about the variables used in the class objects. They seem to be both an instance attribute and a class attribute. How should I understand it? Thanks!

    • @ArjanCodes
      @ArjanCodes  3 ปีที่แล้ว +1

      Thanks for your comment! They are supposed to be instance attributes. This is indeed a bit unclear in the video - it has been fixed in the code repository.

  • @vekyll
    @vekyll 3 ปีที่แล้ว +2

    Ah... I was already starting to write one of my 10K tirades, and then I saw "a functional version". Ok, you're spared for now. :-]
    BTW even more Pythonic way to do these things would be to use generators -- since you don't really need to have the output as a laid out list, you just want to specify the iteration order.

    • @ArjanCodes
      @ArjanCodes  3 ปีที่แล้ว +1

      Please be kind, Vedran ;). Thanks for your detailed comments in your email by the way. This is extremely useful and helps me improve the content that I publish here.

    • @vekyll
      @vekyll 3 ปีที่แล้ว

      @@ArjanCodes No problem. I'm always glad when people use my comments to improve instead of arguing they're always right. :-)

  • @janziem3614
    @janziem3614 3 ปีที่แล้ว

    Your video is nice, easy to understand , but IMHO the functional approach using type hints is the only clean code for a strategy pattern like this.
    14:21 the type hint is not "more strict" - it is just a hint. And you'll notice that by type hint of first parameter. You set the first param to a List of List of SupportTicket. If type hints are strict, your compiler should fail. But its a hint (esp. at IDE level), not a preprocessor.

    • @ArjanCodes
      @ArjanCodes  3 ปีที่แล้ว

      I also use the functional version of this most often, but in some cases, have a Strategy class is useful. In particular if you have data specific to certain strategies (think of parameters that are different for different types of algorithms). In that case, it's useful to have a Strategy class and subclasses with attributes specific to those strategies which you can set when you create the strategy instances.

    • @janekschleicher9661
      @janekschleicher9661 2 ปีที่แล้ว

      @@ArjanCodes In principle, you can use also define a function with more arguments,
      e.g. def prioritized_strategy(ticket_list: List[OrderingTicket], vips: List[Customer]): return [ticket for ticket in ticket_list if ticket.creator in vips]
      and then import partial from functools
      and then create e.g. a prioritize_me_strategy = partial(prioritized_strategy, vips=[Customer("Me")])
      and finally run the process_tickets(ticket_list, prioritize_me_strategy).
      In functional programming, binding arguments is very similar to subclassing wit specific attributes (just usually with less side effects, what can be favourable or not, depending on the application).
      The biggest drawbacks of the full functional approach is:
      - it's not so wildly known and can confuse other team members, especially juniors
      - it can be significantly harder to find out what a specific function variable holds for specific attributes (at runtime in the debugger or so, it's similar, but usually you don't have great IDE support or linting support)
      - it's on a higher abstraction level (objects are "easy", functions is always an abstraction higher)
      - creating of a documentation is more difficulty, especially the generated auto api documentation will hide such partial bindings where sub classes will be placed very prominent
      Beside of it, the programming is more flexible and less verbose with the full functional approach. A very prominent framework using it is fastai - but of course this project does not target juniors (for contributing), while python usually targets junior developer or non computer scientists, too.
      So, in practice, especially in corporate or startup environment, using subclasses in python will often be the more transparent way.

  • @selimrbd
    @selimrbd 3 ปีที่แล้ว

    Do you think it is bad practice to put "if" and "for" statements on 1 line if they contain only one instruction ? I've picked up this habit recently but I don't see you doing it in your videos, I was wondering if you were opinionated about this and why. Thanks !

    • @ArjanCodes
      @ArjanCodes  3 ปีที่แล้ว +1

      I'm not particularly against it. My main concern would be readability. I find it easier to read if the condition and the body are written on different lines, so I generally don't do this. I didn't try this but since I use the Black autoformatter nowadays, it might even replace one-line if-statements with multiline ones, since Black is a lot more opinionated than me :).