What is the Strategy Pattern? (Software Design Patterns)

In this video, learn why the Strategy Pattern is such an important design pattern. I tell you what the strategy pattern is, how it works, and explain it with an example.
Become a Better Developer Using the Courses I Recommend Below:
- Master FAANG Coding Interviews - bit.ly/3CVgRN9
- Learn AWS From Scratch - bit.ly/3gasoAm
- Master Python here - bit.ly/3yJFJpI
Head First Design Patterns - amzn.to/36r8aJT
📚 MY RECOMMENDED READING LIST FOR SOFTWARE DEVELOPERS📚
Clean Code - amzn.to/37T7xdP
Clean Architecture - amzn.to/3sCEGCe
Head First Design Patterns - amzn.to/37WXAMy
Domain Driver Design - amzn.to/3aWSW2W
Code Complete - amzn.to/3ksQDrB
The Pragmatic Programmer - amzn.to/3uH4kaQ
Algorithms - amzn.to/3syvyP5
Working Effectively with Legacy Code - amzn.to/3kvMza7
Refactoring - amzn.to/3r6FQ8U
🎙 MY RECORDING EQUIPMENT 🎙
Shure SM58 Microphone - amzn.to/3r5Hrf9
Behringer UM2 Audio Interface - amzn.to/2MuEllM
XLR Cable - amzn.to/3uGyZFx
Acoustic Sound Absorbing Foam Panels - amzn.to/3ktIrY6
Desk Microphone Mount - amzn.to/3qXMVIO
Logitech C920s Webcam - amzn.to/303zGu9
Fujilm XS10 Camera - amzn.to/3uGa30E
Fujifilm XF 35mm F2 Lens - amzn.to/3rentPe
Neewer 2 Piece Studio Lights - amzn.to/3uyoa8p
💻 MY DESKTOP EQUIPMENT 💻
Dell 34 inch Ultrawide Monitor - amzn.to/2NJwph6
Autonomous ErgoChair 2 - bit.ly/2YzomEm
Autonomous SmartDesk 2 Standing Desk - bit.ly/2YzomEm
MX Master 3 Productivity Mouse - amzn.to/3aYwKVZ
Das Keyboard Prime 13 MX Brown Mechanical- amzn.to/3uH6VBF
Veikk A15 Drawing Tablet - amzn.to/3uBRWsN
Code: github.com/awssimplified/Stra...
Buy Head First Design Patterns: amzn.to/36Y6V71
Buy The Gang of 4's Design Patterns: amzn.to/3iQ5oSD
🌎 Find me here:
Twitter - / beabetterdevv
Instagram - / beabetterdevv
Patreon - Donations help fund additional content - / beabetterdev
#DesignPatterns
#StrategyPattern
#SoftwareDesign
#SoftwareEngineer

