Decorator Design Pattern

  Рет қаралды 425,415

Derek Banas

Derek Banas

Күн бұрын

Get the Cheat Sheet: goo.gl/MzW0g
Best Design Patterns Book : goo.gl/W0wyie
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.

Пікірлер: 567
@derekbanas
@derekbanas 5 жыл бұрын
Learn in One Videos for Every Programming Language Subscribe to Bookmark them: bit.ly/2FWQZTx C++ : kzbin.info/www/bejne/iKbFXn2pn7Kbjbs Python : kzbin.info/www/bejne/hGXQdq18ec-kqqM Java : kzbin.info/www/bejne/pF7bcqSZd6iqqZY PHP : kzbin.info/www/bejne/bYWpYWOef65qbrs MySQL : kzbin.info/www/bejne/r4HYZ6SMasesq5Y JavaScript : kzbin.info/www/bejne/nJvYapyfbb6mfdU C# : kzbin.info/www/bejne/oprWmqqLj6-Lp7M HTML5 : kzbin.info/www/bejne/oXXce4FtprZ4iqM CSS3 : kzbin.info/www/bejne/eYbbeZKojLh8Z80 JQuery : kzbin.info/www/bejne/eIi7mJp4YrlkoLM TypeScript : kzbin.info/www/bejne/Y4G1kIunjJ59gLc ECMAScript : kzbin.info/www/bejne/gJLOoJxmfJ2feMk Swift : kzbin.info/www/bejne/mnzEoJ2Fj5KUsJo R : kzbin.info/www/bejne/qWSpoK2Mm8lqp5Y Haskell : kzbin.info/www/bejne/ZmPCeWaCn9aAqJo Handlebars : kzbin.info/www/bejne/annYcqGDa8dlmpo Bootstrap : kzbin.info/www/bejne/naKydqKLh5qFfsk Rust : kzbin.info/www/bejne/i2Kod5p5g7F3eJo Matlab : kzbin.info/www/bejne/hIS2hZ6hgLeUmKs Arduino : kzbin.info/www/bejne/h4DCe5-wZtajetk Crystal : kzbin.info/www/bejne/eqmpgWCNn9anqcU Emacs : kzbin.info/www/bejne/f5LKk6lvbJl6orM Clojure : kzbin.info/www/bejne/mZqqqnuhedqDd6c Shell : kzbin.info/www/bejne/nqjVn6CHqtKnhtk Perl : kzbin.info/www/bejne/jXbKmXyOqJ15bMU Perl6 : kzbin.info/www/bejne/omHdgaqenLyHna8 Elixir : kzbin.info/www/bejne/pnOxgJSsh9SBgpI D : kzbin.info/www/bejne/qKi9d4ekm56Verc Fortran : kzbin.info/www/bejne/lZCVhpp8g76anJo LaTeX : kzbin.info/www/bejne/jJnQnH-ohM9_qdk F# : kzbin.info/www/bejne/mWjIf3eAg5xobrc Kotlin : kzbin.info/www/bejne/fpDSeJxuqtp3pqM Erlang : kzbin.info/www/bejne/f3bLqJZoppatfZY Groovy : kzbin.info/www/bejne/eGqbm5Zuncmob8k Scala : kzbin.info/www/bejne/equppWOPoL-ibq8 Lua : kzbin.info/www/bejne/n37ElKuQhrKDjtU Ruby : kzbin.info/www/bejne/epvManSCeMyZpq8 Go : kzbin.info/www/bejne/eXechGeHj9qJaZI Objective C : kzbin.info/www/bejne/a5bWgqSQfq9rack Prolog : kzbin.info/www/bejne/iarOqYqme9yAfdU LISP : kzbin.info/www/bejne/r562ometfdekr7c Express : kzbin.info/www/bejne/rnWmfJaEd6umq6s Jade : kzbin.info/www/bejne/omakiZaOdrVnqJo Sass : kzbin.info/www/bejne/rauWnHiigcd4fqc
@yuxiwan8917
@yuxiwan8917 6 жыл бұрын
Move the "adding dough" to plain pizza class then the video is perfect!
@derekbanas
@derekbanas 12 жыл бұрын
Thank you :) I'm working from 6 different books to make this tutorial. I'll cover everything in the GOF book, but probably in completely different ways. I'm glad you are enjoying the tutorial
@skkar2k2
@skkar2k2 6 жыл бұрын
Bro, trust me, never find such great tutor where I can understand the design pattern so easily. You are the champ dude.
@derekbanas
@derekbanas 6 жыл бұрын
Thank you for the nice compliment :)
@NZ255
@NZ255 9 жыл бұрын
Man I'm hungry
@derekbanas
@derekbanas 9 жыл бұрын
+NZ255 That's funny :)
@MojSoltani
@MojSoltani 11 жыл бұрын
you're the master; never has anyone or any article or tutorial hit home design patterns to the point in such short and concise manner.
@derekbanas
@derekbanas 11 жыл бұрын
Thank you :) I'm glad you enjoyed the videos.
@tautvydaskersulis7372
@tautvydaskersulis7372 4 жыл бұрын
Thanks, it's a great video explaining the concept very well. One thing that bothers me about commonly used patterns in Java is that they are often un-intuitive and do not reflect the real world as well as they could. I think the Decorator pattern is one of these. For example at 12:00 you have this line: ``` Pizza basicPizza = new TomatoSauce(new Mozzarella(new PlainPizza())); ``` I would intuitively interpret as making Mozzarella from PlainPizza and then making TomatoSauce from Mozzarella. The example below achieves the same results without decomposition and with a lot less boiler plate code, just by changing the mental model to something more intuitive and representative of real life. Are there any advantages to using the Decorator Pattern in situations like this? ``` List toppings = ArrayList(); toppings.add(new Topping("TomatoSauce", 0.35)); toppings.add(new Topping("Mozzarella", 0.5)); ToppedPizza ready = new ToppedPizza(new PlainPizza(), toppings); ``` I'm sure there are cases where Decorators are needed but I often see programmers using certain patterns to fit the solution into the their mental model when they should be reframing their mental model to match real life. What are your thoughts / experiences regarding this?
@MidasButSilver
@MidasButSilver 2 жыл бұрын
Late for a reply here, but in this case, yes, you are absolutely right. For such a simple example, a list would have done the job just fine. Its about always using the correct tool for the job and if the code was something more complicated you cannot simply rely on something like a list or array anymore. Say for example you are designing more complicated behaviour, for example enemies in a video game. Now each enemy could be different in some way: some are ranged fighters, others melee and the last group can do both, depending on the situation. You could change behaviour of each of them dynamically just with the corresponding decorator. Also in that situation a simple list just wouldn't quite do and probably fail miserably.
@derekbanas
@derekbanas 11 жыл бұрын
I'm happy they have been able to help you :) I decided a long time ago that I wanted to make long videos, but I also wanted them to be interesting and keep the viewers attention. I'm starting to really like the final results as of late. It has been very fulfilling to work at improving my teaching style. Thanks for giving the videos a look!
@RaiyanMonsur
@RaiyanMonsur 2 жыл бұрын
You've been a huge help!
@TheRedNaxela
@TheRedNaxela 6 жыл бұрын
learning for my software engineering course for university right now and have to learn all of these by heart and these videos are very helpfull thank you
@derekbanas
@derekbanas 6 жыл бұрын
I'm happy they were helpful :)
@alltheway99
@alltheway99 11 жыл бұрын
are you sure you wanna have "Adding dough" in Mozarella class ? (10:00)
@budguesor9222
@budguesor9222 3 жыл бұрын
Rightly pointed out. It should be present in the constructor of the PlainPizza
@mohanadalaa807
@mohanadalaa807 2 ай бұрын
@@budguesor9222 nope by leaving it in the constructor of plain pizza it would be printed every time the super constructor is called
@derekbanas
@derekbanas 12 жыл бұрын
Thank you, but you don't have to do that. I'm not one of the guys that asks people to like and favorite everything. The fact that you find them useful is enough for me :)
@bestof1506
@bestof1506 5 жыл бұрын
He sounds a little bit like Andy from The Office (Ed Helms)
@jandeman8410
@jandeman8410 6 жыл бұрын
I keep rewatching these series of design patterns in java. So far it helped me make my apps less violent and less complicated. Also, I love pizza! My thanks for your effort Derek. Kind Regards...
@derekbanas
@derekbanas 6 жыл бұрын
I'm very happy to hear they helped :)
@grandisukruth
@grandisukruth 11 жыл бұрын
First of all thank you very much. This is the best design pattern tutorials i have ever seen. We demand some more stuff from you in future :).
@derekbanas
@derekbanas 11 жыл бұрын
You're very welcome :) Thank you for checking them out
@constantinebindas5445
@constantinebindas5445 11 жыл бұрын
Every video from this course helps me to obtain clear understanding of dedicated pattern. Nice work! One remark about Decorator Pattern. You've forgot to implement decorators for meat stuff and olives to obtain perfect pizza :)
@derekbanas
@derekbanas 11 жыл бұрын
Thank you very much :) It is very nice of you to say that!
@derekbanas
@derekbanas 11 жыл бұрын
Thank you very much :) I love covering niche topics that others ignore
@derekbanas
@derekbanas 12 жыл бұрын
You can always override the PlainPizza class, add a topping of medium, large, Xtra large, etc. You could have all toppings have the same price and then just override the description. The rule extend what differs between toppings will steer you in the right direction
@PopeSweetJezus
@PopeSweetJezus 11 жыл бұрын
This video is 1000 times better than how my teacher tried to explain it to us, why thanks ;)
@derekbanas
@derekbanas 12 жыл бұрын
Thank you :) I like covering niche topics that everyone else avoids
@vigneshsoap123
@vigneshsoap123 8 жыл бұрын
Awesome. Much better explanation than HeadFirstDesignPatterns. Hats off.
@derekbanas
@derekbanas 11 жыл бұрын
You're very welcome :) Yes you could do what you described. It is really my fault because I didn't make an example that required the added flexibility this pattern provides. I tried to come up with a simple example of the pattern and in doing so I didn't get the best example. Sorry about that
@derekbanas
@derekbanas 11 жыл бұрын
You're very welcome :) I'm happy they are helping
@IronMeDen1
@IronMeDen1 2 жыл бұрын
10 years later and this video is still one of the easiest to follow and best videos on a design pattern I've seen so far! Thank you!
@derekbanas
@derekbanas 2 жыл бұрын
Thank you for taking the time to tell me you like them :)
@derekbanas
@derekbanas 11 жыл бұрын
Thank you :) Im very happy that you enjoyed it
@derekbanas
@derekbanas 12 жыл бұрын
Sadly anyone can sue you for anything and win whether they are right or wrong. It all depends on if they can wait you out with lawyers. I constantly fight against record labels that make false copyright claims on music I am licensed to use. I've received cease and desist letters for articles I wrote about false claims by web gurus. You can either fight them or lawyer up :(
@ayoubaid5296
@ayoubaid5296 11 жыл бұрын
My Best channel to learn OOP and DP Great job guys
@derekbanas
@derekbanas 11 жыл бұрын
Thank you :)
@sealwithawkwardness3951
@sealwithawkwardness3951 5 жыл бұрын
For those who don't know about SOLID principles, I recommend reading up on it. "Clean Architecture" by Robert Martin is a really good resource for this but you can just Wiki it. This design pattern is very powerful in that it directly tries to comply with the S(single responsibility) and O(open/close) of SOLID.
@derekbanas
@derekbanas 12 жыл бұрын
Sorry, but I'm not sure what you are trying to do. Send me a message on youtube, or and email derekbanas@verizon.net
@stevenfox9695
@stevenfox9695 9 жыл бұрын
Your videos are so great! I always recommend them to my friends taking the Software Design Pattern class at my university. Well done!
@herp_derpingson
@herp_derpingson 12 жыл бұрын
"Concrete Pizza" Sounds Delicious ;) 5:01
@TehPobo
@TehPobo 4 жыл бұрын
how was it
@herp_derpingson
@herp_derpingson 4 жыл бұрын
@@TehPobo I dont remember it was 8 years ago.
@andrewchavarria9227
@andrewchavarria9227 4 жыл бұрын
@@herp_derpingson I think I'd remember if I ate a concrete pizza
@derekbanas
@derekbanas 11 жыл бұрын
Thank you for taking the time to tell me I was able to help :) That is very much appreciated!
@derekbanas
@derekbanas 11 жыл бұрын
Yes that name sounds better. I'm not sure why I did that? I do most of this coding out of my head so sometimes I make weird decisions when naming things
@yukimushu
@yukimushu 11 жыл бұрын
Fantastic tutorial, very well presented and easy to understand. Wish more tutorials were as high quality as this one! :)
@dantheman52420
@dantheman52420 8 жыл бұрын
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.
@derekbanas
@derekbanas 8 жыл бұрын
I'm very happy that I could help :)
@0x28
@0x28 8 жыл бұрын
That is quite bizarre. Every B.S. in C.S. should cover at least 15 of the 23 patterns like in my degree. We had the GOF book and the companion book Design Patterns Explained, with an additional syllabus or 3.
@dantheman52420
@dantheman52420 8 жыл бұрын
Congrats dude
@rohanofelvenpower5566
@rohanofelvenpower5566 6 жыл бұрын
haha, MDX london here.2nd year we did Java. We didn't cover these. Just basic code and UML with related things like OOP thinking.
@facundostratocaster356
@facundostratocaster356 5 жыл бұрын
Awsome tutorials, I'm preparing for my exam with your videos!! The examples are excellent
@derekbanas
@derekbanas 5 жыл бұрын
Thank you :) I wish you the best on your exams
@MrMalcovic
@MrMalcovic 7 жыл бұрын
The UML association in the diagram is *composition*, not aggregation. Aggregation uses an empty diamond.
@carlmoller807
@carlmoller807 5 жыл бұрын
Also, the Mozarella class extends ToppingDecorator. The ToppingDecorator does not extend Mozarella as the UML shows.
@derekbanas
@derekbanas 11 жыл бұрын
You're very welcome :)
@derekbanas
@derekbanas 11 жыл бұрын
I probably could have come up with a more specific optimized way to present this pattern. The ToppingDecorator could have been more simplified and then the ingredients could have done most anything. In videos I have to find an example that both demonstrates the pattern while keeping everything as simple as possible. Sometimes I get a great example and other times not so much. Sorry about that
@derekbanas
@derekbanas 12 жыл бұрын
Sure I will cover the top 23 patterns described by the GOF
@derekbanas
@derekbanas 11 жыл бұрын
Thank you :) i did my best with them. I found an answer for your question that I agree with here: stackoverflow. com/questions/2166632/can-you-remove-a-decorator
@derekbanas
@derekbanas 11 жыл бұрын
That's funny :) I'm glad you liked it
@derekbanas
@derekbanas 11 жыл бұрын
You're very welcome :) I'm doing my best to cover the more complicated topics in programming. I'm glad people are finding them useful
@derekbanas
@derekbanas 11 жыл бұрын
Yes I slipped up a bit. Thanks for pointing that out
@FabianLema
@FabianLema 10 жыл бұрын
Shouldn't the arrow in mozzarella be pointing the other way around since Mozzarella extends ToppingDecorator? (in the UML) Regardless, kudos on your videos Derek! I'm learning more here than in lecture lol
@lobsterking747
@lobsterking747 4 жыл бұрын
Kept it simple and easy to understand. Good Format and Great Job
@derekbanas
@derekbanas 4 жыл бұрын
Thank you :)
@MBadAtGuitar
@MBadAtGuitar 11 жыл бұрын
Thanks for ur efforts to decorate a basic programmers life with your Design patterns.:) Have a doubt on the following line: new TomatoSauce(new Mozzarella(new PlainPizza())); Why do we pass Mozzarella object to TomatoSauce constructor while TomatoSauce actually expects a Pizza object ? would be great if you can explain. Thanks from India..
@derekbanas
@derekbanas 11 жыл бұрын
I took a short cut. You are correct that I should have defined a Dough class like I did with TomatoSauce. I was just being a bit lazy. Sorry about that
@derekbanas
@derekbanas 11 жыл бұрын
So that it can't be instantiated
@iarbainaltainvantdevara
@iarbainaltainvantdevara 7 жыл бұрын
Enjoying the videos so far. However, I don't understand how is this different from the builder pattern?
@justgao
@justgao 4 жыл бұрын
Great video! When I think about creating a pizza class with many different kinds of toppings, I might choose the builder pattern instead, so that my code for adding toppings can be linearly written instead of in the form of nested constructors. Are there any advantages of the decorator pattern over the builder pattern here?
@ChaitanyaBhagwatChai
@ChaitanyaBhagwatChai 9 жыл бұрын
Hey Derek, i'm just going to reaffirm what others have been saying on your videos all along: thank you so much for this incredible resource for learning about design patterns. Your videos are of such incredible help while studying, both because of your excellent explanations, as well as the nature of the medium (we can replay a segment we don't understand however many times we want) and the commented code that you provide. I am a big believer of a future for internet with micro-transactions, so i just went and donated you 5 bucks. It is not much, i am a student after all, but the content you provide warrants a gesture from me :)
@derekbanas
@derekbanas 9 жыл бұрын
Chaitanya Bhagwat Showing your appreciation is more then enough for me. I'm very happy to be able to help like others have helped me in the past. Thank you for the compliment :) You're very welcome.
@emmanuellmiqueletti7029
@emmanuellmiqueletti7029 5 жыл бұрын
I love watching your videos at 1.5 speed. Idk why but it's funny.
@derekbanas
@derekbanas 5 жыл бұрын
That's fast! I think even I'd be confused
@FesliyanStudios
@FesliyanStudios 7 жыл бұрын
Why not just create methods in the super class that can add price, etc...? Just a newb asking.
@derekbanas
@derekbanas 7 жыл бұрын
This example doesn't require the pattern. I used a simple design so that I could focus on explaining the pattern in the easiest way possible.
@derekbanas
@derekbanas 12 жыл бұрын
Thank you :) I pay no attention to video count, views, or anything else. You guys seem to inform me on that stuff
@SelfDestructionWorldwide
@SelfDestructionWorldwide 5 жыл бұрын
Thank you very much. This video helped me a lot understanding this pattern
@derekbanas
@derekbanas 5 жыл бұрын
I'm happy I could help :)
@derekbanas
@derekbanas 11 жыл бұрын
I'm confused by your question. ToppingDecorator provides those methods to the classes that extend it
@derekbanas
@derekbanas 12 жыл бұрын
Thank you :) God bless you as well
@derekbanas
@derekbanas 11 жыл бұрын
Great! You're very welcome :)
@derekbanas
@derekbanas 11 жыл бұрын
I have all of the diagrams on my site. There is a link in the description for the video. I hope it helps
@malcomclark2261
@malcomclark2261 9 жыл бұрын
Headfirst design patterns explains things similarly but using drinks instead of pizza. You did a much better job of explaining it than the book did though.
@derekbanas
@derekbanas 9 жыл бұрын
Malcom Clark Thank you for the compliment :)
@tonyweak3842
@tonyweak3842 9 жыл бұрын
+Malcom Clark That's true! And HF DP is an excellent book for beginners, so this complement is even bigger. :-)
@nickzomchak9531
@nickzomchak9531 6 жыл бұрын
Hey Derek, why don't u just use List instead and just loop through stuff. I think the pizza example, as well as drinks one are not suitable for this pattern, cause they do not add any new behaviour, u can't do new things with pizza. even if you decorated it. Please correct me if I'm wrong.
@Messier_-82
@Messier_-82 6 жыл бұрын
You are right. This exact case is not suitable for Decorator Pattern. But this example shows well how the design works.
@derekbanas
@derekbanas 11 жыл бұрын
When I start thinking about using a design pattern I normally first ask if I can accomplish making the code more readable in some other way. If I can't think of another way I use the pattern. You'll find with experience that more often then not you'll use patterns rarely. The positives in knowing them are that you'll start to find other unique ways of solving problems while writing easier to understand code.
@0x28
@0x28 8 жыл бұрын
Abstraction (e.g. the Decorator pattern) is used everywhere in modern programming! In ReactJS, in TypeScript, in custom solutions..
@derekbanas
@derekbanas 12 жыл бұрын
You're very welcome :)
@programity
@programity 11 жыл бұрын
Thanks! There are a few very good answers in that.
@derekbanas
@derekbanas 11 жыл бұрын
That's funny :) Thank you for the compliment. I'm glad you are enjoying them.
@nielslindeboom9452
@nielslindeboom9452 9 жыл бұрын
Great video, but I am wondering why are you using such a long parameter in the constructor in the last part of the video?
@derekbanas
@derekbanas 9 жыл бұрын
+Niels Lindeboom Thank you :) I write these largely out of my head so sometimes I do silly things.
@derekbanas
@derekbanas 11 жыл бұрын
Thank you :) I do my best to cover complicated topics in easy to understand ways. I'm glad you liked it
@Mr8lacklp
@Mr8lacklp 8 жыл бұрын
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.
@rf8754
@rf8754 8 жыл бұрын
An ArrayList is not only easier to implement but also makes more sense, this pattern is something that nobody uses in real life, it just adds more complexity without a real need.
@Mr8lacklp
@Mr8lacklp 8 жыл бұрын
Ramon Oliveira Good to know
@arctan64
@arctan64 8 жыл бұрын
This is completely wrong. The decorator pattern is absolutely used. Refer to Java I/O Streams classes for an example. This video uses a very simple example, so simple that the true value of the decorator pattern is hard to get across.
@0x28
@0x28 8 жыл бұрын
There are so many things wrong with this idea. Just because something is easy, doesn't mean it's the best way to do it. What if we wanted a new topping that consists of two other toppings? We would have to modify the Topping class or create an entirely new Topping class just to meet that requirement, resulting in a mass class hierarchy. This way, all of your classes are static, there is no flexibility of OO design and it's one of the reasons the Composition design principle holds true. Your classes are stuck at compile time and there are no runtime dynamics. This guy explains it in the video quite clearly. You are also violating the Single Responsibility Principle: a pizza should not have to know about toppings. Looping through an ArrayList is an incredibly inefficient way to implement this. For every topping, you would have to traverse the ArrayList. Not only that, but now Pizza is tightly coupled to Topping. Additionally, ArrayLists are terrible at sorting and searching, inserting an deleting because the underlying algorithms are of O(n) complexity, which as we all know is terribly inefficient. You can find the explanation for that here: stackoverflow.com/questions/2307283/what-does-olog-n-mean-exactly Not only is the loop inefficient, but you are asking the JVM to traverse a complex object graph each time, which adds to the inefficiency. This will destroy performance and it is an absolutely terrible idea.
@ChristopherOkhravi
@ChristopherOkhravi 7 жыл бұрын
I completely agree with previous commenters in that while the video does convey the pattern nicely it is *unsuitable for the underlying problem*. The example is essentially the same as the one given in Head First: Design Patterns which I too find unsuitable. 1) I don't think you have to get "stuck" on the fact that Ramon suggested ArrayList. The point seems to be that: instead of wrapping a plain pizza in a topping which is wrapped in a topping which is wrapped in a topping etc. you simply pass a *collection* (of your choice, suitable to your scenario which means that performance should not be an issue) of toppings into a Pizza instance (using dependency injection). 2) Even the fact that there are two base types of pizza bugs me. Again, the example is simple and conveys the point but I don't believe it should be taken literally. The crust type could also be extracted to it's own class and passed as a dependency into the Pizza class. Isn't that more suitable than trying wrap Topping decorated Pizzas in Crust decorators? 3) "Pizzas should not have to know about [concrete] toppings". Yes, this can be true regardless of whether you use simple composition or decorator pattern. If you use composition then you simply dependency inject toppings. In the decorator example pizzas not only doesn't know about concrete toppings, it actually doesn't know about the concept of toppings at all which is good. However, I would actually argue that the Toppings aren't actually Toppings in this case at all, they are PizzaDecorators and thus by definition HAS and IS a Pizza. But Toppings are not Pizzas, they are Toppings. 4) This is where I think the crux of the matter lies. If you look at the previously mentioned example of Java IO using decorator pattern, you see Streams decorated with Streams. Here Pizzas are being decorated with Toppings. That's not the same thing. The example is unsuitable for decorator pattern because we're asking Toppings to behave like Pizzas so regarding SRP I would rather say that this is to violate SRP. 5) E.g. notice how difficult it would be to change the way the list of condiments are printed because the commas are hard-coded in Toppings. Is that really a concern suitable for a topping? Shouldn't a topping describe the properties of a topping (such as it's price and name) rather than the way you aggregate it? Collections are a group of abstractions that helps us separate the properties of things from the way we aggregate over the things (think: iterator pattern). Thus: the suggestion to use ArrayList. The type of collection suitable of course depends on the queries you need to run. Depending on what queries you need you should be able to get either O(1) or O(n) for common queries. With Decorator Pattern (as used here) you're stuck with O(n) because its essentially linear search in a linked list. The point is that if you separate the structure of the collection from the items you can change the mode of iteration (i.e. the structure of the collection) without having to change the items. Here you can't. 6) Anyways I need to get back to work but I really don't think this example should be taken literally :) P.s. Derek Banas, thank you very much for your great efforts. Your videos helped me tremendously.
@BigCreep
@BigCreep 12 жыл бұрын
Excellent tutorial! I have a few questions... What if you wanted to make the cost of the toppings the same? Or does that defeat the purpose of this pattern? I might be taking the example too literally, but usually you charge the same for each topping. If I offered 30 different toppings and wanted to change them from .75 to 1.25, would I have to change each object? Also, what if you had 3 sizes and wanted to charge more for toppings on Larges, for example. Could you still use this pattern?
@derekbanas
@derekbanas 12 жыл бұрын
I'm just happy that you find it useful :) Thanks for the kind words. It is just a fun hobby of mine and I expect nothing in return
@derekbanas
@derekbanas 11 жыл бұрын
It is very nice to be able to clear up a subject when others haven't helped! Thank you :)
@execdd17
@execdd17 10 жыл бұрын
"An infinite number of different types of pizza". I think you're on to something here...
@derekbanas
@derekbanas 10 жыл бұрын
Alexander Vanadio That's funny :) I'm up for turning that into a prototype.
@derekbanas
@derekbanas 12 жыл бұрын
Thank you :)
@caiofelipe2117
@caiofelipe2117 11 жыл бұрын
I got an question , is exists a way for using not constant values? for example , Will I need to open the source for changing if the tomato sauce increase for .60 ? Could I use attributes instead of constant values?
@sohedulislam1652
@sohedulislam1652 2 жыл бұрын
If I need to pass different Data type arguments or different return type in same method name with in different classes. Suppose: If I need to pass integer value into the getCost method in BasicPizza class and float type value into the same getCost method in Mozzarella class which also uses the getCost method from BasicPizza class. Then how should implement this or which pattern should follow?
@GhostNappa28
@GhostNappa28 8 жыл бұрын
Loved this video! You are doing a great job. Love the way you present it so calm and composed. :) BTW.. I feel that the statement "Adding Dough" should not be inside Mozarella but in your plain pizza :)
@derekbanas
@derekbanas 8 жыл бұрын
+Kaushik Sundar Thank you :) Yes I think you are correct. I'm not sure why I did it that way. I wrote the code out of my head.
@derekbanas
@derekbanas 12 жыл бұрын
Thank you :) I did my best and I'm glad you liked it
@shyamswaroop384
@shyamswaroop384 3 жыл бұрын
At 5:02, that arrow represents composition, not aggregation!
@2006wal
@2006wal 3 жыл бұрын
Hallo Thank you so much ! i think you have to change the methods in ToppingDecorator getDescription () and getCost () to abstracts, otherweise they haven't any meaning
@html2664
@html2664 10 жыл бұрын
Amazing! I'm from Ukraine, but your tutorial in English is easier than any of I could find in native language! Thanks!
@derekbanas
@derekbanas 10 жыл бұрын
Thank you for the compliment :)
@vincent9563
@vincent9563 7 жыл бұрын
Why don't you answer some questions on your comments?
@derekbanas
@derekbanas 11 жыл бұрын
Thank you :) I'm very happy that I'm able to help.
@sk8eradu
@sk8eradu 11 жыл бұрын
All this coding about pizzas makes me hungry. Thanks for the lesson! :)
@derekbanas
@derekbanas 11 жыл бұрын
You are very kind :) You're very welcome
@derekbanas
@derekbanas 11 жыл бұрын
That's funny :) You're very welcome
@hassankhallouf9391
@hassankhallouf9391 6 жыл бұрын
Great Videos man, thanks, really helpful I just wanted to point out that although this video illustrates the patter perfectly but I don't think this is how I would use this pattern Decorator pattern should be as you said, a way to add functionality to a class dynamically, but the analogy of adding toppings to a pizza isn't really fitting in my brain, like the line where you typed Pizza p = new TomatoTopping() really hurts my brian xD
@derekbanas
@derekbanas 6 жыл бұрын
Thank you :) Yes I was to focused on trying to keep it simple rather then making a better example I agree
@ackpacket
@ackpacket 9 жыл бұрын
Why not have Mozarella, Tomato Sauce, Pepperoni all inherit from an Ingredient class, and have the pizza class maintain a list of Ingredient objects?
@Metallian2112
@Metallian2112 8 жыл бұрын
Because the purpose of the video is to teach the decorator pattern.
@sumantkumar2887
@sumantkumar2887 Жыл бұрын
Amazing explanation, Thak you!!
@derekbanas
@derekbanas 11 жыл бұрын
Thank you :) it was fun to cover this topic
@derekbanas
@derekbanas 11 жыл бұрын
You're very welcome :) Im glad you enjoyed it
@Fork501
@Fork501 6 жыл бұрын
I thought Decorator was when you implement the base class, but then add new methods. Is that another way to do it, or am I thinking of something else? For example, in ToppingDecorator, you would inherit from Pizza then implement getDescription and getCost, but then have a method for addTopping, which wasn't found in Pizza.
5 жыл бұрын
great explanation with good example, thanks for tutorial . proud of you for all your videos:)
@derekbanas
@derekbanas 5 жыл бұрын
Thank you I'm happy you enjoy them :)
@derekbanas
@derekbanas 12 жыл бұрын
Thank you for watching :) I'm glad to help
@jinizzraeel7705
@jinizzraeel7705 9 жыл бұрын
This pattern is really useful.
@derekbanas
@derekbanas 9 жыл бұрын
Jin Izzraeel Yes it is pretty cool :)
@kaanapan1183
@kaanapan1183 3 жыл бұрын
Why? Why do you have a video about anything I look for? I love you bro.
@prajaktaupadhye1791
@prajaktaupadhye1791 9 жыл бұрын
Thank you :).I am follower of of your videos it is helpful in understanding concept.
@derekbanas
@derekbanas 9 жыл бұрын
Prajakta Upadhye I'm glad I could help :) Thank you
@derekbanas
@derekbanas 12 жыл бұрын
Thank you :) I'm glad you enjoy them
@alltheway99
@alltheway99 11 жыл бұрын
thaks again :-) you are a good lecturer :))
@derekbanas
@derekbanas 12 жыл бұрын
That's funny :)
Command Design Pattern
23:41
Derek Banas
Рет қаралды 283 М.
The Most Important Design Pattern in React
35:04
Cosden Solutions
Рет қаралды 60 М.
小路飞嫁祸姐姐搞破坏 #路飞#海贼王
00:45
路飞与唐舞桐
Рет қаралды 12 МЛН
The joker favorite#joker  #shorts
00:15
Untitled Joker
Рет қаралды 30 МЛН
Help Me Celebrate! 😍🙏
00:35
Alan Chikin Chow
Рет қаралды 58 МЛН
Decorator Pattern - Design Patterns (ep 3)
54:35
Christopher Okhravi
Рет қаралды 484 М.
The Smart Way of Using the Decorator Pattern in C#
12:37
Nick Chapsas
Рет қаралды 69 М.
Simplest Decorator Design Pattern in C#
10:55
Code Radiance
Рет қаралды 25 М.
What is the Decorator Pattern? (Software Design Patterns)
19:41
Be A Better Dev
Рет қаралды 26 М.
The State Design Pattern in Python Explained
19:14
ArjanCodes
Рет қаралды 76 М.
10 Design Patterns Explained in 10 Minutes
11:04
Fireship
Рет қаралды 2,3 МЛН
Strategy Design Pattern
11:32
Derek Banas
Рет қаралды 749 М.
Dependency Injection, The Best Pattern
13:16
CodeAesthetic
Рет қаралды 831 М.
小路飞嫁祸姐姐搞破坏 #路飞#海贼王
00:45
路飞与唐舞桐
Рет қаралды 12 МЛН