The Decorator Pattern Explained and Implemented in Java | Structural Design Patterns | Geekific

Поділитися
Вставка
  • Опубліковано 15 гру 2024

КОМЕНТАРІ • 59

  • @Samuel-vq1jo
    @Samuel-vq1jo 2 роки тому +69

    Bro gotta be tired from carrying so many students through softwaredesign courses. No but seriously thank you soo much for your comprehensive content

    • @LordErnie
      @LordErnie Рік тому +4

      Tbh the decorator pattern is hard to wrap your head around because of the fact that you are acting on a shell, not the object, and that the shell can recursively call itself an unknown amount of times. That's what makes it so hard. The object doesn't contain the notifiers, the notifiers contain each other and the object. That in and of itself is totally different in it's way of thinkng compared the usual way of thinking that we have in OOP, which is think human like, not computer like. The decorator pattern is so hard fo this reason, it totally omits the usual way of thinking that we have when programming conventional OOP. The decorator pattern is almost a mirror of recursive funtions in functional programming. I learned that this is logical only after doing a functional programming course.
      To be fair, there are many ways to avoid using the decorator. A shell within a shell within a shell isn't nice conceptually and contextually, and it WILL lead to some nasty code. Using composition is good, but at least know what you are doing. Look at the decorator pattern for a moment and see the following. You call functionality a, which calls b, and c, and so on. But why? Let's say we have that notifier. We want to notify. How do we achieve this? When we do it we go down the line executing everything like a composed function chain. That to me sounds a lot like a pipeline. A simple example of this is middleware. You could achieve a middleware like behaviour by using the decorator pattern. But in fact, you could just use function composition instead. It would be a function that would take another function, and an optional callback function. Ones the calls are done, you will have achieved your result. It's the EXACT SAME THING.
      You wouldn't have to worry about objects having objects and so on. The functions that you pass can generate closures if needed so they can reference their origin object and it's values. C# does this under the hood, but you could just make your own closure if you wanted to. Now call notify, and see that you still call the one primary object that you tasked with notification. It's job is just to contain a chain, a pipeline, of functions or objects. Back to the decorator pattern, it does just this very thing. But human terminology is just really confusing as what you are doing isn't decoration, it's pipeline composure. Hell I am a first year informatics student and when I spotted this during the lecture the teacher sat down with me for three hours straight just to discuss this fenomena.
      It's so much easier when you use the correct terms. It's not a notificationdecorator, it's a pipeline pease. During our little chat (me and teach) we called them fragments, or pipe peases. NotificationPipelineFragment is what we used. We composed the entire thing again, just changing names. And the code? It was self explanatory. Balance was back in the world of WTF OOP, and it actually made more sense to the students (including myself). So to summon it up, the decorator pattern is hard because it is misunderstood, and people use the wrong terminology when using it. I personally thing that the decorator pattern is falsely named. Pipeline pattern, wrapper pattern, or callback compositional pattern would be a better suited name if we would go for something a bit more self explanatory. A hickup in modern day design, just because they used the word wrong. Do note that that's just for simple cases like this one as far as I know.
      And yes, there are cases for why decoration would be a somewhat valid term, but you would still be wrapping and forming a shell, just to have basic callback functionality. Often but not, the decorator uses the state of the inner contained object, or has complex logical relations with the contained object. But it the end it's still, and will always be, a pipeline of objects.

    • @LordErnie
      @LordErnie Рік тому +3

      To add to that, try looking at it like the decorations are addons. Addon pattern would be a weard name, but you get the general idea.

  • @aman8244
    @aman8244 10 місяців тому +13

    This is the best "Design Pattern" playlist.
    You are an amazing teacher.

  • @JB-gv7pt
    @JB-gv7pt 9 місяців тому +2

    I had to watch this twice to fully understand it. I would have neverrrrr gotten it just via my course material. Thank you so much :)

  • @lukasaudir8
    @lukasaudir8 8 місяців тому +2

    This is the go to channel when you need a refresh on some design pattern

  • @thebulletkin8393
    @thebulletkin8393 Рік тому +1

    Been learning these as I go, and man while it’s tough to understand immediately, it feels so satisfying to have a go at them and understand how you can use them on your own

  • @root55
    @root55 Рік тому +3

    Thanks a lot mate. Your code examples helped very much, but also the diagram and general explanation. Exactly what I needed. You are a good Teacher.

  • @moisesdepaulodias7980
    @moisesdepaulodias7980 2 роки тому +2

    The other tutorials always executed a code on the decorator before executing the wrapped code, I didn`t think that I can call the wrapped code before, just like you did, nice video man.

    • @geekific
      @geekific  2 роки тому +1

      Glad I could help!

  • @DjangoMx
    @DjangoMx Рік тому +1

    WTF, the way you explain things is the best ever I've seen!

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

    Your video style is very well executed, helped me wrap my head around this topic. Especially the code example, and how the method calls bubbled up to parent classes. Keep up the high quality! Subscribed ❤

  • @battuhant
    @battuhant Місяць тому

    what is the need to create basenotifier, we can just send it directly to the next object

  • @kerakricks2613
    @kerakricks2613 Рік тому +2

    Why can’t the client can instantiate two notifier classes and notify the user instead of creating subclasses of combinations?

  • @lamdev6186
    @lamdev6186 21 день тому

    At 3:26, BaseNotifierDecorator class should not instantiate nor contain databaseService attribute, as DatabaseService is already instanciated in Notifier class, right ?
    Also, DatabaseService should have been injected in Notifier class by an IoC container.

  • @sharif47
    @sharif47 10 місяців тому +1

    Hey, can anyone tell me why Notifier is also implementing another system in its send function? I believe Notifier has to at least have a send function since it is based on INotifier. But in our current structure, won't every single implementation send message by mail even if it's not required?

  • @amitghosh5662
    @amitghosh5662 2 роки тому +3

    Thanks a bunch.. awesome explanation.

    • @geekific
      @geekific  2 роки тому +1

      Glad it was helpful!

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

    Thanks for nice explanation of Decorator Pattern, it is really helpful. 😀😀

  • @rashmigupta-zg3cr
    @rashmigupta-zg3cr Місяць тому

    Waao, what an explanation!🔥

  • @yogeshdharya3857
    @yogeshdharya3857 6 місяців тому

    Everything makes sense now thank u 🍬👌

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

    Thanks so much for this very clear course !

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

    But in this example i can't have e plain facebook notifier without the email notifier (the first Notifier concrete component), right?

  • @butterbutter
    @butterbutter 8 місяців тому

    Great video! I was wondering if it would be possible to use the Composite pattern here? We can have that Whatsapp and Email notifications implement the Notifier interface and then have compositeApps which can include any platform (Whatsapp, email, facebook, etc). Thanks in advance!

  • @muhammadbinaslam1933
    @muhammadbinaslam1933 8 місяців тому

    Thanks for such great help!

  • @thanirmalai
    @thanirmalai 7 місяців тому

    Amazing content

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

    Amazing Explanation

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

    Other than one being Creational and the other being Structural, what is the difference between the Decorator and Factory pattern? I know in Factory the subclasses of the component can create an object of the subclasses of the Decorator class, but they seem to be implemented in the same way.

    • @geekific
      @geekific  Рік тому +1

      The Decorator pattern enhances the behavior of an individual object dynamically by wrapping it with additional functionality, while the Factory pattern provides an interface for creating instances of different classes without specifying their concrete types directly. In summary, the Decorator pattern enhances the behavior of an object, while the Factory pattern abstracts the creation of objects.

  • @christofrankf8458
    @christofrankf8458 24 дні тому

    awesome, i sat 3 hours in a lecture to understand this, lol

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

    Thank you for your video, it is helpful. But i miss one thing. The problem with inheritance you introduced was that we need to create multiply subclasses such WhatsappSmsNotifier, SmsFacebookNotifier, WhatsappFacebookNotifier and then you introduced approach with decorators.
    The thing I dont understand how decorators solve your example problem ? If user wants to receive notifications via SMS and Whatsapp we still need to create a SmsWhatsappDecorator, dont we ? Thank you for your response.

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

      Glad it was helpful :) Nope. You wrap your objects with two decorators instead of one. Using the singular classes we can now generate the permutations we want, contrary to the issue mentioned at the beginning. Cheers! Hope this answers it.

  • @rfc-793-darpa
    @rfc-793-darpa 2 роки тому

    Question: Is it not better to use the observer pattern in this situation instead?
    I mean, it is much easier to store an array of notify objects and on notifyMethod, iterate through the notify array calling notify.execute(), if you know what I mean;

    • @geekific
      @geekific  2 роки тому +2

      Many problems can be solved with the help of multiple patterns! Feel free to try it out! The video is more about explaining the pattern rather than trying to solve a specific question :)

  • @ProgressiveDeveloper
    @ProgressiveDeveloper 5 місяців тому

    Thanks too much for your playlist.

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

    in the example y do u have to send the email alongwith whatsapp and Facebook , like what if i want to send only by whatsapp and facebook

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

      This is acting as a base behavior or our notifier, it could be anything else, and you could easily modify it

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

    Great Video Bro.
    where can i find the slides ?

    • @geekific
      @geekific  Рік тому +1

      We may add them in the future to GitHub, for now only code is available.

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

    how to send notification only to whatsapp ? or only to fb? with this approach? since the whatsappDecorator needs an instance of a INotfier can you please explain me this?

    • @geekific
      @geekific  Рік тому +1

      If that is your use-case, then the decorator isn't the way to go. Each pattern tries to solve a specific problem. Cheers!

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

      @@geekific thanks for the explaination understood now.

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

    Question: why can’t the client just initiate three objects and call send() three times? Whats wrong with this approach?

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

      There is nothing wrong! However, this way, your code is more reusable, more flexible in case many clients have to make use it, and most importantly the structure of the calls (base call first and then decorators) is guaranteed at compile time. Cheers!

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

    i have a question . What if i want to only send the message by what's up ? Otherwise thank you for your efforts . May god bless you

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

      Thank you! Bless you as well :) Well, then maybe WhatsApp should be your base notifier, as per the design, the decorator is added on top of a default behavior, and in our case it was a mail! Cheers!

  • @perfectketchup
    @perfectketchup 7 місяців тому

    Sorry but why cant you make a list of INotifiers, and call by iteration, instead of creating this ceremonial boilerplate?

  • @rburaksaritas
    @rburaksaritas 7 місяців тому

    isn't this just a microkernel architecture pattern?

  • @barhum5765
    @barhum5765 2 роки тому +1

    god bless your soul

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

    funny enough, the decorator pattern and chain of responsibility pattern are very similar, though the purpose is different.

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

      Hope our videos are helping you understanding the purpose of each!

    • @marcospampi7154
      @marcospampi7154 2 роки тому +1

      @@geekific yep, kinda used these patterns at work, but I have a software engineering exam Thursday and this helps in reviewing.
      Thank you a super duper lot ☕

  • @shivarajput9719
    @shivarajput9719 9 місяців тому

    if someone only wants to send notification through slack, this code end up sending notification email also

    • @geekific
      @geekific  9 місяців тому +1

      Then it is not for your use-case!

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

    As someone new to this I just had a bunch of words piled on words.

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

      Can you please tell us which parts were the most confusing so we can try to help and improve in future videos?