What is the Decorator Pattern? (Software Design Patterns)

แชร์
ฝัง
  • เผยแพร่เมื่อ 20 ต.ค. 2024

ความคิดเห็น • 76

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

    Thank you. I finally understand what "Decorator" does.

  • @marcchapman6812
    @marcchapman6812 3 ปีที่แล้ว +22

    I think you explained it better than the book did, actually.

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

      I don't think there can be a better compliment than this :)

    • @vut9690
      @vut9690 6 หลายเดือนก่อน

      Yup. I subscribed

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

    i've banged my head against a wall for this concept or at least trying to grasp its practicality for a long time. the scales have fallen from eyes (via java😊). thanks!

  • @md.shamswadudabbir12
    @md.shamswadudabbir12 2 ปีที่แล้ว

    Thank you. I was lost when I get into the code of getDescription() in Head First. And also your suggestions are awesome.

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

    this gonna save me from this semester😍 very clear and informative, BIG THANKS🥂

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

      You're very wecome Marckhycs!

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

    watched the first video and turned on the bell icon!

    • @BeABetterDev
      @BeABetterDev  2 ปีที่แล้ว

      Thanks Himanshu and welcome!

  • @wesley2254
    @wesley2254 2 ปีที่แล้ว

    Thank you! Working on a project for school and this helped a lot

  • @ziekaman2450
    @ziekaman2450 2 ปีที่แล้ว

    Thankyou sir. I wish everybody would take the time to explain patterns like you.

  • @sandeepsihoria5230
    @sandeepsihoria5230 2 ปีที่แล้ว

    lovely short and sweet explaination brother

  • @ThuyNguyen-bu9ge
    @ThuyNguyen-bu9ge 3 ปีที่แล้ว +1

    Great voice! Good teaching!

  • @ufukcagataydogan3985
    @ufukcagataydogan3985 3 ปีที่แล้ว +1

    you are the enlighter.....

  • @Shiva-zy7jq
    @Shiva-zy7jq 3 ปีที่แล้ว +1

    Thank you so much for the video. Very informative.

  • @ramakrishnapatil188
    @ramakrishnapatil188 3 ปีที่แล้ว +1

    Really a good explanation. Thanks

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

      Thanks Ramakrishna, you're very welcome!

  • @khomo12
    @khomo12 3 ปีที่แล้ว +1

    Really good explanation!!

  • @ThuyNguyen-bu9ge
    @ThuyNguyen-bu9ge 3 ปีที่แล้ว +3

    If the Decorator Pattern is extending functionality via composition rather than subclassing, shouldn't we have interfaces rather than abstract classes?

  • @UpTown430
    @UpTown430 3 ปีที่แล้ว +1

    Very clear, thank you 👌

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

    really appreciate you doing these videos!

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

      You're very welcome Rob!

  • @HoangHahalehoang
    @HoangHahalehoang 3 ปีที่แล้ว +1

    good explanation buddy, that helps

  • @TheThiagosp1
    @TheThiagosp1 3 ปีที่แล้ว +1

    Very good content. Congrats!!!!

  • @andriys5772
    @andriys5772 7 หลายเดือนก่อน

    Thank you!

  • @ThuyNguyen-bu9ge
    @ThuyNguyen-bu9ge 3 ปีที่แล้ว +4

    It's a recursive strategy - one class decorator has a reference to another class decorator.

  • @alihsanelmas
    @alihsanelmas ปีที่แล้ว

    Well done!

  • @letoatreides8098
    @letoatreides8098 3 ปีที่แล้ว +5

    But isn't it supposed to be an interface if you're implementing it. An abstract class if you're extending it.

    • @rikkoo
      @rikkoo 5 หลายเดือนก่อน

      the idea is to program to an interface, at some point in the development you will have to extend some classes. At the end of the day you're still using implementing an interface through an abstract class

  • @acavaelnick
    @acavaelnick 3 ปีที่แล้ว +1

    Nice video, thanks

    • @BeABetterDev
      @BeABetterDev  3 ปีที่แล้ว +1

      You're very welcome Joaquin!

  • @Gorky25
    @Gorky25 3 ปีที่แล้ว +1

    A very good example, thank you.
    I have additional questions. With the class CoffeeDecoration, we add all additional which will have our let's say final object Espresso right? But if we want to add i.g. Machiatto will again implement Coffee and if Machiatto has something special we will that add in the same way as we added WithMilk, WithSugar, etc. Right?
    p.s. How did you generate the diagram with IntelliJ? I use the community version. If the community doesn't have can you please suggest some add to me and to others?

  • @kunalraikar8087
    @kunalraikar8087 2 ปีที่แล้ว

    Thank you

  • @oianmol
    @oianmol 2 ปีที่แล้ว

    Thanks a lot!

  • @alitc300
    @alitc300 2 ปีที่แล้ว

    Thanks for the great explanation. I turned you code as an exercise to c# and it did not work. Only after i did this it worked. I had to create a CoffeeDecorator object on line 3 and a separate print method on line 5 :-)
    1. Coffee espresso = new Espresso();
    2. printCoffee(espresso);
    3. CoffeeDecorator espressoDecorator = new WithMilk(espresso);
    4. printCoffee2(espressoDecorator);
    5. private static void printCoffeeDecorator(CoffeeDecorator c)
    {
    Console.WriteLine("Cost: " + c.cost() + ", Description: " + c.getDescription());
    }

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

    shouldn't espresso extends coffee? since coffee is an abstract class and not an interface?

  • @George-or3uv
    @George-or3uv 3 ปีที่แล้ว +3

    Just wanted to let the viewers know that there is the 2nd edition of this book that’s out, while 1st Ed Is reviewed in the video, there are minimal differences.

    • @BeABetterDev
      @BeABetterDev  3 ปีที่แล้ว +1

      Thanks for pointing this out George!

  • @satishkumar1799
    @satishkumar1799 2 ปีที่แล้ว

    How can add mutilple functionlities dynamically? Like expresso withsugar and withMilk both..

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

    learned a lot, thanks!

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

      Great to hear, thanks Yasahan!

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

    Just wanted to say thank you. Nice and clear presentation.
    One question if you don't mind: Lets say I want to split business model class from ORM - would decorator pattern be the way to go? Simple example: there is a User class with id/email. One set of decorators would be ORM related, like table name, relationships, foreign keys, etc. Second set of decorators might be for example JSON response serialization. Does this make sense?
    Thank you in advance

  • @jwolfe890
    @jwolfe890 ปีที่แล้ว

    What is the point of even having a Coffee Decorator class? Couldn't the WithMilk and WithSugar classes just extend Beverage and still work within the decorator pattern?

  • @minhnguyenkha867
    @minhnguyenkha867 2 ปีที่แล้ว

    great

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

    thanks

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

      You're very welcome! Thanks for watching Maksym.

  • @zeppelinmexicano
    @zeppelinmexicano 4 หลายเดือนก่อน

    Bien hecho, amigo (well done in your weird language of Inglish).

  • @donwald3436
    @donwald3436 2 ปีที่แล้ว

    15:53 Huh? Surely most of that belongs in the superclass?

  • @asaduzmn
    @asaduzmn หลายเดือนก่อน

    Can anyone tell me what's the name of his IDE?

    • @BeABetterDev
      @BeABetterDev  หลายเดือนก่อน

      IntelliJ Idea

    • @asaduzmn
      @asaduzmn หลายเดือนก่อน

      Sorry to mention. Thanks for explaining nicely

    • @asaduzmn
      @asaduzmn หลายเดือนก่อน

      @@BeABetterDev Thanks

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

    I should not buy the book. it says to much. i prefer your way of teaching :P

  • @drcl7429
    @drcl7429 2 ปีที่แล้ว

    Still don't see how this is a useful pattern. It doesn't prevent infinite classes.

  • @ondict6205
    @ondict6205 ปีที่แล้ว

    coffee shop, pizza shop, cooking pizza, making coffee...
    do Americans do something except for devouring?

    • @BeABetterDev
      @BeABetterDev  ปีที่แล้ว

      That is a very ignorant thing to say.

    • @ondict6205
      @ondict6205 ปีที่แล้ว

      @@BeABetterDev Thank you so much! Now I've learned a new English word. Ignorant means something that Americans are allowed to do towards Russians but Russians aren't allowed towards Americans.

  • @22222222222222223464
    @22222222222222223464 3 ปีที่แล้ว +1

    Hi! Thank you for your videos. I have a question. Isn't that correct that in the case of "espresso = WithSugar(espresso)", actually the espresso variable isn't Espresso, it's just Coffee? I mean, by using this pattern we lose properties and methods of Espresso class when we want to make it with sugar as opposed to the approach you told about at the beginning of the video

    • @SuperSlugger94
      @SuperSlugger94 3 ปีที่แล้ว +1

      In this example what he is doing is creating the Abstract Base Class (ABC) "Coffee" which is the only thing known to the client code! This means that the client/code should deal with implementation specific methods/properties of the ABC class Coffee (the Interface). So the client should not aware of any specific methods or properties assigned to any concrete classes of Coffee (e.g. Espresso). If you start relying on specific methods of concrete classes then you are coupling code which needs to be altered if the implementation changes for those methods, and hence is violating the Open/Closed principle. What you instead could do is having "private" methods inside Espresso which are called once public method(s) of the Coffee Interface is called (getDescription(...) or cost (...) in this example)
      If this is not what you are looking for, then another design pattern could instead be appropriate, for example, Adapter Pattern or regular inheritance.

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

      So to summarize, when creating a Coffee:
      Coffee espresso = new Espresso()
      or
      Coffee java = new Java()
      The code should only know that it is dealing with a Coffee, so you know that for each coffee instance you are able to call getDescription () and cost (). If you need to check the instance of the coffee whether it is an Espresso or Java, for example, to call specific methods/properties like getEspressoIngredients() (made up method). Then you are coupling your code to the concrete class Espresso, but what happenes if Espresso is taken off the menu? Then you need to change your code everywhere you are calling getEspressoIngredients(); which is violating open/closed principle and should be avoidable 😊

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

      @@SuperSlugger94 No no, that's not what I meant. I know that Coffee is an abstract class and how we can use it. My point was that the expression "espresso = WithSugar(espresso)" is not useful because after the assignment we lose the specific parameters of the Espresso class.
      Let's say, as you said, Espresso has its own method getEspressoIngredients, ok, and let's say I want to use the ingredients somehow and send the cup to a client. What do I do?
      - I create the Espresso
      - Put the sugar into it
      - Get Espresso Ingredients
      - Finally, send the cup to a client
      Using the pattern I can't get Espresso Ingredients just because I put the sugar into it. That's the point. For some reason, putting the sugar into espresso doesn't allow me to use it as espresso without casting any longer. However, if I used the classic way that he mentioned at the beginning of the video with a method Add() I would be able to.
      Speaking of the coupling to specific methods, we could use a function sendCoffee() which accepts Coffee, not Espresso, not breaking anything.

    • @22222222222222223464
      @22222222222222223464 3 ปีที่แล้ว +1

      In addition, the statement "CoffeeDecorator is Coffee" doesn't sound good. My point isn't the pattern is bad but the example isn't good.

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

      @@22222222222222223464 Yes, now I see what you mean! It is exactly as you say! If you need the concrete classes of Coffee to be very unique in the sense that they provide completely different behaviors, for example getEspressoIngredients() and getJavaBeanSize(), then as you say this pattern isn't really useful! When decorating a concrete class it again becomes of the thing you are decorating + additional behaviors (but only in the sense of the ABC Interface), the ABC Interface is the only thing you know that won't change when you are decorating something! All other methods specific to concrete classes (e.g Espresso and Java) will disapear once it is decorated with e.g addSugar(Espresso), so it won't be reachable unless explicitly casting.
      So I see your point; in the scenario you are describing this pattern loses its purpose. However, if you only want to extend behaviors related to the ABC Interface then this pattern could be useful depending on your usecase.

  • @termi130
    @termi130 2 ปีที่แล้ว

    Thank you so much!!