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 - Наука та технологія
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! :)
i havent had problems with mixing fps in vegas pro, im not sure about video streaming over usb but it shouldnt cause problems
Thank you hopson very cool
Love them anyways
the glitches were noticeable but not Annoying, also the video quality in total was Very Good
That was actually a really good explanation, I thought the paint program was a good real-world example. The editing was really good too
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.
Yeah that would be great!
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!
Nice man, enjoyed this!
What's the strategy on defeating the circle boss?
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.
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.
@@lukagecko That's an incredible pessimistic point of view. Consider making videos yourself whilst being busy with uni and say that again...
That's love right there..
That was interesting. Can you make more videos on design patterns?
Yeah that is the plan :)
@@Hopsonn yes please
I was searching for around an hour for a good explanation I can understand, THANK YOU
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!
more of these please! this is amazing
Awesome video, wow. Well explained and understandable while being on point and entertaining. You just gained a new subscriber :)
Very happy to see a new video in this channel
Totally unrelated but your voice changed a lot
And a video related comment this is how many games manages multiple classes
I think he bought a new mic
@@MenkoDany he aged when I first saw his channel since he was 17
amazing narration and production.
really nice video! Please do more design patterns in the future !!
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!
Please make more videos on design patterns!! You gave a really good explanation on the strategy pattern
Good job. This was really well done and explained.
Fantastic video. Thanks for sharing!
Yay new video.
Fun & educational !!
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 :)
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 .
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 :)
I love Videos like that. Waiting for more ;)
I looove the n64 callbacks another great vid!
This is the video I needed!
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
Great vid!
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!
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
Great vid thanks
Good video!
That was such a good one.
Disappointed there's only one more in the playlist. 😑
Super cool! As always!
If you want to be cooler, upgrade to C ^^
selecting an algorithm at run time can be as simple as having a list of function pointers
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.
He's back 🙌
I really like using DavinciResolve for video editing. A little bit of a learning curve, but might be worth a try.
Nice video! :D
Seems like you could have acceived something of similar organizational quality without oop.
he's back
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.
Great video despite the audio glitches :P
I will reuse the Wicrosoft Mindows term, I just love it
Nice edit
good video, it has a hard vibe of bisqwit
I think it's the music
Good ol Wicrosoft Mindows 96.
Basically it's a video showing what c++ permitted over C
Well you can use this same design pattern in c too
@@obiwac true but polymorphism makes it faster
@@chyza2012 yup
Very nice video! What Linux Distro are you using? Guess Debian?
Thanks! It is Kubuntu
What distro are you using?
Holy shit he's alive
vary useful video!
but the offset of the preview was distracting LoL
When i program i normally have your videos as background
What do you use to code and to run and stuff?
You misspelled design -> "deisgn" in the description.
Thanks! I'll fix it
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.
getting that bisqwit vibe! hell yeah!
needs the slow talking and 𝓯𝓲𝓷𝓷𝓲𝓼𝓱 𝓪𝓬𝓬𝓮𝓷𝓽
Is there some way to do this without the vtables that come with polymorphism in C++?
Array of function pointers.
THE POKEMON MUSIC
what program are you using to write the code?
It's Visual Studio Code
Ok
how did you make flood fill tool so fast
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!"*
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?
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
@@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.
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
Nice music
Or implement Polymorphism to essentially call one function as far as the program is concerned?
Also KDE... nice!
Basically what this is
Wait so dividing the code functionality into it's own library to be the called by the main program is called design strategy?
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#
i mean, oop is basically an abstraction over function pointers so yeahh you can
I love
Banjo-Kazooie
Yes
Good stuff! The audio quality is unfortunate
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.
that video was pure chaos I didnt understand anything
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.
well yes but this is the reason photoshop is so laggy :)))
Finally. Someone gets it.
for a program like photoshop the correct aproach would be data oriented design but that's not that easy to achive
@@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.
@@eggmeister6641 I know bigger companies that can't manage this :)))
@@orocimarosay1447 Like Microsoft?
Noice, I feel dumb now xD
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.
Console.WriteLine("Hello World"); //not a joke, start small & work your way up
@@JavaJack59 yeah that one I know lol. But everything else is a mystery for me
@@BunnyEZ #relatable
Is this the same as polymorphism?
Yea it is exactly the same, design patterna are based around oop concepts
you have to update your VS Code :D
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 :)
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 !
Isn't this just inheritance
Yeh the strategy pattern is very basic inheritance
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.
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.
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.
This video is pretty well made, but the background music is a bit loud, sometimes it distracts me from your talk
You did all that and still forgot to add 32 y to account for the mouse to fix your tools being off
5:20 69 std
Where's the Minecraft clone?
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
As a programmer i find the bad example really disgusting.
69k subs eheh
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_
Liked the video but the music got a little annoying.
What the fuck is up with the sound?
please dont use that music
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
Someone send this to Yanderedev!
I found the explanation very difficult to follow, and using simple graphics/animations should've explained it much better than all this technical stuff
Bro! You Just Posted Cringe! You are going to loose subscribers.
How is this cringe? Your childish outburst is the only cringe thing around here...
@@phroot799 Haters gonna hate mate ...
@@shorakie5068 I guess... ¯\_(ツ)_/¯
@@phroot799 ? Just random copypaste to raise video stats? Im not childish and i think reponding to random video comments is more childish itself
@@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?