Strategy Design Pattern

แชร์
ฝัง
  • เผยแพร่เมื่อ 17 ก.ย. 2024

ความคิดเห็น • 1K

  • @derekbanas
    @derekbanas  5 ปีที่แล้ว +29

    Learn in One Videos for Every Programming Language
    Subscribe to Bookmark them: bit.ly/2FWQZTx
    C++ : th-cam.com/video/Rub-JsjMhWY/w-d-xo.html
    Python : th-cam.com/video/N4mEzFDjqtA/w-d-xo.html
    Java : th-cam.com/video/n-xAqcBCws4/w-d-xo.html
    PHP : th-cam.com/video/7TF00hJI78Y/w-d-xo.html
    MySQL : th-cam.com/video/yPu6qV5byu4/w-d-xo.html
    JavaScript : th-cam.com/video/fju9ii8YsGs/w-d-xo.html
    C# : th-cam.com/video/lisiwUZJXqQ/w-d-xo.html
    HTML5 : th-cam.com/video/kDyJN7qQETA/w-d-xo.html
    CSS3 : th-cam.com/video/CUxH_rWSI1k/w-d-xo.html
    JQuery : th-cam.com/video/BWXggB-T1jQ/w-d-xo.html
    TypeScript : th-cam.com/video/-PR_XqW9JJU/w-d-xo.html
    ECMAScript : th-cam.com/video/Jakoi0G8lBg/w-d-xo.html
    Swift : th-cam.com/video/dKaojOZ-az8/w-d-xo.html
    R : th-cam.com/video/s3FozVfd7q4/w-d-xo.html
    Haskell : th-cam.com/video/02_H3LjqMr8/w-d-xo.html
    Handlebars : th-cam.com/video/4HuAnM6b2d8/w-d-xo.html
    Bootstrap : th-cam.com/video/gqOEoUR5RHg/w-d-xo.html
    Rust : th-cam.com/video/U1EFgCNLDB8/w-d-xo.html
    Matlab : th-cam.com/video/NSSTkkKRabI/w-d-xo.html
    Arduino : th-cam.com/video/QO_Jlz1qpDw/w-d-xo.html
    Crystal : th-cam.com/video/DxFP-Wjqtsc/w-d-xo.html
    Emacs : th-cam.com/video/Iagbv974GlQ/w-d-xo.html
    Clojure : th-cam.com/video/ciGyHkDuPAE/w-d-xo.html
    Shell : th-cam.com/video/hwrnmQumtPw/w-d-xo.html
    Perl : th-cam.com/video/WEghIXs8F6c/w-d-xo.html
    Perl6 : th-cam.com/video/l0zPwhgWTgM/w-d-xo.html
    Elixir : th-cam.com/video/pBNOavRoNL0/w-d-xo.html
    D : th-cam.com/video/rwZFTnf9bDU/w-d-xo.html
    Fortran : th-cam.com/video/__2UgFNYgf8/w-d-xo.html
    LaTeX : th-cam.com/video/VhmkLrOjLsw/w-d-xo.html
    F# : th-cam.com/video/c7eNDJN758U/w-d-xo.html
    Kotlin : th-cam.com/video/H_oGi8uuDpA/w-d-xo.html
    Erlang : th-cam.com/video/IEhwc2q1zG4/w-d-xo.html
    Groovy : th-cam.com/video/B98jc8hdu9g/w-d-xo.html
    Scala : th-cam.com/video/DzFt0YkZo8M/w-d-xo.html
    Lua : th-cam.com/video/iMacxZQMPXs/w-d-xo.html
    Ruby : th-cam.com/video/Dji9ALCgfpM/w-d-xo.html
    Go : th-cam.com/video/CF9S4QZuV30/w-d-xo.html
    Objective C : th-cam.com/video/5esQqZIJ83g/w-d-xo.html
    Prolog : th-cam.com/video/SykxWpFwMGs/w-d-xo.html
    LISP : th-cam.com/video/ymSq4wHrqyU/w-d-xo.html
    Express : th-cam.com/video/xDCKcNBFsuI/w-d-xo.html
    Jade : th-cam.com/video/l5AXcXAP4r8/w-d-xo.html
    Sass : th-cam.com/video/wz3kElLbEHE/w-d-xo.html

    • @neostarteon9718
      @neostarteon9718 5 ปีที่แล้ว

      An example of this pattern with Dependency Injection would be great.

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

      Can we get a tutorial on Spring Boot and Kafka?

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

      jesus, what do you not know?

  • @Daedlues
    @Daedlues 8 ปีที่แล้ว +534

    This video is amazing because
    1.you supplied code
    2.you tried a wrong approach first which made me think why using this design pattern is important and when to use it
    3.learning by example and my diagram at one video
    4.your teaching style is clear ( little face paced for a beginner like me )
    thanks very much i really needed to learn patterns

    • @derekbanas
      @derekbanas  8 ปีที่แล้ว +73

      +Daedlues Thank you for the nice compliments :) I'm glad it helped.

    • @topogigio10
      @topogigio10 5 ปีที่แล้ว

      @@derekbanas Sos un fenomeno Derek!

    • @todarfclips
      @todarfclips 5 ปีที่แล้ว +4

      For someone who's not a beginner like me, all those points hold true but his speed is amazing. It's exactly the right pace.

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

      He also has magical powers by making a dog flies, amazing!

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

      For my perspective and point of view. This tutorial is bad because he shows a bad way to code and then does everything on top of it. For a begginer it might be hard to grasp what is where.

  • @alex26toma
    @alex26toma 10 ปีที่แล้ว +59

    I like how you explain the bad practice code before presenting the design pattern. This way we know when to use it.

    • @derekbanas
      @derekbanas  10 ปีที่แล้ว +8

      Alex Toma Thank you :)

  • @derekbanas
    @derekbanas  11 ปีที่แล้ว +10

    You're very welcome :) Yes it takes a while to learn how to properly use patterns. Eventually programming stops being a set of commands and rules and instead becomes an art form in my opinion. With a bit of practice you'll soon start making master pieces

  • @slidenerd
    @slidenerd 9 ปีที่แล้ว +44

    i thought i would come revise my design patterns in java since its been quite some time and guess where I landed :D :D :D fantastic one mate,

    • @derekbanas
      @derekbanas  9 ปีที่แล้ว +8

      slidenerd Thank you. Nice to see you :) Keep making those great videos!

  • @brianlaudrupchannel
    @brianlaudrupchannel 8 ปีที่แล้ว +138

    I realise how bad Uni lectures are. If i finish my Degree its because of TH-cam.

    • @derekbanas
      @derekbanas  8 ปีที่แล้ว +14

      +billyblackburn87 I'm glad I could help :)

    • @rodrigobautista3768
      @rodrigobautista3768 8 ปีที่แล้ว

      I understand you men, I'm studying at ESCOM in México.

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

      @@derekbanas Yeah seriously I'm using your videos right now to study for an exam, my teacher spent a 3 hour course on 3 design patterns but you went over one in 11 minutes and I understood better.

    • @ناصرالثقافه-ق5ز
      @ناصرالثقافه-ق5ز 5 ปีที่แล้ว

      @@todarfclips vf

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

      Have you finished your degree?

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

    it's 2022, Aug 1, I am watching this almost 10 years later . And it makes so much sanse. Thank you a lot for this video man. A true gem indeed.

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

      Thank you :) I’m happy it is still helping people

  • @heradsinn
    @heradsinn 5 ปีที่แล้ว +6

    Great video and great series. Two questions:
    1. Would you care to elaborate about 2:49, "Avoid Interfaces that Just Force Action"?
    2. What is the literature on design patterns in general? Perhaps what GoF published back in the days, or maybe there are newer and better books about the patterns?
    Hopefully you still read comments 7 years later ;)

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

      Nope, he didn't read this comment

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

      1. That means you should not add an interface which merely forces yourself to manually determine what every single animal will need to do with flying capabilities. An interface might be more appropriate for an action which would have a distinct implementation for each class.

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

    One marvellous from our times is that we can simply search for whatever input and quickly find a highly capable professor like you, explaining a fairly simple concept in a much simpler way, in just 11 minutes, with a clear enough voice to be understood at 2x speed. Many others would take a 60 min class, or even more to explain the same. Thanks!

    • @derekbanas
      @derekbanas  4 ปีที่แล้ว

      Thank you for the nice compliment :) I appreciate it!

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

    It is awesome to be able to help people all over the world :) Thank you for telling me you are watching!

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

    I have worked as a software engineer for 8 years and then went back to school & started doing Machine Learning. But when I wanted to revise my old stuff , your videos were the best option. Thanks for the effort and yes! UML tutorial will be great as well.

    • @derekbanas
      @derekbanas  8 ปีที่แล้ว

      +Saptarshi Mitra Thank you for the very nice compliment :) Here is my UML tutorial th-cam.com/video/OkC7HKtiZC0/w-d-xo.html

  • @jotaene764
    @jotaene764 10 ปีที่แล้ว +17

    You have the teaching gift! God Bless you even more than you have been till now! Thank's for the charity with the humanity by giving away all that high quality high valuable knowledge of yours!

  • @derekbanas
    @derekbanas  11 ปีที่แล้ว

    I'm very happy that so many people around the world seem to be able to understand me :) the funny thing is that in my country I was often used to speak with people from foreign countries. I could always understand them as well. I'm very happy to be able to help

  • @RVCata
    @RVCata 10 ปีที่แล้ว +4

    Well...this playlist has helped me better understand design patterns than 2 years of college. You deserve many beers. Many many beers :D.

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

      Thank you very much :) I'm happy that I could help.

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

    You're welcome :) I can't wait to cover C++ and the differences that occur with these patterns. They are very similar as you said, but there are slight differences. The same is true with C#. I'll get to it as soon as possible. i promise to cover C and C++ as completely as I have done with Java

  • @TheAdithya9
    @TheAdithya9 7 ปีที่แล้ว +6

    Extremely helpful. Really well put, well designed and such a good example! At it's most basic level. Good editing too. I'm sure it must be hard to squeeze it all under 15 minutes.

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

      Thank you for the compliments :) I'm glad you liked it

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

    Thank you for the kind words :) You have no idea how much I enjoy doing this. It is great to be able to help people!

  • @1yanivyaron1
    @1yanivyaron1 9 ปีที่แล้ว +24

    Hi Derek,
    Thanks a lot for your channel, it helped me a lot in my job interviewing process.
    I owe you one ;-)

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

    Thank you :) I did my best to cover just about everything in one video. I'm always very appreciative of the kind compliments!

  • @derekbanas
    @derekbanas  12 ปีที่แล้ว +23

    Bog vas blagoslovi, kot dobro. Hvala

    • @vikashverma9
      @vikashverma9 4 ปีที่แล้ว

      Where can I get PPT used in this video?

  • @derekbanas
    @derekbanas  12 ปีที่แล้ว

    It doesn't care that it is an interface. That is why the pattern is so great. The interface just does its job by allowing functionality to change dynamically while not effecting the super class. It may take some time to wrap your head around design patterns, but when you do you'll be able to write great code. It took me awhile as well

  • @jrumiak
    @jrumiak 8 ปีที่แล้ว +10

    At 3:12 you state that super class change shouldn't affect subclasses but later you left flyingType uninitialised forcing every subclass cunstructor to set it's value. Shouldn't you just provide a default?

    • @matthewhartz9235
      @matthewhartz9235 7 ปีที่แล้ว +5

      Yeah, i think this is a bad aspect of the pattern. If you, as a developer, were unaware that you need to initialize this, you won't get a compile time error. Or, if you provided a default, at least in this example, you might have flying dogs by accident in production.

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

      Instead of having variable initialized by default, we must have explicit constructor for initializing all the strategy variables in the class, all such classes should not have default constructor for them , which has variables of interfaces in them.

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

      I think that the Strategy Pattern has less to do with Super vs. Sub-classing and more to do with being able to change/substitute algorithms at runtime. In other words: It allows you to avoid hard-coding one particular algorithm (or implementing several and using conditionals to select between them) at compile-time and, instead, dynamically select the most appropriate at run-time.
      The Strategy Pattern basically lets you implement behavior (ie. an interface), where ever you find it most appropriate, and then it allows you to easily change that behavior. So for instance you can implement an I/O interface, which is initially text-only (all over), and then dynamically change it to a GUI interface; without re-compiling and without duplicating lots of code.

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

    Yes you can often use an Enum in this situation, but it will limit flexibility which is normally what you aim for with most design patterns

  • @StingJia
    @StingJia 9 ปีที่แล้ว +8

    Following the whole tutorial and type each line code manually. If anyone need the the source code, I upload them to:
    github.com/jiapengjun/designPatterns

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

      Sting Jia He has it in his website.

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

      +Sting Jia cool, thanks a lot)

  • @derekbanas
    @derekbanas  11 ปีที่แล้ว

    Thank you very much :) I used to work as a software architect and part of my job was to train new hires and answer their questions. I'm basically doing here what I did with trainees. I'm not a professor

  • @abbos-alimirkhanov114
    @abbos-alimirkhanov114 9 ปีที่แล้ว +3

    Hi Derek Banas! It was a bit difficult for me to understand this line of code 'public Flys flyingType;' on 5:31th minute.
    As I understand it is field variable, and first we have to declare visibility which is PUBLIC then type, which should be some type like integer, String.. or void. After that goes name which is Flys, however in your case it is first visibility which is PUBLIC, then name Flys and then type FLYINGTYPE (I still can't understand what type is it) This line of code is breaking my logic))) However, I have not read about COUPLING which you mentioned in video.

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

      Abbos-Ali Mirkhanov By using composition to store flyingType I'm able to change capabilities at run time. We can then define the flyingType by passing it to setFlyingAbility and then tryToFly automatically executes the proper code without having to define anything at the beginning.

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

      Flys is not name, its type in this case. There is an interface Flys, and the variable flyingType is of this interface's type - Flys. The name of the field variable is flyingType..

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

      Flys is actually the type of the object and FlyingType is the name of the object. You said correctly, that it's a field variable, means there's nothing declared (no memory has been allocated).
      The point of this is,
      to define every class that extends from animal whether it can fly or not.
      Abbos-Ali Mirkhanov you should focus more on your programming understanding before you pay attention to design patterns mate

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

      shouldnt flyingType be private? maybe that's what he meant?

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

    Done thanks
    Todo move notes to onenote
    9:30 UML diagram
    It’s a pattern to favor composition over inheritance.
    Use case: animal superclass, some subclasses fly and some dont.
    Instead of animal superclass having a fly method (where not all subclasses can fly) and subclasses overriding it to fly/not fly, and instead of having a flies interface that some subclasses implement and some dont (both these solutions are bad and duplicate the flying code or the non flying code), we can have a flies interface (but not have animals implement it directly). Instead we have “ability objects” that implement the Fly interface like ItFlys: Fly or CantFly:Fly. And animal superclass has instance variable (composition) of type Fly (the type of the interface) and subclasses set that instance variable to ItFlys or CantFly polymorphically. We can have many different types of flying, classes that implement the Fly interface like ItFlys, CantFly, FlyWings, FlySuperFast etc

  • @ronenfe
    @ronenfe 7 ปีที่แล้ว +4

    I think the better way to implement it, is to create another subclass FlyingAnimal that inherits from Animal and let bird inherit from it instead of from animal.

    • @Alex-ht1oq
      @Alex-ht1oq 7 ปีที่แล้ว

      ronenfe Can't have multiple inheritance in Java. Sorry lol .

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

      ronenfe said "instead of Animal". so it is still single inheritance

    • @paulrybitskyi1737
      @paulrybitskyi1737 7 ปีที่แล้ว +4

      On the Derek's "When to use strategy pattern" slide, there is a point called "When you need to use several behavior dynamically". Dynamically means at run-time. So, while you can create another subclass FlyingAnimal that inherits from Animal and let the bird inherit from it and dog inherit from Animal class, what happens when your dog, by some magical way, obtained wings and now can fly? If we went with your proposed way of doing things, then there is pretty much no way of telling the class that the dog can fly now. If we went with the strategy pattern as in the Derek's video, we could easily change the flying ability by passing another implementation. Although I admit that the example is a bit silly, the pattern definitely finds its uses in modern development.

  • @derekbanas
    @derekbanas  12 ปีที่แล้ว

    Thank you :) I did my best to make it understandable by looking at it from a bunch of angles.I'm glad you liked it

  • @xpopcornx1747
    @xpopcornx1747 8 ปีที่แล้ว +31

    Seems like bad code when Dog is forced to set flying ability, a behaviour it shouldn't have.

    • @vnie1988
      @vnie1988 8 ปีที่แล้ว +4

      +XPopCornX
      I was thinking the same thing. It seems to me that creating a FlyingType class and slotting it in between Bird and Animal in the inheritance chain would be better. Dog would never have to know about it.

    • @popuzin
      @popuzin 8 ปีที่แล้ว +9

      +vnie1988 but then dog can't fly even if it takes jet pack =) or if dog will mutate in some crazy flying doggybird )).

    • @Gam3r979
      @Gam3r979 8 ปีที่แล้ว +5

      +XPopCornX You could easily set the default flyingAbility to CantFlys to avoid this problem

    •  8 ปีที่แล้ว +15

      +XPopCornX I was thinking that maybe the reason it's called strategy pattern is because you're going to define the different strategies in order to implement (achieve) the method.
      If I implement a Flying interface I guess a more understandable example would be that in the noFly strategy, the animal boards a plane.
      So in that way, the animal can achieve or implement the interface, but using a different strategy. And there are many other strategies that could be thought of to implement an interface.
      The real world scenario that I'm thinking about with what I learned here is: databases. There are databases that write to a file (like sqlite), but other databases behave differently. So maybe a strategy pattern would be an interface to store data. We want the same thing (to store data), but the strategies are different.

    • @l_u_c_k_y_7
      @l_u_c_k_y_7 8 ปีที่แล้ว

      hahhahaha

  • @derekbanas
    @derekbanas  12 ปีที่แล้ว

    That topic is both very simple and also very complicated. First off, you couldn't even participate in high frequency trading because those computers receive and act on information in less than 10 milliseconds. They just look for patterns, price discrepancies and trading errors and try to take advantage of them. Ex. If the price of IBM went up 2 cents it more than likely will go up another 1 cent so, buy it at 197.16 and then sell it at 197.17. Stuff like that

  • @BogdanBadulescuBadulescu
    @BogdanBadulescuBadulescu 9 ปีที่แล้ว +9

    it's : Flies ..

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

    I don't know enough to comment on C++/C#, but for PHP they are extremely useful. One of the major reasons that your tutorials are so good are that you obviously go to a lot of trouble thinking/preparing for them beforehand and they are presented very well - which means that the subjects/methodologies you are trying to explain are easily understood. Wish I'd come across your channel earlier!! Thanks again!! :-)

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

    I'm a junior dev, and I picked up a lot of intuition along my programming years. I haven't heard of this pattern before, and I realized that I just wrote a piece of code yesterday that follows this design pattern after not being satisfied with a more traditional polymorphic implementation. Nice.
    Thank you for your work as a TH-cam educator, I really appreciate your videos. Cheers.

  • @nitin.cherian
    @nitin.cherian 11 ปีที่แล้ว

    Hi Derek. One of the best tutorials I have ever seen. I would like to point out the things I liked most about the tutorials.
    1. Your voice. It just glues me to the tutorial.
    2. The tutorial is short, crisp and awesome.
    3. Even your comments and replies here is constructive and clear.
    4. The code is well documented and very understandable.
    Thank you so much. Hats off!!

  • @TheRunner94
    @TheRunner94 11 ปีที่แล้ว

    These are by far the most helpful tutorials on TH-cam for programmers. I am a first year computer science student and I have been spending hours in the library trying fully grasp structures of programs and object oriented programming. Great videos!

  • @derekbanas
    @derekbanas  11 ปีที่แล้ว

    Thank you :) I do my best to make the videos fast and compact. I'm glad you liked it

  • @jnryu661
    @jnryu661 8 ปีที่แล้ว

    Hello, Derek, I am software developer having 3 years experience(Delphi/Oracle Pro*C, PHP(MVC)) and keep failing wtih interviews especially due to lack of understanding about OOP conception and design pattern. I just found your video and it does help me to understand those questions I have asked during interview and I think I will make it better next time.
    Thank you so much !

    • @derekbanas
      @derekbanas  8 ปีที่แล้ว

      I'm very happy that I could help :) I wish you all the best in your future interviews!

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

    Derek,
    Theses tutorials are life savers. I'm working for a company in a java boot camp accelerated program in which all of my classmates have previous programming exp. except me. To say I'm drowning in information overload would be a understatement. These tut's have helped immensely. God speed!

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

      Thank you very much :) i'm very happy to hear that they are helping.

  • @derekbanas
    @derekbanas  11 ปีที่แล้ว

    Yes that is exactly it. Also if we can't think of something as a real object then it shouldn't be instantiated.

  • @derekbanas
    @derekbanas  11 ปีที่แล้ว

    Thank you :) Yes, I tend to make videos for intermediate programmers. Most everyone else aims at beginners so I like to be different.

  • @derekbanas
    @derekbanas  11 ปีที่แล้ว

    It is very nice for me to be able to reach people all over the world! I'm very happy that you're enjoying the videos :)

  • @daywattly
    @daywattly 6 ปีที่แล้ว

    I spent days looking at different videos and examples of strategy design patterns that never explained or touched base with the instance variable of the interface. There was so many levels of confusion going on. Like how this abstract could extend multiple sub-classes of the interface being you can only extend one class. And why the need for the interface if I was just extending another class. This was the best explanation that I came across by far. Thank you.

    • @derekbanas
      @derekbanas  6 ปีที่แล้ว

      Thank you very much :) I'm happy the video learned up a complex topic

  • @derekbanas
    @derekbanas  11 ปีที่แล้ว

    Thank you very much :) I constantly do my best to improve them. I'm very happy that you enjoy them

  • @derekbanas
    @derekbanas  11 ปีที่แล้ว

    You're very welcome :) I did my best to make the gof book make more sense

  • @derekbanas
    @derekbanas  11 ปีที่แล้ว

    Yes you are correct. I wish I could have come up with a better example, but I wanted to keep everything as simple as possible

  • @smithlo4092
    @smithlo4092 5 ปีที่แล้ว

    Thanks Derek for such a great video.
    The first time I listen this video. I think I understand something. But when I try to watch it again. I feel I get lost. Here is my understand (which might incorrect)
    - Strategy Patterns can use when we have objects shared behavior and we want to avoid code duplication.
    - Instead of put that behavior as a attribute or method in SuperClass (which is bad because subclass have to override those method and this cause code duplication.) So, we use an interface.
    However using an interface does not mean just to move the method over there (which cause another problem -- each subclass will implement that interface and cause code dependent. (I do not sure I understand this point correctly or not.)
    So, Derek suggests that we use interface in a new approach:
    - Create an interface with behavior as a method.
    (Flys interface)
    - Then, create classes for each of those shared behavior and implemented method from behavior interface.
    (Itflys and Cantfly)
    - next, the SuperClass (Animal) have a new_field and use behavior interface as a data type
    - after that, the SuperClass (Animal) need to create a method to access to interface method. -- tryToFly() which return flyingType.fly()
    - and at object subclass construction -- Derek do interface polymorphically.
    -- flyingType = new CantFly(); //
    - at run-time, the instance of subclass (Dog) can access to behavior by using method in SuperClass -- trytoFly()
    which will call fly() method from the behavior class (Cantfly in this case)
    The benefit of Strategy Method is:
    a) I can add behavior which unaffected to Object SuperClass,
    b) As long as the method name in behavior implemented class not change, modification to the algorithm inside method won't cause any issue to the reference subclass.
    It would be grateful If someone could confirm my understanding or correct me what I misunderstand. Thank you.

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

    Thank you very much :) In the refactoring tutorial I spend a good amount of time answering design pattern challenge questions. That was a lot of fun

  • @sambitkabi4617
    @sambitkabi4617 7 ปีที่แล้ว

    The tutorial is great.. Creating an instance of a Fly directly inside the constructor using new keyword leads to more coupling. Instead we can pass it as a parameter in the constructor to use polymorphism

  • @derekbanas
    @derekbanas  11 ปีที่แล้ว

    Yes it is halfway between a regular class and an interface

  • @derekbanas
    @derekbanas  11 ปีที่แล้ว

    Thank you for telling me you like them and that they help PHP programmers. I always wondered how well they would translate for languages other then java, C# and C++. Im very happy that they've helped

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

    Its simple straight and clear. I have just listen only a few of them. I am in progress of listening more of deisgn principles session. Will recommend everyone who wants to learn design pattern... Thanks Derek

    • @derekbanas
      @derekbanas  10 ปีที่แล้ว

      Thank you for the kind message :)

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

    Thanks this is the first video that made “favor composition over inheritance” click for me. I’ve always heard that phrase but now I see how it’s actually used.

  • @SpiritsofCreativity
    @SpiritsofCreativity 11 ปีที่แล้ว

    Wow, I had totally skipped these tutorials for some reason when your first created them,. Now I just watch the first design pattern and I am just completly amazed! (why did I skip these videos!!)

  • @derekbanas
    @derekbanas  11 ปีที่แล้ว

    Yes a java enum is often well suited for that.

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

    Thanks for your great post Derek.
    I really enjoyed your direct example of the Strategy Pattern emphasizing how you have decoupled any dependence from previous videos. Comments detracting from the ultimate goal of discussing the pattern in general, are no doubt valuable in specific business cases but the attention to detail about what is important to the pattern is what I find most valuable in this videos.
    For those who already appreciate the existence of individual business constraints within a given implementation, you're intentional quality of skipping particular details about , for example, how private or final a method should or should not be be is something in which, I ,for one, find much value.
    Your willingness to politely discuss via comments with others how your vagueness on certain points might be more invaluably clarified in particular situations is a cherry on top.
    Please hit me up if you want to discuss cleaning up your content a bit and syndicating for a good cause. I'm willing to pith in on other people's ventures and I am always looking for good coders to pitch in on mine.
    Thanks again,
    Anthony Ruffino
    aruffino84@gmail.com
    815-301-7606

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

      You definitely understand my way of thinking in these videos. I always thought it was better to avoid covering the details at times if it gets in the way of the topic at hand. I'm glad you see that because most people don't.
      I provide everything for free, so if you want to embed the videos, or use the code from my site in any way feel free to do that.
      I do all of this purely to help people in any way that I can.

  • @derekbanas
    @derekbanas  11 ปีที่แล้ว

    You're very welcome :) Yes you can create an instance directly in the methods attribute. It is a little odd I know

  • @derekbanas
    @derekbanas  11 ปีที่แล้ว

    Hi Vishal, Thank you :) I'm glad you found it useful. I record with Camtasia 2 and I edit with iMovie.

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

    Got to thank you for these series. I failed my OOP subject last year (studying Computer Engineering) and this is being a great help. Studying while on my vacations. Thanks alot man.

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

      TigasFMS You're are very welcome :) I wish you all the best in your schooling.

  • @derekbanas
    @derekbanas  12 ปีที่แล้ว

    I've been thinking about covering Extreme / Agile programming techniques. I'm just not sure if there is any audience for that topic? I don't particularly care about views, but I don't want to alienate every viewer. I'll probably do a overview video on the subject and see how people like it. I also want to cover Discrete mathematics because the Khan academy never covered it and it works perfectly with understanding programming.

  • @VIGNESHGOPAL
    @VIGNESHGOPAL 11 ปีที่แล้ว

    Hey Derek, I would like to thank you to the core. I haven't seen any video tuts on youtube with code. So helpful. Really exiting after seeing your tuts. Thanks alot.!!!

  • @derekbanas
    @derekbanas  11 ปีที่แล้ว

    Sorry I go overboard every once in a while. Over the years you guys out there have taught me to always be on my toes and to not just flippantly provide answers to questions :)
    I'll see what I can do about covering the different unix shell versions and commands. I'd like to do it in an interesting way, which I can't think of at the moment

  • @maurobilotti
    @maurobilotti 11 ปีที่แล้ว

    Derek, THANK YOU VERY MUCH!!... you have made an incredible job. The most important fact in my case, is your clarity to talk, i'm from argentina and as you know, this part of the world talks in spanish. I think that i have quite good english, but i saw a lot of tutorials where you barely understand what they are saying... however, your tutorials are excellent in this point. Congrats to you! Keep doing this job... all the developers in whole world are grateful with you.

  • @derekbanas
    @derekbanas  11 ปีที่แล้ว

    You're very welcome :) If it helps make your code more understandable then do it. There is a misconception that there is only one way to use each pattern. Even the GOF book states that it is but a guide and the patterns should be adjusted based on need. I hope that helps

  • @chiligarden
    @chiligarden 8 ปีที่แล้ว

    This is the easiest tutorial about design pattern that I have viewed. I can't wait to finish viewing/learning from your tutorials.

    • @derekbanas
      @derekbanas  8 ปีที่แล้ว

      +chiligarden Great I'm happy that it helped :)

  • @nicolasap86
    @nicolasap86 10 ปีที่แล้ว

    I wanted to leave a quick note before I forget. Thank you so much Derek. Right now I'm in a Software Design class in which we have to learn these really tricky patterns. You do an absolute amazing job at explaining concepts(literally the best tutorials I've ever seen on the web for programming) I hope you continue to do this work and that you'll find a way to spin it into something even bigger than youtube videos(if you haven't already)

    • @derekbanas
      @derekbanas  10 ปีที่แล้ว

      nicolasap86 Thank you :) I'm very happy that I could help with patterns. Many more videos are coming and they will always be free.

  • @derekbanas
    @derekbanas  11 ปีที่แล้ว

    Thank you very much :) It is very nice to be able to help people understand this stuff. Many more videos are coming.

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

    Old is Gold.
    Never imagined video from 2012 is still the best one out here

  • @derekbanas
    @derekbanas  11 ปีที่แล้ว

    Thank you very much for taking your time to show your appreciation :) You have no idea how much I appreciate that! I'm very happy to have been of help

  • @derekbanas
    @derekbanas  11 ปีที่แล้ว

    Thank you very much :) I already have the UML tutorial finished. I hope you like it

  • @derekbanas
    @derekbanas  12 ปีที่แล้ว

    Thank you very much :) I appreciate that. I planned on introducing UML along with this tutorial. The only problem is that I could easily make 20 videos on the subject. I can't guarantee it would be finished. Thank you for telling me there is interest in that tutorial though

  • @derekbanas
    @derekbanas  12 ปีที่แล้ว

    Don't worry it is easy to get confused by design patterns. It is just a new way of thinking about programming. It may not seem like it now, but after you understand these oop concepts, it becomes very easy to make very complicated systems

  • @Vendettaaaa666
    @Vendettaaaa666 11 ปีที่แล้ว

    to summarize,
    Use strategy pattern(Composition), when you wanna have behaviours that change DYNAMICALLY.
    Use Inheritance when the behaviour changes STATICALLY.
    Right?

  • @derekbanas
    @derekbanas  11 ปีที่แล้ว

    You're very welcome :) it is very gratifying to be able to help people all over the world

  • @derekbanas
    @derekbanas  11 ปีที่แล้ว

    I have made a bunch of stock analysis tools and many patterns are involved. Yes the observer is very common. Sorry, but it depends on your program

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

    I read these up instead of GoF. I have read Head first before but these videos do a better job than anything I know of.
    Derek Banas , you da real MVP .

    • @derekbanas
      @derekbanas  10 ปีที่แล้ว

      sankalp srivastava Thank you for the nice compliment :)

  • @derekbanas
    @derekbanas  11 ปีที่แล้ว

    Thank you :) Technically Fly should be an interface because in the real world Fly wouldn't be considered an object. I stick with that idea when writing code, but you may find a reason to use an abstract class that I haven't thought of.

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

    flyingType should be private I agree. I didn't call setFlyingAbility in a constructor because I was trying to be as flexible as possible. Well sort of I could have done other things to add flexibility in hindsight. Sometimes while focusing on the pattern I tend to disregard everything because I have this belief in my head that if I focus on optimization that it will confuse people. I hope that makes sense

  • @KieranDevvs
    @KieranDevvs 7 ปีที่แล้ว

    Wouldn't an easier case to be have a nested superclass?
    Animal (Abstract) -> FlyingAnimal (Abstract) -> Bird
    Animal (Abstract) -> LandAnimal (Abstract) -> Dog
    FlyingAnimal type creates a base fly method that can be overridden.

  • @derekbanas
    @derekbanas  11 ปีที่แล้ว

    Yes that should work. You may want to give it a different descriptive name other than fly though

  • @derekbanas
    @derekbanas  11 ปีที่แล้ว

    Thank you very much :) I work hard to make the best tutorials I can. I'm glad you like them

  • @sudarshankj
    @sudarshankj 7 ปีที่แล้ว

    I never liked paying college fees, because I felt its not worth it. But for your videos, I voluntarily wish to. That is the degree of satisfaction I have. I can't thank you enough.

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

      Thanks again for the compliment :) I'm very happy that the videos are helping. My goal is a free education for all

  • @MrRaam030
    @MrRaam030 11 ปีที่แล้ว

    Amazing tutorial, there is lot of stuff cramped in nicely into this short video, felt like completed 50 pages of reading in ease ... Thank u..

  • @derekbanas
    @derekbanas  11 ปีที่แล้ว

    Thank you :) I have a UML tutorial and I'm going to revisit these patterns again in my code refactoring tutorials that will start next week.

  • @derekbanas
    @derekbanas  11 ปีที่แล้ว

    You're very welcome :) If you take a look at my OOD tutorials and the refactoring one you'll see that my number one goal is to write very easy to understand code. Very often we have to give up on some capabilities if we want to achieve that goal.

  • @KrishnaKumar-ud9cj
    @KrishnaKumar-ud9cj 8 ปีที่แล้ว

    Excellent. Many Thanks to u.
    1. Way of explanation
    2. Choosing of right examples
    3. Timing
    All are good.
    Till now I am hearing about OOP but not able to understand how it's powerful but because of your examples only I could understand how OOP powerful.

    • @derekbanas
      @derekbanas  8 ปีที่แล้ว

      +Krishna Kumar You're very welcome :)

  • @Avarnus
    @Avarnus 11 ปีที่แล้ว

    Thank you so much for those video on Design Pattern. As soon as I see it, I start looking at it and man, I realize I don;t understand in deeper concept even though I have completed projects in school using those concept.

  • @spooky_zen
    @spooky_zen 6 ปีที่แล้ว

    compile, overloading, static and earley; Runtime, overriding dynamic and late.

  • @TheWischmopps
    @TheWischmopps 7 ปีที่แล้ว

    Hi, i'm studying computer science at university of tübingen in germany. All i can say ist that because of you i don't have to visit the lectures for software design to be successful. You know how to put the things in a nutshell. Afterwards i still can dive in, in fact even better then.

    • @derekbanas
      @derekbanas  7 ปีที่แล้ว

      Thank you :) I'm very happy I helped. Won't your professor get lonely though?

  • @laufgas
    @laufgas 8 ปีที่แล้ว

    Hi Derek, I have number of questions:
    1. Just to make sure that I understand what I see: in your Flys.java file you have 3 classes? Flys which is interface and 2 classes of Fly/CantFly which implements the fly() method? It is a bit confusing. Why all of the classes are in the same .java file?
    2. I read your replies to the comments below and also in your website and I did not understand how the Animal class uses flyingType: Flys as a field? I saw you wrote about composition, but what exactly is composition?
    Thanks for the great videos!
    Shai

    • @krzysztofk.8842
      @krzysztofk.8842 8 ปีที่แล้ว

      Well you probably figured it out yourself by now, since you asked your question a month ago. Putting this two classes in the interface JAVA file is just for clarity and (as far as I understood in my short career as java weekend warrior) is the preffered way, something like good practise, as these classes shouldn't be used outside the interface.

    • @samtux762
      @samtux762 8 ปีที่แล้ว

      1. This is called inner classes. It is convenient to place several small tightly related classes in one java file. You can (and usually should) place each class in separate Java file, because these inner classes bloat code in no time.
      2. You can have an instance of a class in a class field. `Flys flyingTipe;` is no different from `List myList;`

  • @jzeman
    @jzeman 11 ปีที่แล้ว

    Derek. Even though your implementation is technically correct there are few problems. First the strategy pattern should always have the strategy implemented, typically through a constructor. Secondly the use of an interface to implement an in-ability is confusing, it should not be done. A more appropriate example would say an color system where the strategy could be color by RGB, HSV or HSL. In short stratgey must be required, should not be negated

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

    Two birds in a single shot! I am trying to learn Design Patterns for C#, but learning Java too :)
    Actually 4 birds (Design Patterns, Java, Inheritance and Polymorphism). Excellent example with all these together. Thank you very much. You are the best!!!

  • @derekbanas
    @derekbanas  11 ปีที่แล้ว

    Composition is a major part of most of the design patterns, so I can't really assign that just to the strategy pattern. Design patterns in general try to add flexibility through avoiding high coupling in many ways that are quite similar. Inheritance should be avoided if at all possible, but above all else understandable code is the goal

  • @derekbanas
    @derekbanas  11 ปีที่แล้ว

    Thank you very much :) That is a much appreciated compliment. I do my best to present these topics in an interesting way

  • @derekbanas
    @derekbanas  11 ปีที่แล้ว

    You would use it with classes that are very similar. What you referred to is the skill one must develop after learning about patterns. Knowing when not to use a pattern is just as important as learning when to use them. I cover more on that in the refactoring tutorial.

  • @derekbanas
    @derekbanas  11 ปีที่แล้ว

    Thank you :) I did my best with it

  • @derekbanas
    @derekbanas  12 ปีที่แล้ว

    Ok, I'll make one. I want to make an entire software engineer course here and that will definitely be part of it :)

  • @derekbanas
    @derekbanas  11 ปีที่แล้ว

    Yes I should have marked it as private. Sorry about that. I made the UML tutorial. It's on my TH-cam channel. Thank you for the kind words. I'm always here to teach whenever you are in need :)

  • @elliotjunemusic
    @elliotjunemusic 9 ปีที่แล้ว

    Been trying to get this pattern for a whille, and what are it's advantages, probably the BEST tutorial out there on the matter. Fudge. I'm impressed.

    • @derekbanas
      @derekbanas  9 ปีที่แล้ว

      Etienne-Joseph Charles Thank you :) I did my best to make it easy to understand.

  • @derekbanas
    @derekbanas  12 ปีที่แล้ว

    Thank you :) I have a Design Pattern, Java, etc. playlist on my TH-cam channel. Sorry, if everything is a bit confusing because I have so many videos. I try to aim for 10 to 15 minute videos. I feel that that is the sweet spot for completely covering topics. I know I'm the only person making videos that long. Actually my videos start out at 30 minutes before editing. I'm working to improve them all of the time. Thank you for your insight :)