Introduction to the Decorator Design Pattern
A free video tutorial from Paulo Dichone | Android, Java, Flutter Developer and Teacher
Software Engineer - Android, Java, Flutter, Dart and Teacher
4.4 instructor rating • 13 courses • 115,077 students
Learn more from the full courseJava Design Patterns: The Complete Masterclass - 21 Patterns
Master Java Design Patterns and Write Robust and Extensible Software - MVC, Observable, Singleton, Facade, Builder...
09:50:47 of on-demand video • Updated February 2020
- Master Java Design Patterns
- Apply Design Patterns to Real-World Projects
- Build Software that's Robust and Flexible Using Java Design Patterns
- Solve common Software architecture problems with Java Design Patterns
English [Auto] This really we'll tackle the decorator pattern decorator design pattern as always. Let's look at an example so that we can actually see why decorator pattern very useful for us. All right. Suppose we have a an ice cream shop right where we create icecream so we have these higher tea in classes here so we have the ice cream class that creates ice cream of course so we are abstracting out all of the other methods that come with the X-wing right so we're just focusing on one which is the cost. So. So we have our ice cream class and we create different kinds of ice when we have vanilla ice cream we have chocolate ice cream mint ice cream and so forth. All is good. But the problem now here is that we are just bound by inheritance and as we've been discussing before inheritance can be very limiting in so many ways and you will see why in a second suppose that we decide our boss or owner or shop owner owner decided that we need to add for instance vanilla ice cream with mint. OK so we have side Menthe and one side we have ice cream so we have to create another class and that class will have to inherit from ice cream which means we have to override the cost because now we have two costs. Right. Or vanilla ice cream and mint. So this is how things will start looking like. So the more we added the more flavors the more types of ice cream and the messier it gets. All right. And the fur business keeps booming and our boss gets so excited and he wants us to keep adding more flavors of different ice creams. Then you notice that this is not a good strategy. All right. First of all each time we create these ice cream objects we're using inheritance when creating new objects in the memory and all of them are not being created at runtime which means we are lagging and it's just not very cost effective. This is where we look at the decorator design pattern. So what the decorator design pattern will do will do this right. We will make sure that each of the ice creams that we add to our menu they will be created at runtime. And plus we will decouple we will remove the dependency that we may have with the direct inheritance that we had before. We've seen in the previous lectures in the previous in the previous slides. So what are we going to have here we're just going to create an interface again call this ice cream and then we're going to create a basic Ice-Cream class which will of course implemented the cost that we have at the top here from our eyes from interface. And we're going to instantiate our basic Ice-Cream constructor of course as well. But then we will create will separate everything also by adding in the ice room decorator. What these guys will implement. Also the ice cream interface and we will pass the notes we'll pass the eye screen interface as well in the constructor. So using the composition here Jaba composition right. So we don't have to initialize everything. So all of this is going to be done at runtime and then all the concrete classes such as chocolate ice cream mint ice cream pecan ice cream or whatever other combinations that we may come up with it will be much easier now because we are just going to use the ice cream decorator as opposed to have a direct connection with the ice. Now we have abstract we have organize our code in such a way that everything is going to be done at runtime and we are not bound by the direct inheritance that we had in previous slides. Now it's much cleaner we can add as many many as many flavors as we possibly can and will be safe at all times. And the code is way cleaner as well. So in next video we're going to do we are going to go into code and create this dependency that we call and create this hierarchy. This dependency here using the decorator design pattern. I hope you're excited and I'll see you in the next video.