Decorator Design Pattern

Get the Cheat Sheet:

Best Design Patterns Book :

Welcome to my Decorator Design Pattern Tutorial. The Decorator allows you to modify an object dynamically. You would use it when you want the capabilities of inheritance with subclasses, but you need to add functionality at run time.

It is more flexible than inheritance. The Decorator Design Pattern simplifies code because you add functionality using many simple classes. Also, rather than rewrite old code you can extend it with new code and that is always good.






50 responses to “Decorator Design Pattern”

  1. Malcolm Bailey Avatar

    The UML association in the diagram is *composition*, not aggregation. Aggregation uses an empty diamond.

  2. MecchaKakkoii Avatar

    Great vid! Concise and very clear 🙂

  3. Jackson Rollins Avatar

    I get that this is for demonstration purposes only so thank you. If I were ever to create a pizza by programming I would have a `Pizza` class with a toppings member that held an array of `Topping`s. `new Pizza([new ThinDough(), new Mozzarella(), new TomatoSauce()]);`

  4. yashfernandojain Avatar

    All of these design look exactly the same to me.


    thank you for the helpful tutorial, but i have a doubt. in this pattern, all the toppings implement a topping decorator which are added to a PlainPizza. would it go against the guidelines of decorator design pattern if a have a different class at the same level as plainpizza( i know PlainPizza is the most basic but in a scenario where there was another type of pizza say BasicPizza) that implements Pizza and all toppings are added to it? Do i have to rewrite both PlainPizza and BasicPizza to extend some class XYZPizza than implements Pizza interface to maintain the structure? Thank you in advance.

  6. שקד גרינפלד Avatar

    one more time,I just have to say!
    you are the best in Youtube…

  7. Priyanka Jondhale Avatar

    Why do we need to define methods(getDescription , getCost) for tempPizza in ToppingDecorator Class?? i think It will work fine without that too. Is it ??

  8. LunamiiMusic Avatar

    The GOF book has the component object within the abstract decorator listed as private, not protected. Is there any reason to go with Protected vs private?

  9. restoMAG Avatar

    I just don't get where's the "adding functionality at runtime" if you can't create a different pizza at runtime.

  10. Sarah Tammenga Avatar

    Now I wanna eat a pepperoni pizza… But it the tutorial really helped me! Thanks

  11. FesliyanStudios Avatar

    Why not just create methods in the super class that can add price, etc…? Just a newb asking.

  12. Yevgeny Avatar

    Never heard of three cheese pizza (I've worked in a restau for one summer). Is four cheese pizza is also popular in the US?

  13. Ashish Saraswat Avatar

    Thanks for the video .. They are really helpful .. Rather than reading the GangOfFour book this is much precise and better to be done over a weekend

  14. Justin Almassi Avatar

    you can get an instance of an interface? – you typed 'public protected Pizza' (Pizza is an interface) how is that possible? I thought interfaces were totally abstract.

  15. Vimanyu Aggarwal Avatar

    +Derek Banas : Filled diamond represents composition not aggregation ! Also, the diamond is positioned near the parent. Kindly correct me if I am wrong.

  16. Thien Pham Quang Avatar

    What is ThreeCheesePizza means ?

  17. MrBeardtard Avatar

    How come in the Mozzeralla or the tomatosauce class there was no declaration of tempPizza, how did java just know?

  18. Cindy O Avatar

    Your voice is so different since this is a 5 year old video, and it really throws me off haha

  19. Ramon Brand Avatar

    Is this the same as the "Component" design pattern?

  20. Kirk Bae Avatar

    Thank you very much!

  21. grungeisdead Avatar

    You never thought to mention the language before getting into the video? (Even in the comments…)

  22. JamSamJackx Avatar

    Does the ToppingDecorator need to be abstract or can it be just another class that uses the Interface, then topping objects just extend that?

  23. flowing in May Avatar

    Enjoying the videos so far. However, I don't understand how is this different from the builder pattern?

  24. floriegl Avatar

    Great video. Really helped me with my presentation in programming.

  25. Mohammed Sultan Avatar

    Whenever I watch one of your videos, I ask my self "For which company this guy works? Google, Microsoft,….". However, when I see the number of videos you have on your channel , I think you are unemployed 🙂

  26. SpMeKP Avatar

    You should be hired as a tutor at my college. You just saved me 5 hours to say the least, while providing me with information in the most appropriate way. And this is not the first time…

  27. 0x28 Avatar

    Abstraction (e.g. the Decorator pattern) is used everywhere in modern programming! In ReactJS, in TypeScript, in custom solutions..

  28. 0x28 Avatar

    I'm glad I already graduated from my B.Sc. in C.S. I actually hate programming. I'm glad I'm off that boat!

  29. Yashasvi Girdhar Avatar

    What’s the need to use abstract class ‘Topping Decorator’ here? It doesn’t do anything and I think it’s use makes the concept confusing.

  30. Jaco Vermeulen Avatar

    I understand how this example works, though I wonder if you would be able to help me understand it better. I am given a class hierarchy where I have a Singly linked list(SLL). Doubly linked list(DLL) inherits from SLL. Now my assignment requires me to implement DLL as a decorated SLL. Any thoughts? I think I do not yet fully understand the decorator as I cannot figure out how I would go about doing this.

  31. Dragan Smoljan Avatar

    why its important for class ToppingDecorator to be abstract?

  32. michi bliblablub Avatar

    Couldn't you just add two properties for cost and description in ToppingDecorator?
    Then you don't need the methods in the subclasses who overrides the superclass-methods.
    ToppingDecorator.GetDescription() would then look like this:
    return tempPizza.GetDescription() + describtion;

  33. Fastjur Avatar

    Great video, one question though. Why did you add the "Adding dough" println statement to the Mozarella class? This should be in the PlainPizza class as this is where the dough is added and the price for the dough (and thus the plain pizza) is calculated.

  34. Vignesh K Avatar

    Awesome. Much better explanation than HeadFirstDesignPatterns. Hats off.

  35. Mr8lacklp Avatar

    But why would you do that instead of just making an abstract class topping and defining many different toppings and having an ArrayList of those inside your Pizza. If you want to know the price of the Pizza instance now, you just have to loop through the ArrayList inside your getCost() method and add the price of a plain Pizza to it. Same goes for the description. Just call getName on all the toppings. Besides now you coul have all the toppings subscribe to a sales department or something like that and have it manage their prices.

  36. Uğur Dinç Avatar

    To add more toppings, would I have for instance > new GreenPepper(new Spinach(new TomatoSauce(new Mozarella(new PlainPizza())))) ?

  37. Daniel Wahlstrom Avatar

    Great video, but poor example. The main method here doesn't do the design pattern justice. Check out this succinct explanation on SO:

  38. Arthur Chan Avatar

    Hi, Derek can you give me some hints of how to design a sales tax calculator? Say there are two types of products, normal and other, normal has a sale tax of 10% and other doesnt have tax. Also if products are imported, there will be additional 5% tax. Given a list of Products and their quantity, print out receipt just like a cashier machine does. For this simple question, I can just have two child classes derived from Product class but I dont think it is good if I want it to be scale-able, extendable, because later there may be more taxes for different products(which means i have to create bounch more different child classes).

    Right now, what i think is to just have one Product class, and a singleton class of called TaxMap, which contains tax for different products. So when I try to get tax, I will just call getTaxRate(ProductType) from TaxMap.

  39. Daniel Grooms Avatar

    I read this pattern in Gang of Four today, was trying to figure out what the main method would end up looking like, very glad I found this video. None of my classes for my B.S. CompSci covered this stuff and I'm finding it very important for my job now.

  40. Onno man Avatar

    why no dutch cheese 😉 😉

  41. David Kozák Avatar

    Derek, thank you very much. This video really helped me understand the DP. I have one question. I can see how to add new functionality at runtime, but I cannot imagine the process of removing functionality. Lets say that I have a composite object of Mozarella,Tomato Sauce and Pizza. How can I change this to just Mozarella and Pizza(Without the tomato sauce) or even simplify it to just Pizza? 🙂

  42. Big Freestyler Avatar

    thank you this really helps me in my exams!

  43. Nathan Wiles Avatar

    Wouldn't it be better instead of classes for TomatoSauce, Mozzarella, etc to just have methods with those names that return a reference to the tempPizza? Meaning the instantiation might look something like

    Pizza cheesePizza = new ToppingDecorator().addTomatoSauce().addMozzarella().getPizza();

    That seems much more clean to me.

  44. D.OriginaL Avatar

    does the pizza maker HAS to have the main (String[] args)

  45. Ksenia Burova Avatar

    You are the best! Come to teach at UTK

  46. İrem Ertürk Avatar

    rather than creating the Pizza object by nested new statements , is there a way of doing it with factory pattern?

  47. Junkzi Avatar

    The "Adding Dough" Part was cheated 😮 just try a pizza without moz 🙂

  48. Martin Kohl Avatar

    Great video on the Decorator pattern Derek! Loved it!

Leave a Reply

Your email address will not be published. Required fields are marked *