From the course: JavaScript: Patterns

Unlock the full course today

Join today to access over 22,500 courses taught by industry experts or purchase this course individually.

Observer pattern

Observer pattern - JavaScript Tutorial

From the course: JavaScript: Patterns

Start my 1-month free trial

Observer pattern

- [Instructor] The Observer pattern is one where we maintain a list of objects based on events, and is typically done with updating data based on these events. It is implemented for example, with the subscribe methods in MeteorJS. Let's take a look at code to see how it looks like. So if you go into the exercise files, and then in chapter 4, and then video 04_02, you can drag and drop this folder directly into visuals to do code. And that's a good example of the observer pattern. So right now we created a class, so with a constructor we created that class for car. And inside of it we have a gas element, and we have a setter and then we have an observer. So basically we're able to set what is the gas level for that car, and then we can observe what is the level of that gas. There's a couple of functions here that allows us to register and then un-register an observer and then notify what is the gas level…

Contents