Decorator Design Pattern
The decorator enables us to add functionality to an existing object without needing to create a new inheritance chain or update the prototype. The idea is to add functionality to items based on need instead of adding them to the object from the get-go. This can become very useful when you have multiple objects with partially shared functionality.
Video Transcript
00:00the last structural design pattern that we're going to meet in this section is
00:05the decorator design pattern. The decorator design pattern is quite unique
00:10and interesting. It is basically the concept of it is to add features to
00:15specific objects without creating a subclass or changing the original
00:20interface or constructor. The idea here is to add features to different objects
00:26without actually creating a class for them, without actually creating a prototype
00:31for them. This is not something you'd use very often but you would definitely use
00:36this very often in scenarios where you have a lot of objects that you're adding
00:40to the stage and randomly each one of them has different features. Some of them
00:44are selectable, some of them are tintable, some of them are whatever you want to
00:48give it. And in those scenarios instead of creating a hundred different classes
00:53well it would be a lot easier to just create different functionality
00:56decorators that would then add functionality which is really natively
01:00supported by JavaScript and incredibly powerful. So we're going to take a look at
01:04that now. This is going to wrap up part one of our structural design patterns. So
01:10let's get right into the decorator. Imagine that we have plenty of
01:16different constructs on our stage besides our red and blue and yellow
01:22rectangles. So what we want to do is we want to add functionality to specific
01:27elements. For example maybe we want the squares to be self-destructing and that's
01:32exactly where a decorator could become very useful when we don't want to build a
01:36whole new construct just for one added functionality that is so explicit and
01:41so personal. The idea behind it is basically to dynamically after an
01:46object was created to add to it some core functionality using a decorator.
01:50Let's see it in action it's really easy and actually really fun. So I'm going to
01:55go ahead right underneath the rectangle creation and I'm going to go ahead and
01:58create a new function which I'll call self-destruct decorator. Our self-destruct
02:06decorator is going to accept an object any type of object although in our case
02:10it really is expecting it to be an item of that is implementing a certain
02:16interface which is fine. In our case what I want to do is I want to first
02:20of all I want to go to that object and I want to add it a kill functionality
02:24which doesn't exist natively inside of our object currently and what I'm going
02:29to do is I'm going to go to the object to the item itself that's living within
02:32and literally remove it. Now this is something really powerful because I could
02:36then add functionality to specific items as I see fit as the need is
02:41needed instead of adding it to everyone which will be a little bit of an
02:44overkill or creating a subclass which would make our code a lot larger than it
02:49needs to be especially if we are going to create this functionality in a way
02:53that could be shared and used with a lot of different types of functions if every
02:57item has an item property and it's adhering to a certain interface. Alright
03:01and I'm going to go ahead and make this self destructor decorator a little bit
03:05smarter by also adding some actions to it so I'm going to go ahead and I'm
03:09going to go to that item I'm going to add a click event here click callback
03:13really more accurately and I'm going to just go ahead and literally just go
03:17ahead and call that object kill making sure that it's get it'll get killed once
03:23it's clicked on. Alright all that's left for us to do is to test things out but
03:26before we do we need to add that functionality of self destruct decorator
03:30and we're going to do that in our yep we're going to do that in our blue
03:36circle builder when we're adding that rectangle that yellow rectangle into our
03:41element so even before we continue onwards I'm just going to go ahead and
03:45just wrap that and just send my rectangle that I've just created to add
03:49to this specific element that functionality meaning that any other
03:52rectangle wouldn't have that unless it's living inside of the blue circle
03:56builder and then it would have that so I'm going to go ahead and save it click
03:59on refresh and if I click on refresh now and just go ahead and add a few
04:03circles with those rectangles if I click on it it will get removed and
04:07that's exactly what I wanted it to do. In this lecture we met the decorator
04:13and with it we're wrapping up this section but fear not we're not done with
04:17structural design patterns because in the next section we'll take a look at
04:21structural design patterns part two as we continue exploring different
04:26fantastic structural design patterns.
