Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Observer Pattern | Behavioral Patterns
Quizzes & Challenges
Quizzes
Challenges
/
JavaScript Design Patterns

bookObserver Pattern

The Observer pattern is a foundational concept in event-driven JavaScript. It provides a way for objects, known as observers, to subscribe to and receive notifications from another object, called the subject or observable, when certain events occur. This pattern is highly relevant to JavaScript, where building interactive user interfaces and handling asynchronous events are common tasks. By decoupling the source of events from the code that responds to them, the Observer pattern makes your code more modular, reusable, and easier to maintain.

1234567891011121314151617181920212223242526272829303132333435363738394041
// A simple EventEmitter implementing the Observer pattern class EventEmitter { constructor() { this.events = {}; } // Subscribe to an event on(event, listener) { if (!this.events[event]) { this.events[event] = []; } this.events[event].push(listener); } // Emit an event emit(event, data) { if (this.events[event]) { this.events[event].forEach(listener => listener(data)); } } // Unsubscribe from an event off(event, listenerToRemove) { if (!this.events[event]) return; this.events[event] = this.events[event].filter( listener => listener !== listenerToRemove ); } } // Usage example const emitter = new EventEmitter(); function greet(name) { console.log(`Hello, ${name}!`); } emitter.on('greet', greet); emitter.emit('greet', 'Alice'); // Output: Hello, Alice! emitter.off('greet', greet); emitter.emit('greet', 'Bob'); // No output, as the listener was removed
copy

To see how the Observer pattern works in practice, consider the process step by step. First, you have a subject—in this case, the EventEmitter—that maintains a list of observers, or listeners, for different types of events. Observers subscribe to specific events by calling the on method, passing in the event name and a callback function. When the subject triggers an event using the emit method, it notifies all observers subscribed to that event, passing along any relevant data. Observers can also unsubscribe from events using the off method, which removes their callback from the subject's list. This subscription mechanism enables you to build flexible, event-driven systems where different parts of your application can respond to changes or actions without being tightly coupled to the source of those events.

1. The Observer pattern is useful for:

2. In the Observer pattern, what is the role of the observer?

question mark

The Observer pattern is useful for:

Select the correct answer

question mark

In the Observer pattern, what is the role of the observer?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 4. Capítulo 1

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

Suggested prompts:

Can you explain how the EventEmitter class works in more detail?

What are some real-world use cases for the Observer pattern in JavaScript?

How does the Observer pattern help with decoupling code?

Awesome!

Completion rate improved to 7.14

bookObserver Pattern

Deslize para mostrar o menu

The Observer pattern is a foundational concept in event-driven JavaScript. It provides a way for objects, known as observers, to subscribe to and receive notifications from another object, called the subject or observable, when certain events occur. This pattern is highly relevant to JavaScript, where building interactive user interfaces and handling asynchronous events are common tasks. By decoupling the source of events from the code that responds to them, the Observer pattern makes your code more modular, reusable, and easier to maintain.

1234567891011121314151617181920212223242526272829303132333435363738394041
// A simple EventEmitter implementing the Observer pattern class EventEmitter { constructor() { this.events = {}; } // Subscribe to an event on(event, listener) { if (!this.events[event]) { this.events[event] = []; } this.events[event].push(listener); } // Emit an event emit(event, data) { if (this.events[event]) { this.events[event].forEach(listener => listener(data)); } } // Unsubscribe from an event off(event, listenerToRemove) { if (!this.events[event]) return; this.events[event] = this.events[event].filter( listener => listener !== listenerToRemove ); } } // Usage example const emitter = new EventEmitter(); function greet(name) { console.log(`Hello, ${name}!`); } emitter.on('greet', greet); emitter.emit('greet', 'Alice'); // Output: Hello, Alice! emitter.off('greet', greet); emitter.emit('greet', 'Bob'); // No output, as the listener was removed
copy

To see how the Observer pattern works in practice, consider the process step by step. First, you have a subject—in this case, the EventEmitter—that maintains a list of observers, or listeners, for different types of events. Observers subscribe to specific events by calling the on method, passing in the event name and a callback function. When the subject triggers an event using the emit method, it notifies all observers subscribed to that event, passing along any relevant data. Observers can also unsubscribe from events using the off method, which removes their callback from the subject's list. This subscription mechanism enables you to build flexible, event-driven systems where different parts of your application can respond to changes or actions without being tightly coupled to the source of those events.

1. The Observer pattern is useful for:

2. In the Observer pattern, what is the role of the observer?

question mark

The Observer pattern is useful for:

Select the correct answer

question mark

In the Observer pattern, what is the role of the observer?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 4. Capítulo 1
some-alt