Пікірлер: 96

  • @magnus7538
    @magnus75383 жыл бұрын

    I've made some pretty bonkers class hierarchies because I never thought of doing it this way before. Thanks for the insight!

  • @xiongbenjamin
    @xiongbenjamin Жыл бұрын

    you just made OOP better for me to understand now. I use C# and my instructors could not simply this concept at all. Thank you!

  • @younghirsch
    @younghirsch3 жыл бұрын

    Thank you for a simple example! Got my OOP exam coming up and this was really helpful :)

  • @BeABetterDev

    @BeABetterDev

    3 жыл бұрын

    You're very welcome! I wish you the best of luck on your exam!

  • @stardriver8660
    @stardriver86603 жыл бұрын

    You are the best! Please more Design Pattern content.

  • @luckyluckydog123
    @luckyluckydog1232 жыл бұрын

    you explain things very clearly, and I'm grateful for such a high quality video. Thanks! That said, I have some perplexities with the example you're using (maybe because of my limited understanding; please do not take them as criticisms). 1. In my understanding the strategy pattern is not well-suited to the example. If you have to create a "NoFly" class for the RedDuck, then you're not much better off than the initial suggestion of overring the fly() method in the RedDuck with a "do nothing" method. If the RedDuck cannot fly, in an ideal design one shouldn't tell it how to fly at all. Also, every time I instantiate a RedDuck, I have to remember to pass it the "NoFly" object, but nothing prevents me from passing it another flying strategy. Surely design patterns should "design out" possible bugs, but in this example this doesn't seem to be the case to me. Wikipedia reports, on its "strategy pattern" page, a different example which looks much better to me. It's about a restaurant bill, containing a List of orders and prices, and the "strategy" concerns possible discounts applied to the prices. In this case too there is a "NoDiscounts" strategy, but the situation to me is different. There's only one class, RestauntantBill, and naturally all RestauntantBill instances need to know what discounts should be applied ("no discounts today" is a reasonable strategy); on the other hand the "duck" example mixes the strategy pattern with inheritance, IMHO. 2.If your example the performFly() method takes no arguments and just prints out a string. This is not wrong, but in many example of the Strategy pattern the "Strategy" method actually changed the internal state of Duck class they belong to, ie they are of the type performFly(this). This took me some time to get my head around, and I think a full explanation should also comprehend this more complex situation. 3. I personally don't really like these examples with Ducks, Dogs, Animals and so on. I'd much prefer a realistic (or at least semi-realistic) situation. This is because, with the Ducks example, I have to do an extra abstraction effort to remember what the heck I want to do with these essentially meaningless classes and methods. But this may be just me. In any case thanks again for the video. Regardless of whether my observation has a basis or not, just reflecting on these things helps understanding.

  • @simonneubauer2369

    @simonneubauer2369

    2 жыл бұрын

    Very good comment, i think almost the same, especially your 1.point. Creating a Dummy Method feels wrong, but after thinking a while, i think thats not that bad. See it as a Finance Balance Sheet, you getting all Benefits, and If one concrete Claas has a slightly different Behaviors, thats the price you gonna need to pay. At some Point you need to Code the exception , If you otherwise want to Profit from all the benefits. What Brothers me more, is that the concrete Claas has a fly Method, but it will never get called, even worse, it could be missleading for the client. One option my be to make the instance variable optional ? FlyBehavior fb: IFlyBevahior | null. Will this Work or has someone an elegant solution for this ?

  • @2NormalHuman

    @2NormalHuman

    9 ай бұрын

    Yes I think creating a dummy NoFly class is bad because it violates Liskov Substitution principle. Because you are expecting a call to fly method to result in a fly, but this implementation does nothing.

  • @rameshp7822
    @rameshp7822 Жыл бұрын

    Reading the book and coming here, gave me a better understanding of this pattern. Appreciate your teaching..

  • @petroniobonavides3530
    @petroniobonavides35308 ай бұрын

    Unfortunally, I didi not finish all the patterns of the book. You were GREAT!!! Your explanations are AWESOME!!!!

  • @UpTown430
    @UpTown4303 жыл бұрын

    Thank you for this crystal clear explanation, please keep going to upload more patterns 👍

  • @BeABetterDev

    @BeABetterDev

    3 жыл бұрын

    Thanks bahadir! More coming every week :D

  • @mohammeds6314

    @mohammeds6314

    2 жыл бұрын

    ​@@BeABetterDev plz continue, I love way of explaining

  • @youtubewts
    @youtubewts3 жыл бұрын

    It would be great to include some real world examples and other content that's extra to the book.

  • @BeABetterDev

    @BeABetterDev

    3 жыл бұрын

    Thanks for the suggestion Will. As I noted in another comment, I've already recorded part 2 which is on the observer pattern using an example from the book. Going forward though, I will try to use more relatable examples. Thanks for watching!

  • @codingwithgraceandtruth2456
    @codingwithgraceandtruth24562 жыл бұрын

    Very crisp and to the point tutorial and wonderfully explained, learned a lot. Thank you.

  • @BeABetterDev

    @BeABetterDev

    2 жыл бұрын

    Glad it was helpful Arun!

  • @RaymondBerger
    @RaymondBerger3 жыл бұрын

    Thanks is great! excited for the next videos and might even get the book to follow along

  • @BeABetterDev

    @BeABetterDev

    3 жыл бұрын

    Thanks Raymond! I can't say enough positive things about the book. Even though I'm already familiar with all the patterns, I'm learning something new with every chapter. For those just getting started with Design Patterns, I would consider this an essential read. Thanks again for your support!

  • @MrToftheL
    @MrToftheL3 жыл бұрын

    Thanks for the video. I like using a video game example where you have different types of Adventurers who need interchangeable BattleBehavior strategies.

  • @BeABetterDev

    @BeABetterDev

    3 жыл бұрын

    Thanks Consilium! The video game example works well!

  • @lazymacs2823
    @lazymacs28233 жыл бұрын

    Thanks, this is really helpful. Please do more videos about design patterns.

  • @teetanrobotics5363
    @teetanrobotics53632 жыл бұрын

    You're excellent explainer of concepts. Request you to make all 26 design patterns. Thank you

  • @aerikezedek
    @aerikezedek3 жыл бұрын

    awesome presentation, love it, really learnt a lot, more power!

  • @AndheraKayamRahey
    @AndheraKayamRahey3 жыл бұрын

    Nice video. I have a question, if I need to add one more function like colour of the beak then do I need to pass two objects of two interface while creating mallard duck object. Or how should we proceed with it?

  • @ArunKumar-eb2uq
    @ArunKumar-eb2uq3 жыл бұрын

    Hi, thanks for the great toutorial, I just had one question, by including the instance variable in base class is it breaking liskovs substitution principle? Because now every derived class knows about the fly method and it needs a implementation even if it does not know to fly?

  • @blvckchocolvte

    @blvckchocolvte

    2 жыл бұрын

    It is not breaking liskovs substitution because the parent Duck class is the one which implements the FlyBehaviour, in this case the subclasses aren't forced to implement the interface but simply invoke the methods from the Parent class which makes sense to them.

  • @fahmad4921
    @fahmad49213 жыл бұрын

    awesome video, I just purchased my book a little while ago, I know how to code in all basic functions to strings, booleans to OOP, always was stuck on how to code in the pattern etc.. i think this book will help

  • @BeABetterDev

    @BeABetterDev

    3 жыл бұрын

    Thanks Mystical Road Man! I find doing the exercises really helps. It helps drill the concepts into your mind and gives you a sense of muscle memory.

  • @prasadm3614
    @prasadm36143 жыл бұрын

    I was looking for composite design pattern in ur play list .... Can u plz make one ? Btw u r jus super

  • @fahadal2734
    @fahadal27344 ай бұрын

    Thanks for explaining these pattern.

  • @ObsidianJunkie
    @ObsidianJunkie3 жыл бұрын

    Is this not just dependency injection, essentially?

  • @usmonbekravshanov6933
    @usmonbekravshanov6933 Жыл бұрын

    Top-notch explanation. Thanks a lot

  • @ajithkumar-io9kq
    @ajithkumar-io9kq2 жыл бұрын

    Great!!!. Please make a video on facade design pattern

  • @ufukcagataydogan3985
    @ufukcagataydogan39853 жыл бұрын

    Thank u so much, this the best explanation !!!!

  • @BeABetterDev

    @BeABetterDev

    3 жыл бұрын

    You're very welcome!

  • @ngneerin
    @ngneerin3 жыл бұрын

    I really don't understand why nobody gives real world examples

  • @AjayYoutube-rz2so

    @AjayYoutube-rz2so

    3 ай бұрын

    Yeah, ive been badly searching for a good video which explain when to implement this pattern in the real world.

  • @karankhanna2973
    @karankhanna29732 жыл бұрын

    With java8 , default and staic methods can have body and the same can be reused in multiple classes implementing the interface, the issue with fly() method will not arise and we will bypass the complexity and a chance of multiple inhertance(multiplier parent class), Is there still space for strategy pattern?

  • @justingiovanetti
    @justingiovanetti2 ай бұрын

    What program are you using the stylus with?

  • @ApplicableProgramming
    @ApplicableProgramming3 жыл бұрын

    Nice tutorial, as usual. But I am impressed how nice your handwriting is :) I purchased wacom tablet to try something similar, but boy oh boy is my handwriting unusable :D I have to stick to drawing lines and circles for now..

  • @BeABetterDev

    @BeABetterDev

    3 жыл бұрын

    Hah the handwriting on a tablet takes a little practice to get used to - definitely more difficult than I was expecting. And thank you for the kind words!

  • @salahayman3513
    @salahayman35133 жыл бұрын

    when will continue this series enjoy it to the max really

  • @furqankamani2828
    @furqankamani28283 жыл бұрын

    Thanks for the video. Please create more videos for other patterns ...

  • @BeABetterDev

    @BeABetterDev

    3 жыл бұрын

    Coming soon! Thanks for watching.

  • @gunjanmodi2229

    @gunjanmodi2229

    3 жыл бұрын

    @@BeABetterDev Much awaited :)

  • @andrewmayer78
    @andrewmayer783 жыл бұрын

    Wow, you just happened to publish this video the same day that I was looking for just this. Thanks! Looking forward to the rest

  • @ArunaDang
    @ArunaDang3 ай бұрын

    Can you tell the page no from the book? I'm not able to find strategy patters in Head first design pattern book.

  • @KiranKumar-sb3ti
    @KiranKumar-sb3ti2 жыл бұрын

    can we use default methods of interface instead of using strategy patter?

  • @egehurturk6209
    @egehurturk62093 жыл бұрын

    Amazing

  • @BeABetterDev

    @BeABetterDev

    3 жыл бұрын

    Thanks!

  • @prasenjitsaha5322
    @prasenjitsaha53222 жыл бұрын

    Does Collections.sort(Collection, Comparator) use Strategy pattern?

  • @milanamatya8359
    @milanamatya83593 жыл бұрын

    rather than using dependency injection, function to set fly behavior in Duck class would be better I guess. Doing that if we have to add another behavior we just have to add another function to set rather than adding dependency everytime when adding a new behavior.

  • @monikabhasin4922
    @monikabhasin49223 жыл бұрын

    best explanation

  • @BeABetterDev

    @BeABetterDev

    3 жыл бұрын

    Thanks Monika!

  • @alanmangroo3656
    @alanmangroo36562 жыл бұрын

    Am I right in thinking that Java lambdas would be well suited to this pattern?

  • @berkonder7519
    @berkonder75193 жыл бұрын

    I am studying my software design pattern exam with these videos and this videos are very pure and helpfull than my teacher. Thank you bro, keep going

  • @BeABetterDev

    @BeABetterDev

    3 жыл бұрын

    Thank you so much Berk. Comments like these mean a lot to me and motivate me to keep on making more content. Thanks again and good luck on your exam!

  • @tiagosantos680
    @tiagosantos6802 жыл бұрын

    This creates dependencies on constructors, so this should maybe be used together with a Builder

  • @22222222222222223464
    @222222222222222234643 жыл бұрын

    Hi! What do you use to show your whiteboard?

  • @BeABetterDev

    @BeABetterDev

    3 жыл бұрын

    Hi lo, I use photoshop but I used to use SmoothDraw (free). I created a video on how to set it up too if you are interested: kzread.info/dash/bejne/aHqfm9p9oK3Ops4.html&lc=UgzhB5GLwHY-8hGYsxN4AaABAg

  • @22222222222222223464

    @22222222222222223464

    3 жыл бұрын

    @@BeABetterDev Oh, man! You explained everything I wanted to ask! Thank you a lot for being so open to sharing info

  • @justaregulargamer7314
    @justaregulargamer73142 жыл бұрын

    Is this a good book if I'm learning js?

  • @PanKonKeso99
    @PanKonKeso998 ай бұрын

    Bro Amazing turorial but you almost left me blind with that IDE

  • @BeABetterDev

    @BeABetterDev

    8 ай бұрын

    lol! I'm so sorry - I prefer the white :D

  • @Askbowe
    @Askbowe3 жыл бұрын

    What program did you use to draw on the screen ??

  • @BeABetterDev

    @BeABetterDev

    3 жыл бұрын

    Hi Bowe, In this video, I used Photoshop. However in the past, I used a program called Smoothdraw which is free and very similar. I made a video on it here: kzread.info/dash/bejne/aHqfm9p9oK3Ops4.html Hope this helps.

  • @ngneerin
    @ngneerin3 жыл бұрын

    Payment method could be example of it

  • @ManuelMontoyaRdz
    @ManuelMontoyaRdz3 жыл бұрын

    I'm so glad I abandoned OOP six years ago, and I will never going back to that hell.

  • @BeABetterDev

    @BeABetterDev

    3 жыл бұрын

    Beauty is in the eye of the beholder!

  • @DodaGarcia
    @DodaGarcia3 жыл бұрын

    Holy shit I finally understand singletons and dependency injection

  • @BeABetterDev

    @BeABetterDev

    3 жыл бұрын

    Great to hear Doda!

  • @youtubewts
    @youtubewts3 жыл бұрын

    Good job, but are you allowed to reuse a book so closely for your own content? I wouldn't want you to get in to any trouble for this. I understand that it also kind of promotes the book.

  • @BeABetterDev

    @BeABetterDev

    3 жыл бұрын

    This is a good question Will. I'll have to check this out. Thanks for bringing this to my attention.

  • @alexfeng75
    @alexfeng75 Жыл бұрын

    I didn't find how RedDuck is implemented in the code. Other than that, it is clearly explained.

  • @Prashantkumar-pn6qq
    @Prashantkumar-pn6qq3 жыл бұрын

    Ducks fly with wings last time I checked as well😉😂

  • @BeABetterDev

    @BeABetterDev

    3 жыл бұрын

    Hah, good catch :D

  • @jeremyinvictus
    @jeremyinvictus11 ай бұрын

    In this example why not just assign the "flyWithWings" behavior to all Mallard ducks in the constructor for MallardDuck?

  • @cedrickmccrarydeandre9214
    @cedrickmccrarydeandre92143 жыл бұрын

    Cedrick Mccrary the one who structure the strategy to these two components. gang gang

  • @Jebusankel
    @Jebusankel3 жыл бұрын

    I don't like the example. How a duck flies isn't separable from the duck. I would do something like say you're building a robot and it could have wheels that roll or legs that walk and they both implement a move interface. Also the joke you missed would be if one of the ducks flew Delta or some other airline.

  • @hv7443
    @hv74433 жыл бұрын

    Is this what Guice did?

  • @Uncle-gj9in
    @Uncle-gj9in2 жыл бұрын

    I'm just learning but I think your example is not entirely correct. As you did it, every duck has to fly because you are taking the instance FlyBehavior into the constructor of abstract class Duck. What about ducks that do not fly and do not need FlyBehavior? I think a possible solution would be to create the second constructor of the abstract class Duck without instantiating FlyBehavior.

  • @johndee2990
    @johndee29903 ай бұрын

    ​ @evanwest4751 is acts like a massive Baguette

  • @haidernaqvi87
    @haidernaqvi873 жыл бұрын

    Duck walkerDuck = new walkerDuck(what am i supposed to pass here ); ?

  • @Uncle-gj9in

    @Uncle-gj9in

    2 жыл бұрын

    Yes, I think he made a mistake. As he implemented this, every duck h a s to fly. I think he should have used the second constructor without FlyBehavior.

  • @zae0616
    @zae06169 ай бұрын

    "Real life scenario", then the silly duck thing? To me that is not a real life scenario.Real life implies something you might actually use like input or output of data in json, csv, or yaml, or if you wanted to stay academic, calculating grades based various grading schemes. There are probably hundreds of other simple examples. Personally I find examples i can imagine using much more inspiring than abstract caricatures of algorithms.

  • @midicine2114
    @midicine2114 Жыл бұрын

    Isn't this just composition?

  • @wesley2254
    @wesley22542 жыл бұрын

    No fly... breaks skateboard

  • @prezadent1
    @prezadent13 жыл бұрын

    You arrows are backwards and denotes 'has a' instead of 'is a'. Very bad.

  • @BeABetterDev

    @BeABetterDev

    3 жыл бұрын

    Thanks for pointing this out. I've corrected this in my upcoming videos.

  • @konrain7299
    @konrain72993 жыл бұрын

    god, I forgotten how ugly Java was...

  • @BeABetterDev

    @BeABetterDev

    3 жыл бұрын

    beauty is in the eye of the beholder!

  • @nickfleming3719
    @nickfleming371911 ай бұрын

    So... I dont see why the first example of simply implementing a flyable interface is so terrible.

  • @drcl7429
    @drcl7429 Жыл бұрын

    Personally find this book abhorrent.

  • @springbootcourse2906
    @springbootcourse29062 жыл бұрын

    3 minutes of bla bla bla. Please reduce the introduction

  • @dev22221
    @dev222212 жыл бұрын

    PHP code example of strategy pattern: github.com/8ivek/design_patterns/blob/main/03%20Behavioural%20design%20patterns/01%20Strategy%20Design%20Pattern/main.php comments are welcome.