Design Patterns Explained - The Strategy Pattern

Поділитися
Вставка
  • Опубліковано 23 чер 2024
  • The strategy design pattern can help make code more readable, maintainable when you need to be able to select an algorithm during the run time of a program.
    This is done through the use of polymorphism.
    Firstly I show how code can be bad when not using it, and then show the same code, but written using the pattern in mind.
    =========
    SOURCE CODE: github.com/Hopson97/design-pa...
    =========
    RESOURCES:
    More info on strategy pattern:
    refactoring.guru/design-patte...
    =========
    MUSIC USED:
    Ken Silverman - PREPSONG - • Ken Silverman - PREPSO...
    Pokemon DP - Route 209 - • Video
    Banjo Kazooie - Main Title - • Video
    Banjo Kazooie - Pause Screen - • Video
    Orchard Media - Square Hill (Spyro) - • orchard media - square...
    Tales of Phantasia - The Final Act - • Video
    Ken Silverman - DRIFSNG22 - • Ken Silverman - DRIFSN...
    =========================================
    LINKS:
    GitHub:
    github.com/Hopson97
    Discord Server:
    / discord
    Support me on Patreon:
    / hopson
    Follow me on Twitter:
    / hopson1997
    Follow me on Twitch:
    / hopsonn
    Subscribe to the subreddit:
    / hopsoncommunity
    Community Channel:
    / @hopsoncommunity8184
  • Наука та технологія

