What is the Strategy Pattern? (Software Design Patterns)

preview_player
Показать описание
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:

📚 MY RECOMMENDED READING LIST FOR SOFTWARE DEVELOPERS📚

🎙 MY RECORDING EQUIPMENT 🎙

💻 MY DESKTOP EQUIPMENT 💻

🌎 Find me here:

#DesignPatterns
#StrategyPattern
#SoftwareDesign
#SoftwareEngineer
Рекомендации по теме
Комментарии
Автор

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

magnus
Автор

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!

xiongbenjamin
Автор

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

petroniobonavides
Автор

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

rameshp
Автор

You are the best! Please more Design Pattern content.

stardriver
Автор

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.

luckyluckydog
Автор

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

ngneerin
Автор

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

younghirsch
Автор

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

aerikezedek
Автор

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

lazymacs
Автор

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

teetanrobotics
Автор

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

codingwithgraceandtruth
Автор

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

RaymondBerger
Автор

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

UpTown
Автор

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

youtubewts
Автор

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?

AndheraKayamRahey
Автор

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?

karankhanna
Автор

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

MrToftheL
Автор

What program are you using the stylus with?

justingiovanetti
Автор

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?

ArunKumar-ebuq