Decorator Design Pattern Tutorial

Decorator Design Pattern TutorialWelcome 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.

If you like videos like this, it helps if you tell Google [googleplusone]

Sharing is nice [nttfblike]

Code from the Video

PIZZA.JAVA

THREECHEESEPIZZA.JAVA

PLAINPIZZA.JAVA

TOPPINGDECORATOR.JAVA

MOZZARELLA.JAVA

TOMATOSAUCE.JAVA

PIZZAMAKER.JAVA

17 thoughts on “Decorator Design Pattern Tutorial”

  1. Now I got the point, the ‘loop’ of the toppings stop because you will always have a PlainPizza that don’t extend ToppingDecorator. So it is like a list of elements(Decorators).

  2. Thanks for the video. I think it helped me get the idea.

    However, I’m wondering if the constructor for Mozzarella should say only:

    public Mozzarella(Pizza newPizza) {
    super(newPizza);
    System.out.println(“Adding Moz”);
    }

    Likewise, a constructor for PlainPizza should be added like so:

    public PlainPizza() {
    System.out.println(“Adding Dough”);
    }

    I think this works, because currently if you had a pizza with only tomato sauce it will say you have thin dough but it would never get added. The price for the dough does get added to the cost. Do you concur? If not, can you explain what I’m missing?

    Thanks,
    Ken

    1. Yes it could have been done that way and your way does make more sense since it really doesn’t make sense to have mozzarella do anything with the dough. I did it that way to limit the number of classes

  3. Hi.. Good one. Have a question though. Why “Adding Dough” is mentioned in Moz.java… Does that mean we are printing adding dough only when we are adding Moz right? So, if I just want Sauce and Dough, the output will not print Adding Dough but the cost could be 4.35$.

    PS: I didn’t tried out the program yet. Just got the doubt as you explain.

    1. The limitations are normally the same for each one. All to often people force them into code when they just make the code more complicated. That is the major skill that one must develop. It is just as important to not use them as it is to use them.

  4. Hi Derek,
    First of all, thanks a ton for making these tutorials, you have really helped me to get a better understanding of design patterns and when to use them.

    I have a small doubt though, Why is ToppingDecorator class implementing the interface Pizza, doesn’t it break the “Is A” principle for inheritance, after all ToppingDecorator itself is not a pizza. Can’t we just use aggregation to achieve the desired result.

    Thanks in advance !!

  5. hey i ve been a good fan of your videos, I’ve been using a tool called sublime text lately but it seriously lacks somewhere in code completion. if not sublime what would be the best for c++ development, i mean something very fast and efficient and low power consuming.

    1. Thank you 🙂 On OSX I use Code::Blocks. Some people really like XCode as well. On Windows Visual Studio is the best by far. Eclipse CDT is okay for both OSs. Code::Blocks works on Linux as well. I hope that helps.

Leave a Reply

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