Observer - removing events
In the last lesson we created an event dispatcher. In this lecture, we will add the functionality to remove events as well.
Video Transcript
00:00In the last lecture, we created an observer,
00:03an event dispatcher,
00:04but we still need to implement removing events.
00:08Literally, whenever we wanna have an event stop,
00:11then we need to have a way to stop them.
00:13Beyond that, it's also really important to remove events
00:16because it will help you manage your memory.
00:19If you're not removing events,
00:20references to the different objects
00:22are gonna always stay there
00:23because they're stored inside of that array
00:26as you saw in the last lecture.
00:29So let's go ahead and continue implementing
00:31a fully functional event dispatcher.
00:35So we're still working on our event dispatcher decorator,
00:38which is not only a decorator,
00:39but it is also the observer design pattern.
00:42We created already an add event,
00:44an auto a dispatch event,
00:46but we're missing also a mechanism to remove events.
00:50And I'm gonna go ahead and create that
00:51by creating the remove event function method.
00:58Now, first property is gonna be type,
00:59the second is gonna be listener,
01:02exactly the same way as it was for our adding of events.
01:05The next step is we, this time around,
01:07instead of creating a new list,
01:09I just wanna check to see if the list
01:11of that specific type exists.
01:13And only in that case,
01:14then I'm gonna go ahead to the next step,
01:16which is gonna be to try to figure out
01:18is there actually something in it?
01:20So even before that, I'm gonna go ahead here
01:23and I'm gonna go ahead here and just save here
01:26that array so we don't type your list type 100 times
01:29to make it just a tad bit cleaner.
01:33And let's set that a,
01:35and let's push our screen a bit up
01:37so we could see better.
01:38Okay, so now that we have that,
01:40I wanna basically approach that array
01:41and find out if it's inside of that index of,
01:46is that listener inside of that index,
01:48very similar to the way we've done it previously.
01:51Only in this case, if that index is minus one,
01:54that means it doesn't exist,
01:55we don't have to do anything.
01:56So if our index is only larger than minus one
01:59and only in that case,
02:00then there's something for us to do.
02:02What do we wanna do?
02:02We wanna basically splice it out,
02:04taking that index item and removing it from our array,
02:09getting rid of it,
02:10making sure that it doesn't exist anymore.
02:12So let's test things out.
02:14If I go back into my application where I added events,
02:17I'm gonna go ahead and just remove that exact same event
02:20with that function that I saved with the reference,
02:22making sure that I have the same signature.
02:25I'm gonna go ahead and remove here.
02:27And by removing this event,
02:28I shouldn't get the event to run at all
02:30because the dispatment happens only after
02:33we removed the event.
02:34So let's go ahead and test it out.
02:36And if I click here on refresh,
02:37we'll see where only it's over,
02:39we're not getting that second function
02:42which has function number two, it's over two,
02:45that doesn't have it.
02:46If I comment it out, you'll see that now it's back in.
02:50All right, so that's it.
02:51We've just created together the observer design pattern,
02:54a fully integrated one, which we have an add event,
02:57we have a remove event,
02:58and we even have a dispatment event
03:00where all of them together are kinda needed
03:02to be able to be functional.
03:04We wrapped it around inside of a decorator,
03:06making it very easy for us to decide
03:08who's gonna have those functionalities.
03:10Very easily, we could reuse this over and over and over
03:13without actually putting it into every single function
03:16or needing to use inheritance for this in this scenario.
03:20Okay, so with that said, by the way,
03:22we could use this decorator and decorate,
03:25we could decorate even a prototype,
03:27adding it into every single shape
03:29if we added and decorate our singleton.
03:32So with that said, what I wanna do next is nothing.
03:35I'm gonna leave it to you,
03:36but I wanna give you a homework assignment
03:38that I'm not gonna check,
03:39although you're more than welcome to send me
03:41if you have any questions.
03:43What I want you to do is I wanna go,
03:44you will go into our massive,
03:45large circle generator singleton.
03:48And I want you to figure out a way to make it somehow
03:54dispatching events and having events.
03:57Now, I'll tell you right away,
03:58you can't do it directly with the actual instance,
04:01so don't even try.
04:02What you could do is you could maybe set the shape factory
04:05to be it, or you could create a new object to do it
04:08and then expose those functions into our process
04:12where you're actually sending them back in the return.
04:15The steps involved are a little bit complicated,
04:17but every single thing that you need to do
04:20was covered throughout this training in general.
04:22And if you're capable of making this update,
04:24I think you could proudly say
04:27that you really know how to work with design patterns
04:29and you know your way around JavaScript.
04:33In this lecture,
04:34we completed implementing a fully functional event dispatcher,
04:38implementing the observer design pattern.
04:41In the next lecture,
04:42we're gonna meet a new design pattern
04:44called the state design pattern.