КОМЕНТАРІ • 151

  • @Hopsonn
    @Hopsonn  4 роки тому +127

    Hi all! It has been a while and I spent a very time making this video so I hope you enjoy the first episode of this new series :)
    Unfortunately, there were many issues I didn't realise until too late. The main one is the video itself is full of "glitches". This was caused by me using a random mix of 60FPS and 30FPS clips, and also using an external HDD to store the clips.
    It turns out the editing software I used (Camtasia) does not play well with this kind of thing. (and it makes sense, streaming video via USB external HDD into editing software is bound to cause issues, this would have been an issue for other software as well probably).
    I tried to sort as many of these glitches as I could but more popping up :(
    I have learned from these mistakes, so hopefully future videos in this series will be more up to standard.
    Hope you understand and enjoy the video regardless! :)

    • @IANSYT
      @IANSYT 4 роки тому

      i havent had problems with mixing fps in vegas pro, im not sure about video streaming over usb but it shouldnt cause problems

    • @gaborkrisko
      @gaborkrisko 4 роки тому

      Thank you hopson very cool

    • @respawn_101slab2
      @respawn_101slab2 4 роки тому

      Love them anyways

    • @shorakie5068
      @shorakie5068 4 роки тому +2

      the glitches were noticeable but not Annoying, also the video quality in total was Very Good

    • @raskolnikov6347
      @raskolnikov6347 4 роки тому +2

      That was actually a really good explanation, I thought the paint program was a good real-world example. The editing was really good too

  • @TehBrian
    @TehBrian 4 роки тому +13

    Very useful! Thank you so much, I’d love to see this turn into a series where you go over all the main or important design patterns.

  • @AntoNosu
    @AntoNosu 4 роки тому +5

    Awesome video, well explained. Would love for you to do similar stuff with other patterns. Its very easy to understand when you write code without the pattern first, and then change it to use the pattern!

  • @Danidev
    @Danidev 4 роки тому +4

    Nice man, enjoyed this!

  • @dandymcgee
    @dandymcgee 4 роки тому +170

    The explanation of the problem was excellent, but the explanation of the solution felt a bit rushed and made a lot of assumptions about pre-existing knowledge and experience. I think aiming for the 10-minute mark is a sane goal to have for explanation videos, and would give you a bit more time to take a breath and explain things at a speed viewers can more easily comprehend. Personally, I prefer C-style lookup table of function pointers, but it's more or less the same thing as a virtual table in C++. Great idea though, and I'm looking forward to seeing how you iterate on it in future videos.
    Aside: Don't get caught up on micro optimizations like "video glitches". Nobody cares. If you fix them in the background we'll all get the benefits, but there's really no need to publicly apologize or point out these minor errors. It just undermines your work. Focus on big-picture feedback like speed of explanation, quality of content, readability of code snippets, etc.

    • @lukagecko
      @lukagecko 4 роки тому +15

      I disagree with the notion that nobody cares about these "micro optimizations". While the substance and content of any given piece of media takes center stage, the presentation of said content is also important. Bad presentation can diminish or completely negate the effect your piece of media is trying to achieve. To be clear I don't think the glitches in this video are a deal breaker, far from it, but they are distracting especially to people with less understanding of the subject matter (like myself). Furthermore I think a pinned comment acknowledging the mistakes with a promise to do better should be a bare minimum if someone has standards they are striving towards. It also reduces the number of comments pointing out the mistake clogging up the comment section.
      But shit happens and ignoring the glitches the video is quite good. I look forward to seeing the next one hopefully without the glitches, cheers.

    • @Adam-cn5ib
      @Adam-cn5ib 4 роки тому

      @@lukagecko That's an incredible pessimistic point of view. Consider making videos yourself whilst being busy with uni and say that again...

    • @jacobrose9606
      @jacobrose9606 4 роки тому

      That's love right there..

  • @h.hristov
    @h.hristov 4 роки тому +51

    That was interesting. Can you make more videos on design patterns?

    • @Hopsonn
      @Hopsonn  4 роки тому +21

      Yeah that is the plan :)

    • @SrIgort
      @SrIgort 4 роки тому

      @@Hopsonn yes please

  • @khaledmkhawad1
    @khaledmkhawad1 3 роки тому

    I was searching for around an hour for a good explanation I can understand, THANK YOU

  • @trentoncouture1946
    @trentoncouture1946 4 роки тому

    This video is awesome!
    I would definitely be more inclined to keep up to date with your videos and become a patron if this series continues.
    Thanks for the great content!

  • @arthurcheek5634
    @arthurcheek5634 4 роки тому

    more of these please! this is amazing

  • @laurenzfoglia15
    @laurenzfoglia15 4 роки тому

    Awesome video, wow. Well explained and understandable while being on point and entertaining. You just gained a new subscriber :)

  • @tinycloud7738
    @tinycloud7738 4 роки тому

    Very happy to see a new video in this channel

  • @dirtyblasion15
    @dirtyblasion15 4 роки тому +28

    Totally unrelated but your voice changed a lot
    And a video related comment this is how many games manages multiple classes

    • @MenkoDany
      @MenkoDany 4 роки тому +3

      I think he bought a new mic

    • @dirtyblasion15
      @dirtyblasion15 4 роки тому

      @@MenkoDany he aged when I first saw his channel since he was 17

  • @Renegen1
    @Renegen1 2 роки тому

    amazing narration and production.

  • @GurkenbrotLetsPlay
    @GurkenbrotLetsPlay 4 роки тому

    really nice video! Please do more design patterns in the future !!

  • @dawid_dahl
    @dawid_dahl 3 роки тому

    Ouf, nice! I really liked how you first showed how and why stuff is not ideal, and then demonstrated how the pattern solves the problem.
    If you just go straight to the solution and skip the justification for why the solution was invented in the first place, it will never stick in memory, at least not for me. Subbed and hope for more content like this!

  • @paulmaccormick
    @paulmaccormick 4 роки тому

    Please make more videos on design patterns!! You gave a really good explanation on the strategy pattern

  • @leonf.7893
    @leonf.7893 4 роки тому

    Good job. This was really well done and explained.

  • @1sN0s
    @1sN0s 3 роки тому

    Fantastic video. Thanks for sharing!

  • @shorakie5068
    @shorakie5068 4 роки тому +3

    Yay new video.
    Fun & educational !!

  • @gage2560
    @gage2560 4 роки тому

    Finally finished the university, now I can finally hop on the coding train.
    Great video, don't need to apologize about the minor video errors. Keep it up :)

  • @1996Pinocchio
    @1996Pinocchio 4 роки тому

    for some reason I thought you'd talk about game design in video games. I'm happy to see that I was wrong, because this is way more useful .

  • @WiLDDesigns
    @WiLDDesigns 4 роки тому +6

    Great video! I would recommend the gameprogrammingpatterns book by Robert Nystorm, it's full of these awesome patterns and a great resource. Keep it up Hopson :)

  • @sirenti9384
    @sirenti9384 4 роки тому

    I love Videos like that. Waiting for more ;)

  • @LukeSchoen
    @LukeSchoen 4 роки тому

    I looove the n64 callbacks another great vid!

  • @littleodev
    @littleodev 4 роки тому

    This is the video I needed!

  • @NosAltarion
    @NosAltarion 4 роки тому

    Learned that pattern a week ago and only now I found that video?
    I call Bs.
    Thanks for the explanation, it was clear, simple and quick.
    Hope you'll cover some other patterns

  • @james3742
    @james3742 4 роки тому

    Great vid!

  • @monkey22mm1
    @monkey22mm1 4 роки тому +2

    Really good video! Its very interesting how similar the strategy design pattern is to a state machine! Could you make a video exploring the observer design pattern? Or possibly the command pattern? That would work well too if you continue with the paint program because of its use for undo and redo functions!

  • @MrTurbo_
    @MrTurbo_ 4 роки тому

    i made paint in windows batch when i was 13, that was the definition of unmaintainable code, but it worked and it was pretty cool for something made in windows batch

  • @johns66723
    @johns66723 4 роки тому

    Great vid thanks

  • @cheese-pew2542
    @cheese-pew2542 4 роки тому

    Good video!

  • @heinzriemann3213
    @heinzriemann3213 Рік тому

    That was such a good one.
    Disappointed there's only one more in the playlist. 😑

  • @animalibera4326
    @animalibera4326 4 роки тому +1

    Super cool! As always!
    If you want to be cooler, upgrade to C ^^

  • @phrydo9076
    @phrydo9076 4 роки тому +2

    selecting an algorithm at run time can be as simple as having a list of function pointers

    • @fiona9891
      @fiona9891 4 роки тому

      Yeah, but in this case you'd likely have to have two functions for some of the tooltypes, so it's better to contain them in classes.

  • @LiteSketch
    @LiteSketch 4 роки тому

    He's back 🙌

  • @braydenstorms6049
    @braydenstorms6049 4 роки тому +2

    I really like using DavinciResolve for video editing. A little bit of a learning curve, but might be worth a try.

  • @Xgamesvidoes
    @Xgamesvidoes 4 роки тому

    Nice video! :D

  • @foodice11
    @foodice11 4 роки тому +4

    Seems like you could have acceived something of similar organizational quality without oop.

  • @Superik
    @Superik 4 роки тому +2

    he's back

  • @Markd315
    @Markd315 4 роки тому +4

    Good refactoring tip that I'll fall back to when I get a chance to pay off some tech debt. Just as an aside though I can't figure out why you're using c++ for any of this.

  • @risgrynsgrot
    @risgrynsgrot 4 роки тому

    Great video despite the audio glitches :P

  • @celivalg
    @celivalg 4 роки тому

    I will reuse the Wicrosoft Mindows term, I just love it

  • @super4543
    @super4543 4 роки тому +1

    Nice edit

  • @ryuuji159
    @ryuuji159 4 роки тому +6

    good video, it has a hard vibe of bisqwit

  • @helen76262
    @helen76262 4 роки тому +1

    Good ol Wicrosoft Mindows 96.

  • @arnaud7197
    @arnaud7197 4 роки тому +3

    Basically it's a video showing what c++ permitted over C

    • @obiwac
      @obiwac 4 роки тому

      Well you can use this same design pattern in c too

    • @arnaud7197
      @arnaud7197 4 роки тому

      @@obiwac true but polymorphism makes it faster

    • @obiwac
      @obiwac 4 роки тому

      @@chyza2012 yup

  • @hershey3
    @hershey3 4 роки тому

    Very nice video! What Linux Distro are you using? Guess Debian?

    • @Hopsonn
      @Hopsonn  4 роки тому +1

      Thanks! It is Kubuntu

  • @miha53cevic
    @miha53cevic 4 роки тому +1

    What distro are you using?

  • @griffinbrown8182
    @griffinbrown8182 4 роки тому

    Holy shit he's alive

  • @jonthemadscientist
    @jonthemadscientist 4 роки тому +1

    vary useful video!
    but the offset of the preview was distracting LoL

  • @tropicalianopolis
    @tropicalianopolis 4 роки тому

    When i program i normally have your videos as background

  • @csicee
    @csicee 4 роки тому

    What do you use to code and to run and stuff?

  • @Thiago1337
    @Thiago1337 4 роки тому +3

    You misspelled design -> "deisgn" in the description.

    • @Hopsonn
      @Hopsonn  4 роки тому +2

      Thanks! I'll fix it

  • @flirora
    @flirora 4 роки тому

    You could also instantiate all the tools when the application starts instead of creating a new Tool object every time you switch tools, at least in this case.

  • @zetef9646
    @zetef9646 4 роки тому

    getting that bisqwit vibe! hell yeah!

    • @roridev
      @roridev 4 роки тому

      needs the slow talking and 𝓯𝓲𝓷𝓷𝓲𝓼𝓱 𝓪𝓬𝓬𝓮𝓷𝓽

  • @timonpasslick
    @timonpasslick 4 роки тому

    Is there some way to do this without the vtables that come with polymorphism in C++?

  • @noooeels367
    @noooeels367 4 роки тому +1

    THE POKEMON MUSIC

  • @orbyfied
    @orbyfied 4 роки тому +1

    what program are you using to write the code?

  • @chromosoze
    @chromosoze 4 роки тому

    how did you make flood fill tool so fast

  • @vilkillian
    @vilkillian 4 роки тому +1

    So is this pattern just says: *"Hey, you! You have super-powerful coding tools with OOP, like.... INTERFACES, yeah! go ahead and try use them!"*

  • @kshitiz06
    @kshitiz06 3 роки тому

    Kinda confused between factory pattern and strategy pattern. Both instantiate a class from a subset of classes on the fly (based on some parameter).
    What’s the difference?

    • @Hopsonn
      @Hopsonn  3 роки тому

      The strategy pattern is about selecting an algorithm to use during runtime, in this case the different strategies are the paint tools
      Factory pattern is about creating an object. Eg in a game to spawn an emeny you might have a MonsterFactory class to create the different enemy types

    • @kshitiz06
      @kshitiz06 3 роки тому

      @@Hopsonn thank you for such a quick response. I understand that semantically one is about creating a new object and other is about selecting a behavior. But if we leave the semantics aside, both patterns are doing the same things - there is a parent class (interface), bunch of child classes (implementing that interface), and we instantiate one of the child class based on a parameter. Polymorphism allows client to not worry about exactly which class is being instantiate.
      Am I correct? Or am I missing something. Sorry if question is stupid. I am just beginning to learn design patterns.

  • @ryonagana
    @ryonagana 3 роки тому

    i didn't understand a little thing, if every tool change creates a new unique_ptr what happens whern you change the tool, how the last pointer is freed? i know when a unique_ptr ref count is 0 is marked to be garbage collected

  • @zakedake7077
    @zakedake7077 4 роки тому

    Nice music

  • @swordkorn
    @swordkorn 4 роки тому

    Or implement Polymorphism to essentially call one function as far as the program is concerned?
    Also KDE... nice!

    • @Hopsonn
      @Hopsonn  4 роки тому

      Basically what this is

  • @redead-ita
    @redead-ita 4 роки тому

    Wait so dividing the code functionality into it's own library to be the called by the main program is called design strategy?

  • @EliteTester
    @EliteTester 4 роки тому

    I feel like you could use function pointers to do this and avoid OOP but I'm not sure. Am I wrong?
    EDIT: or use something similar in C#

    • @Hopsonn
      @Hopsonn  4 роки тому +1

      i mean, oop is basically an abstraction over function pointers so yeahh you can

  • @bromine35
    @bromine35 3 роки тому

    I love
    Banjo-Kazooie

  • @steinardarri
    @steinardarri 4 роки тому

    Good stuff! The audio quality is unfortunate

  • @jaloveast1k
    @jaloveast1k 4 роки тому +3

    I was like "why you don't just make an interface Tool, extend it for every tool and delegate all the user input to the currentTool object, which is an instance of one of these classes". That's called strategy, alright 😁
    P.S. It's a bit unfair tho, since I already read few books about patterns, but it was loooong ago and I don't really remember their names. This one especially, since it's just composition.
    P.P.S. If anyone is looking for the best book on the subject, it's "Head First Design Patterns by Kathy Sierra, Bert Bates, Elisabeth Robson, Eric Freeman".
    10/10.

  • @benzeltser9851
    @benzeltser9851 2 роки тому

    that video was pure chaos I didnt understand anything

  • @keldwikchaldain9545
    @keldwikchaldain9545 4 роки тому +5

    Honestly I'm not sure why the concept of "interface inheritance" has gotten its own name. It's an incredibly useful tool for abstraction which is in no way unique to object oriented programming.

  • @orocimarosay1447
    @orocimarosay1447 4 роки тому +9

    well yes but this is the reason photoshop is so laggy :)))

    • @eggmeister6641
      @eggmeister6641 4 роки тому +1

      Finally. Someone gets it.

    • @orocimarosay1447
      @orocimarosay1447 4 роки тому +2

      for a program like photoshop the correct aproach would be data oriented design but that's not that easy to achive

    • @eggmeister6641
      @eggmeister6641 4 роки тому

      @@orocimarosay1447 How many people work on the Photoshop team of Adobe? How much money and resources do they have? I'm pretty sure they could manage it.

    • @orocimarosay1447
      @orocimarosay1447 4 роки тому +2

      @@eggmeister6641 I know bigger companies that can't manage this :)))

    • @eggmeister6641
      @eggmeister6641 4 роки тому

      @@orocimarosay1447 Like Microsoft?

  • @Ubilyat
    @Ubilyat 4 роки тому +1

    Noice, I feel dumb now xD

  • @BunnyEZ
    @BunnyEZ 4 роки тому

    I am pretty new to programming and was wondering. How the frick do I know what to type when I want to create a program on c#, even the simplest one.

    • @JavaJack59
      @JavaJack59 4 роки тому

      Console.WriteLine("Hello World"); //not a joke, start small & work your way up

    • @BunnyEZ
      @BunnyEZ 4 роки тому +1

      @@JavaJack59 yeah that one I know lol. But everything else is a mystery for me

    • @JavaJack59
      @JavaJack59 4 роки тому

      @@BunnyEZ #relatable

  • @lemony_dom
    @lemony_dom 4 роки тому

    Is this the same as polymorphism?

    • @Hopsonn
      @Hopsonn  4 роки тому

      Yea it is exactly the same, design patterna are based around oop concepts

  • @xXMurmelkotzeXx
    @xXMurmelkotzeXx 4 роки тому

    you have to update your VS Code :D

  • @MaxPicAxe
    @MaxPicAxe 4 роки тому

    Nice video, but this doesn't seem like a design pattern, but rather having the sense of when to use interfaces/abstract classes and being able to write good code... But looking forward to rest of videos :)

    • @Hopsonn
      @Hopsonn  4 роки тому

      It is a well documented design pattern, it just happens to be a very basic one (Hence I used it for the first episode of the series) :)
      Thanks !

  • @lm1338
    @lm1338 4 роки тому +1

    Isn't this just inheritance

    • @Hopsonn
      @Hopsonn  4 роки тому +1

      Yeh the strategy pattern is very basic inheritance

  • @TheBcoolGuy
    @TheBcoolGuy 4 роки тому +2

    Yeah, this can be hard to keep in mind all at once since there is so much information to account for at once. One downside of OOP. You need to think ahead a lot, and it can make it really hard. That's not an excuse not to do it, of course.

    • @marcossidoruk8033
      @marcossidoruk8033 Рік тому

      It is 100% an excuse to not use it. What you just said perfectly summarises why OOP sucks so bad.
      "premature optimization is the root of all evil" they say, and of course its true, but well premature compromise to an object model is equally as bad. Wanna make your code much more complicated than it needs to? Wanna make it so it is deeply tied to a particular initial approach and is basically unrefactorable? use OOP.

  • @UltimaN3rd
    @UltimaN3rd 4 роки тому +1

    I think the first code style is easier to implement, understand, read and maintain. Personal preference I guess, although the SDP code will be slower.

  • @minhtrinh7440
    @minhtrinh7440 4 роки тому

    This video is pretty well made, but the background music is a bit loud, sometimes it distracts me from your talk

  • @alexwasdreaming9440
    @alexwasdreaming9440 4 роки тому

    You did all that and still forgot to add 32 y to account for the mouse to fix your tools being off

  • @coopercallaghan
    @coopercallaghan 4 роки тому +2

    5:20 69 std

  • @10e999
    @10e999 4 роки тому

  • @origedit
    @origedit 4 роки тому

    Where's the Minecraft clone?

    • @Hopsonn
      @Hopsonn  4 роки тому +1

      That was years ago lol, don't really do that anymore. I was really inexperienced when I made that, but I have plans to recreate it however

  • @MoempfLP
    @MoempfLP 4 роки тому +1

    As a programmer i find the bad example really disgusting.

  • @qwerasdfhjkio
    @qwerasdfhjkio 4 роки тому +1

    69k subs eheh

  • @SimGunther
    @SimGunther 4 роки тому

    The interpreter pattern is the only superior pattern from the Gang of Four design book. Also, all the patterns should be functions like in Haskell.
    _Change my mind_

  • @blunderchips
    @blunderchips 4 роки тому +1

    Liked the video but the music got a little annoying.

  • @roko567
    @roko567 4 роки тому

    What the fuck is up with the sound?

  • @andrewfurey21
    @andrewfurey21 4 роки тому

    please dont use that music

    • @Hopsonn
      @Hopsonn  4 роки тому

      Not sure what "that" music is, but if refering to banjo kazooie it was intentional to give that vibe, as it was used alongside the "don't do this" code

  • @leitheoleu5451
    @leitheoleu5451 3 роки тому

    Someone send this to Yanderedev!

  • @cate01a
    @cate01a 4 роки тому

    I found the explanation very difficult to follow, and using simple graphics/animations should've explained it much better than all this technical stuff

  • @sloosha
    @sloosha 4 роки тому +2

    Bro! You Just Posted Cringe! You are going to loose subscribers.

    • @phroot799
      @phroot799 4 роки тому +4

      How is this cringe? Your childish outburst is the only cringe thing around here...

    • @shorakie5068
      @shorakie5068 4 роки тому

      @@phroot799 Haters gonna hate mate ...

    • @phroot799
      @phroot799 4 роки тому +2

      @@shorakie5068 I guess... ¯\_(ツ)_/¯

    • @sloosha
      @sloosha 4 роки тому

      @@phroot799 ? Just random copypaste to raise video stats? Im not childish and i think reponding to random video comments is more childish itself

    • @phroot799
      @phroot799 4 роки тому

      @@sloosha what do you mean by "random copy-paste to increase video stats? And what's the point of comments if you can't respond to them? Plus I asked a very valid question(what's cringy about the video?) Overall I'm just confused about what you're trying to say. Was your first comment some kind of joke?