What is the Decorator Pattern? (Software Design Patterns)

Поділитися
Вставка
  • Опубліковано 15 лис 2020
  • In this video, learn why the Decorator Pattern is such an important design pattern. I tell you what the decorator 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/beabetterdevv/Desi...
    🌎 Find me here:
    Twitter - / beabetterdevv
    Instagram - / beabetterdevv
    Patreon - Donations help fund additional content - / beabetterdev
    #DesignPatterns
    #DecoratorPattern
    #SoftwareDesign
    #SoftwareEngineer

КОМЕНТАРІ • 72

  • @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 2 місяці тому

      Yup. I subscribed

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

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

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

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

  • @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.

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

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

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

    lovely short and sweet explaination brother

  • @HuGGGzz
    @HuGGGzz 3 роки тому

    really appreciate you doing these videos!

  • @ThuyNguyen-bu9ge
    @ThuyNguyen-bu9ge 3 роки тому +1

    Great voice! Good teaching!

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

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

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

    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!

  • @Shiva-zy7jq
    @Shiva-zy7jq 3 роки тому +1

    Thank you so much for the video. Very informative.

  • @UpTown430
    @UpTown430 3 роки тому +1

    Very clear, thank you 👌

  • @andriys5772
    @andriys5772 3 місяці тому

    Thank you!

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

    Thanks a lot!

  •  Рік тому

    Well done!

  • @ThuyNguyen-bu9ge
    @ThuyNguyen-bu9ge 3 роки тому +4

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

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

    Thank you so much!!

  • @yasahanzengin3329
    @yasahanzengin3329 3 роки тому

    learned a lot, thanks!

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

    Thank you

  • @TheThiagosp1
    @TheThiagosp1 3 роки тому +1

    Very good content. Congrats!!!!

  • @ramakrishnapatil188
    @ramakrishnapatil188 3 роки тому +1

    Really a good explanation. Thanks

    • @BeABetterDev
      @BeABetterDev  3 роки тому

      Thanks Ramakrishna, you're very welcome!

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

    Really good explanation!!

  • @HoangHahalehoang
    @HoangHahalehoang 3 роки тому +1

    good explanation buddy, that helps

  • @ufukcagataydogan3985
    @ufukcagataydogan3985 3 роки тому +1

    you are the enlighter.....

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

    watched the first video and turned on the bell icon!

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

    Nice video, thanks

  • @Gorky25
    @Gorky25 3 роки тому

    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?

  • @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?

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

    great

  • @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!

  • @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 Місяць тому

      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

  • @maksymoliinyk2647
    @maksymoliinyk2647 3 роки тому

    thanks

    • @BeABetterDev
      @BeABetterDev  3 роки тому

      You're very welcome! Thanks for watching Maksym.

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

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

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

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

  • @zeppelinmexicano
    @zeppelinmexicano 16 днів тому

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

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

    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());
    }

  • @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?

  • @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 2 роки тому +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 2 роки тому

      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 2 роки тому

      @@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 2 роки тому +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 2 роки тому

      @@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.

  • @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

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

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

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

    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.